Skip to content

Commit 2b950e9

Browse files
committed
Enable execution in Docker
1 parent dd020ed commit 2b950e9

File tree

5 files changed

+122
-12
lines changed

5 files changed

+122
-12
lines changed

.dockerignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# The following should be excluded from generated Docker images
2+
.dockerignore
3+
.git/
4+
.gitignore
5+
.idea/
6+
Dockerfile
7+
db/*.sqlite3

Dockerfile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
FROM ruby:3.0.2
2+
3+
RUN mkdir /app
4+
WORKDIR /app
5+
6+
COPY Gemfile /app/Gemfile
7+
COPY Gemfile.lock /app/Gemfile.lock
8+
RUN bundle install
9+
10+
COPY . /app
11+
EXPOSE 3000
12+
13+
CMD ["rails", "server", "-b", "0.0.0.0"]

README.md

+94-11
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,114 @@ The following external libraries were *directly* utilized in this project.
88
| --- | --- | --- |
99
| Ruby | https://www.ruby-lang.org/ | Well...it's obvious isn't it?! |
1010
| Rails | https://rubyonrails.org/ | Framework for modern web applications |
11+
| Puma | https://puma.io/ | Web server for executing your Rails application |
1112
| SQLite | https://www.sqlite.org/index.html | The lightweight database |
1213

1314
## Build
14-
TODO
15+
Builds are performed using the `Rakefile` provided in the project root.
1516

1617
#### CLI
17-
TODO
18+
In order to buid from the command-line, you'll need to have Ruby (3.0+) and Rails (6.1.4+) installed on your system.
19+
20+
:point_up: NOTE: To initially build the project, you may need to run the `rake` command to install the tools utilized for builds.
21+
22+
Once built, you can **migrate** the database scripts and run the application:
23+
```shell script
24+
$ rails db:setup; rails db:migrate; rails server
25+
```
1826

1927
#### Docker
20-
TODO
28+
For those who do not have the Ruby and Rails dependencies installed locally, [Docker](https://hub.docker.com/) is an option to build the application and run the application within a container.
29+
30+
To execute the Rails application, use the following Docker command to create your image:
31+
```shell script
32+
docker build -t github.com/weesvc/weesvc-rails:0.0.1-SNAPSHOT .
33+
```
34+
Once the the image is available, you can simply run the provided script which will open a browser to access the service
35+
at http://localhost:3000/api/hello .
36+
37+
```shell script
38+
$ ./docker-run.sh
39+
```
40+
:point_up: NOTE: the `docker-run.sh` script is setup to **not** maintain state between executions. This means each
41+
time you start the container, you will be starting with a freshly created database.
2142

2243
## Using the Application
23-
TODO
44+
:point_up: TIP: Use the very cool [HTTPie](https://httpie.org/) application for testing locally from the command-line.
45+
46+
Execute a `GET` command to retrieve the available _places_ from the database.
47+
```shell script
48+
$ http GET :3000/api/places
49+
50+
HTTP/1.1 200 OK
51+
Content-Type: application/json; charset=utf-8
52+
53+
[]
2454
```
25-
rails new weesvc-rails-api --api
26-
rails generate model Place name:string description:string
27-
rails generate controller api/places
28-
rails db:setup
29-
rails db:migrate
30-
rails server
55+
Add a _place_ into the database using a `POST` command.
56+
```shell script
57+
$ http POST :3000/api/places name=NISC description="NISC Lake St. Louis Office" latitude:=38.7839 longitude:=90.7878
58+
59+
HTTP/1.1 200 OK
60+
Content-Type: application/json; charset=utf-8
61+
62+
{
63+
"created_at": "2021-11-14T19:07:38.049Z",
64+
"description": "NISC Lake St. Louis Office",
65+
"id": 1,
66+
"latitude": 38.7839,
67+
"longitude": 90.7878,
68+
"name": "NISC",
69+
"updated_at": "2021-11-14T19:07:38.049Z"
70+
}
3171
```
32-
Thanks to https://medium.com/@oliver.seq/creating-a-rest-api-with-rails-2a07f548e5dc
72+
Run the `GET` command again to retrieve _places_ which now include your newly added _place_!
73+
```shell script
74+
$ http GET :3000/api/places
3375

76+
HTTP/1.1 200 OK
77+
Content-Type: application/json; charset=utf-8
78+
79+
[
80+
{
81+
"created_at": "2021-11-14T19:10:32.042Z",
82+
"description": "NISC Lake St. Louis Office",
83+
"id": 1,
84+
"latitude": 38.7839,
85+
"longitude": 90.7878,
86+
"name": "NISC",
87+
"updated_at": "2021-11-14T19:10:32.042Z"
88+
}
89+
]
90+
```
91+
Use the `PATCH` command to update a specific value. For example we'll update the `Description` as follows:
92+
```shell script
93+
$ http PATCH :3000/api/places/1 description="Lake St. Louis"
3494

95+
HTTP/1.1 200 OK
96+
Content-Type: application/json; charset=utf-8
3597

98+
{
99+
"created_at": "2021-11-14T19:10:32.042Z",
100+
"description": "Lake St. Louis",
101+
"id": 1,
102+
"latitude": 38.7839,
103+
"longitude": 90.7878,
104+
"name": "NISC",
105+
"updated_at": "2021-11-14T19:13:16.232Z"
106+
}
107+
```
108+
This returns the newly "patched" version of the _place_. Next we'll remove the row using the `DELETE` method.
109+
```shell script
110+
$ http DELETE :3000/api/places/1
111+
112+
HTTP/1.1 200 OK
113+
Content-Type: application/json; charset=utf-8
114+
115+
{
116+
"message": "Place successfully deleted."
117+
}
118+
```
36119

37120
## API Compliance
38121
A core requirement for all _WeeSVC_ implementations is to implement the same API which are utilized for benchmark comparisons. To ensure compliance with the required API, [k6](https://k6.io/) is utilized within the [Workbench](https://github.com/weesvc/workbench) project.

config/routes.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
root to: redirect("/api/hello")
44

55
namespace :api do
6-
resources :hello do
6+
resources :hello, only: [:index] do
77
end
88
resources :places do
99
end

docker-run.sh

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
open http://localhost:3000/api/hello
4+
5+
docker run -it --name weesvc-rails --rm -p 3000:3000 \
6+
github.com/weesvc/weesvc-rails:0.0.1-SNAPSHOT \
7+
/bin/sh -c "rails db:setup; rails db:migrate; rails server -b 0.0.0.0"

0 commit comments

Comments
 (0)