phaedra 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +108 -21
- data/example/Gemfile +4 -0
- data/lib/phaedra/rack_app.rb +1 -1
- data/lib/phaedra/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1ac7eff901bd41a6da00ead154dc726635a7663597f5dd263fbe07c6f8df8821
|
4
|
+
data.tar.gz: 3eaafe5f99657959f22bd097191329404469087fc864ea980ba85a15156c87ce
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 83c5681c356986221a82202728e3efdfe912f9db5c82bf66b388bb38a1e9450aac8269bda442fbca528230f64e8615446e7f8231cae9080fabd11c02fefee216
|
7
|
+
data.tar.gz: 5631c0968e80834d7b571547151e1e9addcc64136e07e73c91828afcde277996763bf05b9591896ec530eefabbd2ede670f3fffbbdd902d26cadbcfe3a173138
|
data/README.md
CHANGED
@@ -53,7 +53,7 @@ Your function can support `get`, `post`, `put`, `patch`, and `delete` methods wh
|
|
53
53
|
|
54
54
|
Each method is provided access to `request` and `response` objects. If your function was directly instantiated by WEBrick, those will be `WEBrick::HTTPRequest` and `WEBrick::HTTPResponse` respectively. If your function was instantiated by Rack, those will be `Phaedra::Request` (a thin wrapper around `Rack::Request`) and `Rack::Response` respectively.
|
55
55
|
|
56
|
-
|
56
|
+
### Callbacks
|
57
57
|
|
58
58
|
Functions can define `action` callbacks:
|
59
59
|
|
@@ -77,30 +77,106 @@ end
|
|
77
77
|
|
78
78
|
You can modify the `request` object in a `before_action` callback to perform setup tasks before the actions are executed, or you can modify `response` in a `after_action` to further process the response.
|
79
79
|
|
80
|
-
|
80
|
+
### Shared Code You Only Want to Run Once
|
81
81
|
|
82
|
-
|
83
|
-
|
84
|
-
In your OpenFaaS project's function folder (e.g., `testphaedra`), simply define a file `handler.rb` which will load Phaedra's default Rack app:
|
82
|
+
You can use `require_relative` to load and execute shared Ruby code from another folder, say `lib`. This is particularly useful when setting up a database connection or performing expensive operations you only want to do once, rather than for every request.
|
85
83
|
|
86
84
|
```ruby
|
87
|
-
#
|
85
|
+
# api/run-it-once.rb
|
88
86
|
|
89
87
|
require "phaedra"
|
88
|
+
require_relative "../lib/shared_code"
|
90
89
|
|
91
|
-
class
|
92
|
-
def
|
93
|
-
|
94
|
-
|
95
|
-
|
90
|
+
class PhaedraFunction < Phaedra::Base
|
91
|
+
def get(params)
|
92
|
+
"Run it once! #{SharedCode.run_once}"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
```
|
96
96
|
|
97
|
-
|
98
|
-
|
97
|
+
```ruby
|
98
|
+
# lib/shared_code.rb
|
99
|
+
module SharedCode
|
100
|
+
def self.run_once
|
101
|
+
@one_time ||= Time.now
|
99
102
|
end
|
100
103
|
end
|
101
104
|
```
|
102
105
|
|
103
|
-
|
106
|
+
Now each time you invoke the function at `/api/run-it-once`, the timestamp will never change until the next redeployment.
|
107
|
+
|
108
|
+
## Deployment
|
109
|
+
|
110
|
+
### Vercel
|
111
|
+
|
112
|
+
All you have to do is create a static site repo ([Bridgetown](https://www.bridgetownrb.com), Jekyll, Middleman, etc.) with an `api` folder and Vercel will automatically set up the serverless functions every time there's a new branch or production deployment. As mentioned above, you'll need to ensure you add `Handler = PhaedraFunction` to the bottom of each Ruby function.
|
113
|
+
|
114
|
+
### OpenFaaS
|
115
|
+
|
116
|
+
We recommend using OpenFaaS' dockerfile template so you can define your own `Dockerfile` to book Rack + Phaedra using the Puma web server. This also allows you to customize the Docker image configuration to install and configure other tools as necessary.
|
117
|
+
|
118
|
+
First, make sure you've pulled down the template:
|
119
|
+
|
120
|
+
```sh
|
121
|
+
faas-cli template store pull dockerfile
|
122
|
+
```
|
123
|
+
|
124
|
+
Then add a `Dockerfile` to your OpenFaaS project's function folder (e.g., `testphaedra`):
|
125
|
+
|
126
|
+
```dockerfile
|
127
|
+
# testphaedra/Dockerfile
|
128
|
+
|
129
|
+
FROM openfaas/of-watchdog:0.7.7 as watchdog
|
130
|
+
|
131
|
+
FROM ruby:2.6.6-slim-stretch
|
132
|
+
|
133
|
+
COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
|
134
|
+
RUN chmod +x /usr/bin/fwatchdog
|
135
|
+
|
136
|
+
ARG ADDITIONAL_PACKAGE
|
137
|
+
RUN apt-get update \
|
138
|
+
&& apt-get install -qy --no-install-recommends build-essential ${ADDITIONAL_PACKAGE}
|
139
|
+
|
140
|
+
WORKDIR /home/app
|
141
|
+
|
142
|
+
# Use cache layer for Gemfile
|
143
|
+
COPY Gemfile .
|
144
|
+
RUN bundle install
|
145
|
+
RUN gem install puma -N
|
146
|
+
|
147
|
+
# Copy over the rest
|
148
|
+
COPY . .
|
149
|
+
|
150
|
+
# Create a non-root user
|
151
|
+
RUN addgroup --system app \
|
152
|
+
&& adduser --system --ingroup app app
|
153
|
+
RUN chown app:app -R /home/app
|
154
|
+
USER app
|
155
|
+
|
156
|
+
# Run Puma as the server process
|
157
|
+
ENV fprocess="puma -p 5000"
|
158
|
+
|
159
|
+
EXPOSE 8080
|
160
|
+
|
161
|
+
HEALTHCHECK --interval=2s CMD [ -e /tmp/.lock ] || exit 1
|
162
|
+
|
163
|
+
ENV upstream_url="http://127.0.0.1:5000"
|
164
|
+
ENV mode="http"
|
165
|
+
|
166
|
+
CMD ["fwatchdog"]
|
167
|
+
```
|
168
|
+
|
169
|
+
Next add the `config.ru` file to boot Rack:
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
# testphaedra/config.ru
|
173
|
+
|
174
|
+
require "phaedra"
|
175
|
+
|
176
|
+
run Phaedra::RackApp.new
|
177
|
+
```
|
178
|
+
|
179
|
+
Finally, add a YAML file that lives alongside your function folder:
|
104
180
|
|
105
181
|
```yaml
|
106
182
|
# testphaedra.yml
|
@@ -111,24 +187,24 @@ provider:
|
|
111
187
|
gateway: http://127.0.0.1:8080
|
112
188
|
functions:
|
113
189
|
testphaedra:
|
114
|
-
lang:
|
190
|
+
lang: dockerfile
|
115
191
|
handler: ./testphaedra
|
116
192
|
image: yourdockerusername/testphaedra:latest
|
117
193
|
```
|
118
194
|
|
195
|
+
(Replace `yourdockerusername` with your [Docker Hub](https://hub.docker.com) username.)
|
196
|
+
|
119
197
|
Now run `faas-cli up -f testphaedra.yml` to build and deploy the function. Given the Ruby function `testphaedra/api/run-me.rb`, you'd call it like so:
|
120
198
|
|
121
199
|
```sh
|
122
200
|
curl http://127.0.0.1:8080/function/testphaedra/api/run-me
|
123
|
-
|
124
|
-
# output of the Ruby function
|
125
201
|
```
|
126
202
|
|
127
|
-
In case you're wondering: yes, with Phaedra you can write multiple Ruby functions accessible via different URL paths
|
203
|
+
In case you're wondering: yes, with Phaedra you can write multiple Ruby functions which will be accessible via different URL paths—all handled by a single OpenFaaS function. Of course it's possible set up multiple Phaedra projects and deploy them as separate OpenFaaS functions if you wish.
|
128
204
|
|
129
|
-
|
205
|
+
### Rack
|
130
206
|
|
131
|
-
Booting Phaedra up as Rack app is very simple. All you need is a `config.ru` file alongside your `api` folder:
|
207
|
+
Booting Phaedra up as Rack app is very simple. All you need to do is add a `config.ru` file alongside your `api` folder:
|
132
208
|
|
133
209
|
```ruby
|
134
210
|
require "phaedra"
|
@@ -138,7 +214,16 @@ run Phaedra::RackApp.new
|
|
138
214
|
|
139
215
|
Then run `rackup` in the terminal.
|
140
216
|
|
141
|
-
|
217
|
+
The settings (and their defaults) you can pass to the `new` method are as follows:
|
218
|
+
|
219
|
+
```ruby
|
220
|
+
{
|
221
|
+
"root_dir" => Dir.pwd,
|
222
|
+
"serverless_api_dir" => "api"
|
223
|
+
}
|
224
|
+
```
|
225
|
+
|
226
|
+
### WEBrick
|
142
227
|
|
143
228
|
Integrating Phaedra into a WEBrick server is pretty straightforward. Given a `server` object, it can be accomplished thusly:
|
144
229
|
|
@@ -172,6 +257,8 @@ load File.join(Dir.pwd, "api", "func.rb")
|
|
172
257
|
@server.mount '/path', Handler
|
173
258
|
```
|
174
259
|
|
260
|
+
This method precludes any automatic routing by Phaedra, so it's discouraged unless you are using WEBrick within a larger setup that utilizes its own routing method. (Interestingly enough, that's how Vercel works under the hood.)
|
261
|
+
|
175
262
|
----
|
176
263
|
|
177
264
|
## Development
|
data/example/Gemfile
ADDED
data/lib/phaedra/rack_app.rb
CHANGED
data/lib/phaedra/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: phaedra
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jared White
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-06-
|
11
|
+
date: 2020-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- LICENSE.txt
|
81
81
|
- README.md
|
82
82
|
- Rakefile
|
83
|
+
- example/Gemfile
|
83
84
|
- example/api/simple.rb
|
84
85
|
- example/api/the-time.rb
|
85
86
|
- example/config.ru
|