Skip to content

Commit 48032c6

Browse files
author
Graham Jenson
authored
Merge pull request #41 from bazelruby/graham/rails-server-running
[Feature] example rails server running
2 parents f6f7bab + 79bbd82 commit 48032c6

File tree

18 files changed

+495
-62
lines changed

18 files changed

+495
-62
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
2+
#
3+
# If you find yourself ignoring temporary files generated by your text editor
4+
# or operating system, you probably want to add a global ignore instead:
5+
# git config --global core.excludesfile '~/.gitignore_global'
6+
7+
# Ignore bundler config.
8+
/.bundle
9+
10+
# Ignore the default SQLite database.
11+
/db/*.sqlite3
12+
/db/*.sqlite3-journal
13+
/db/*.sqlite3-*
14+
15+
# Ignore all logfiles and tempfiles.
16+
/log/*
17+
/tmp/*
18+
!/log/.keep
19+
!/tmp/.keep
20+
21+
# Ignore uploaded files in development.
22+
/storage/*
23+
!/storage/.keep
24+
.byebug_history
25+
26+
# Ignore master key for decrypting credentials and more.
27+
/config/master.key
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
2.6.5

examples/simple_rails_api/BUILD

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package(default_visibility = ["//:__subpackages__"])
2+
3+
load(
4+
"@bazelruby_ruby_rules//ruby:defs.bzl",
5+
"ruby_binary",
6+
"ruby_test",
7+
)
8+
9+
ruby_binary(
10+
name = "server",
11+
srcs = glob(
12+
include = [
13+
"app/**/*",
14+
"config/**/*",
15+
"bin/*",
16+
"config.ru",
17+
],
18+
),
19+
main = ":bin/rails",
20+
deps = [
21+
"@bundle//:rails",
22+
],
23+
)

examples/simple_rails_api/Gemfile

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
source 'https://rubygems.org'
2+
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
3+
4+
ruby '2.6.5'
5+
6+
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
7+
gem 'rails', '~> 6.0.2'
8+
# Use sqlite3 as the database for Active Record
9+
gem 'sqlite3', '~> 1.4'
10+
# Use Puma as the app server
11+
gem 'puma', '~> 4.1'
12+
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
13+
# gem 'jbuilder', '~> 2.7'
14+
# Use Redis adapter to run Action Cable in production
15+
# gem 'redis', '~> 4.0'
16+
# Use Active Model has_secure_password
17+
# gem 'bcrypt', '~> 3.1.7'
18+
19+
# Use Active Storage variant
20+
# gem 'image_processing', '~> 1.2'
21+
22+
# Reduces boot times through caching; required in config/boot.rb
23+
gem 'bootsnap', '>= 1.4.2', require: false
24+
25+
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
26+
# gem 'rack-cors'
27+
28+
group :development, :test do
29+
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
30+
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
31+
end
32+
33+
group :development do
34+
gem 'listen', '>= 3.0.5', '< 3.2'
35+
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
36+
gem 'spring'
37+
gem 'spring-watcher-listen', '~> 2.0.0'
38+
end
39+
40+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
41+
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
GEM
2+
remote: https://rubygems.org/
3+
specs:
4+
actioncable (6.0.2)
5+
actionpack (= 6.0.2)
6+
nio4r (~> 2.0)
7+
websocket-driver (>= 0.6.1)
8+
actionmailbox (6.0.2)
9+
actionpack (= 6.0.2)
10+
activejob (= 6.0.2)
11+
activerecord (= 6.0.2)
12+
activestorage (= 6.0.2)
13+
activesupport (= 6.0.2)
14+
mail (>= 2.7.1)
15+
actionmailer (6.0.2)
16+
actionpack (= 6.0.2)
17+
actionview (= 6.0.2)
18+
activejob (= 6.0.2)
19+
mail (~> 2.5, >= 2.5.4)
20+
rails-dom-testing (~> 2.0)
21+
actionpack (6.0.2)
22+
actionview (= 6.0.2)
23+
activesupport (= 6.0.2)
24+
rack (~> 2.0)
25+
rack-test (>= 0.6.3)
26+
rails-dom-testing (~> 2.0)
27+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
28+
actiontext (6.0.2)
29+
actionpack (= 6.0.2)
30+
activerecord (= 6.0.2)
31+
activestorage (= 6.0.2)
32+
activesupport (= 6.0.2)
33+
nokogiri (>= 1.8.5)
34+
actionview (6.0.2)
35+
activesupport (= 6.0.2)
36+
builder (~> 3.1)
37+
erubi (~> 1.4)
38+
rails-dom-testing (~> 2.0)
39+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
40+
activejob (6.0.2)
41+
activesupport (= 6.0.2)
42+
globalid (>= 0.3.6)
43+
activemodel (6.0.2)
44+
activesupport (= 6.0.2)
45+
activerecord (6.0.2)
46+
activemodel (= 6.0.2)
47+
activesupport (= 6.0.2)
48+
activestorage (6.0.2)
49+
actionpack (= 6.0.2)
50+
activejob (= 6.0.2)
51+
activerecord (= 6.0.2)
52+
marcel (~> 0.3.1)
53+
activesupport (6.0.2)
54+
concurrent-ruby (~> 1.0, >= 1.0.2)
55+
i18n (>= 0.7, < 2)
56+
minitest (~> 5.1)
57+
tzinfo (~> 1.1)
58+
zeitwerk (~> 2.2)
59+
bootsnap (1.4.5)
60+
msgpack (~> 1.0)
61+
builder (3.2.4)
62+
byebug (11.0.1)
63+
concurrent-ruby (1.1.5)
64+
crass (1.0.5)
65+
erubi (1.9.0)
66+
ffi (1.11.3)
67+
globalid (0.4.2)
68+
activesupport (>= 4.2.0)
69+
i18n (1.7.0)
70+
concurrent-ruby (~> 1.0)
71+
listen (3.1.5)
72+
rb-fsevent (~> 0.9, >= 0.9.4)
73+
rb-inotify (~> 0.9, >= 0.9.7)
74+
ruby_dep (~> 1.2)
75+
loofah (2.4.0)
76+
crass (~> 1.0.2)
77+
nokogiri (>= 1.5.9)
78+
mail (2.7.1)
79+
mini_mime (>= 0.1.1)
80+
marcel (0.3.3)
81+
mimemagic (~> 0.3.2)
82+
method_source (0.9.2)
83+
mimemagic (0.3.3)
84+
mini_mime (1.0.2)
85+
mini_portile2 (2.4.0)
86+
minitest (5.13.0)
87+
msgpack (1.3.1)
88+
nio4r (2.5.2)
89+
nokogiri (1.10.7)
90+
mini_portile2 (~> 2.4.0)
91+
puma (4.3.1)
92+
nio4r (~> 2.0)
93+
rack (2.0.7)
94+
rack-test (1.1.0)
95+
rack (>= 1.0, < 3)
96+
rails (6.0.2)
97+
actioncable (= 6.0.2)
98+
actionmailbox (= 6.0.2)
99+
actionmailer (= 6.0.2)
100+
actionpack (= 6.0.2)
101+
actiontext (= 6.0.2)
102+
actionview (= 6.0.2)
103+
activejob (= 6.0.2)
104+
activemodel (= 6.0.2)
105+
activerecord (= 6.0.2)
106+
activestorage (= 6.0.2)
107+
activesupport (= 6.0.2)
108+
bundler (>= 1.3.0)
109+
railties (= 6.0.2)
110+
sprockets-rails (>= 2.0.0)
111+
rails-dom-testing (2.0.3)
112+
activesupport (>= 4.2.0)
113+
nokogiri (>= 1.6)
114+
rails-html-sanitizer (1.3.0)
115+
loofah (~> 2.3)
116+
railties (6.0.2)
117+
actionpack (= 6.0.2)
118+
activesupport (= 6.0.2)
119+
method_source
120+
rake (>= 0.8.7)
121+
thor (>= 0.20.3, < 2.0)
122+
rake (13.0.1)
123+
rb-fsevent (0.10.3)
124+
rb-inotify (0.10.0)
125+
ffi (~> 1.0)
126+
ruby_dep (1.5.0)
127+
spring (2.1.0)
128+
spring-watcher-listen (2.0.1)
129+
listen (>= 2.7, < 4.0)
130+
spring (>= 1.2, < 3.0)
131+
sprockets (4.0.0)
132+
concurrent-ruby (~> 1.0)
133+
rack (> 1, < 3)
134+
sprockets-rails (3.2.1)
135+
actionpack (>= 4.0)
136+
activesupport (>= 4.0)
137+
sprockets (>= 3.0.0)
138+
sqlite3 (1.4.1)
139+
thor (1.0.0)
140+
thread_safe (0.3.6)
141+
tzinfo (1.2.5)
142+
thread_safe (~> 0.1)
143+
websocket-driver (0.7.1)
144+
websocket-extensions (>= 0.1.0)
145+
websocket-extensions (0.1.4)
146+
zeitwerk (2.2.2)
147+
148+
PLATFORMS
149+
ruby
150+
151+
DEPENDENCIES
152+
bootsnap (>= 1.4.2)
153+
byebug
154+
listen (>= 3.0.5, < 3.2)
155+
puma (~> 4.1)
156+
rails (~> 6.0.2)
157+
spring
158+
spring-watcher-listen (~> 2.0.0)
159+
sqlite3 (~> 1.4)
160+
tzinfo-data
161+
162+
RUBY VERSION
163+
ruby 2.6.5p114
164+
165+
BUNDLED WITH
166+
2.0.2
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Rails API App
2+
3+
This example is meant to be an example of how to structure a rails application in a `bazel`-ish way.
4+
5+
## Run the Example
6+
7+
To start the application run:
8+
9+
```
10+
bazel run :server -- server
11+
```
12+
13+
then call the `home_controller.rb#home` method with:
14+
15+
```
16+
curl 127.0.0.1:3000
17+
```
18+
19+
## Creating the examples
20+
This application was created by running `rails new simple_rails_api --api`, adding the `BUILD` and `WORKSPACE` files, then executing the changes below.
21+
22+
### Deleting folders
23+
24+
This is a simple example app so we deleted a lot of the folders, e.g. `vendor` `public` `test` `tmp` `db` `lib` `log` folders.
25+
26+
### Bundler
27+
28+
Since we are importing the rails application, we do not want to run `bundler/setup` as this looks at the Gemfile provided. This means that any gem setup will have to be manual.
29+
30+
### Rails.root APP_PATH
31+
32+
Because the directory the application being started is in the bazel-bin folder (not the source directory) we need to change some config.
33+
34+
In `bin/rails` set:
35+
36+
```
37+
APP_PATH = Dir.pwd + '/config/application'
38+
```
39+
40+
and in `config/application.rb` add:
41+
42+
```
43+
config.root = Dir.pwd
44+
```
45+
46+
### Add simple home controller
47+
48+
Added a simple home controller to make sure everything was working correctly
49+
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
workspace(name = "bazelruby_ruby_rules_example")
2+
3+
# Importing rules_ruby from the parent directory for developing
4+
# rules_ruby itself...
5+
local_repository(
6+
name = "bazelruby_ruby_rules",
7+
path = "../..",
8+
)
9+
10+
load(
11+
"@bazelruby_ruby_rules//ruby:deps.bzl",
12+
"ruby_register_toolchains",
13+
"ruby_rules_dependencies",
14+
)
15+
16+
ruby_rules_dependencies()
17+
18+
ruby_register_toolchains(version = "2.6.5")
19+
20+
load("@bazel_skylib//:workspace.bzl", "bazel_skylib_workspace")
21+
22+
bazel_skylib_workspace()
23+
24+
load("@bazelruby_ruby_rules//ruby:defs.bzl", "bundle_install")
25+
26+
bundle_install(
27+
name = "bundle",
28+
gemfile = "//:Gemfile",
29+
gemfile_lock = "//:Gemfile.lock",
30+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
class ApplicationController < ActionController::API
2+
include ActionController::MimeResponds
3+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class HomeController < ApplicationController
2+
def home
3+
respond_to do |format|
4+
format.html { render json: "hello" }
5+
format.json { render json: {message: "hello"} }
6+
end
7+
end
8+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env ruby
2+
3+
APP_PATH = Dir.pwd + '/config/application'
4+
5+
require 'rails/commands'

0 commit comments

Comments
 (0)