phaedra 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +57 -10
- data/example/api/simple.rb +10 -0
- data/example/api/the-time.rb +0 -1
- data/lib/phaedra/base.rb +6 -2
- data/lib/phaedra/rack_app.rb +3 -3
- data/lib/phaedra/version.rb +1 -1
- data/phaedra.gemspec +1 -1
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f9629e11c78d2a35fa9929d69a79f57fe1711fa4c6f6b4d73e70f8be43019918
|
4
|
+
data.tar.gz: 65a0caf688ea9172723fe398e29861f2c22f99df02dce2d2e34523fbbb4c929b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8c54e4a03e0cbbd16c4d444215ce29102315e1be92942a9c1d3542eb6eeb948de8b78fff6f5f5913e3ef1ee2c7eae11aaac70545682bcc8f4884987cdb5c28a1
|
7
|
+
data.tar.gz: 666994b4fbb211c6a3bc555ac4f663997d6ed9ae24fcae4d670c76fe6392bb1f3761206000657edaa21e51778a2ebac5dad5a0171cfa45f032d9dbd69939f7d6
|
data/README.md
CHANGED
@@ -1,10 +1,8 @@
|
|
1
1
|
# Phaedra: Serverless Ruby Functions
|
2
2
|
|
3
|
-
|
3
|
+
Phaedra is a web microframework for writing serverless Ruby functions. They are isolated pieces of logic which respond to HTTP requests (GET, POST, etc.) and typically get mounted at a particular URL path. They can be tested locally and deployed to a supported serverless hosting platform or to any [Rack-compatible web server](https://github.com/rack/rack).
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
Serverless compatibility is presently focused on [Vercel](https://vercel.com), but there are likely additional platforms we'll be adding support for in the future (OpenFaaS, Fission, etc.).
|
5
|
+
Serverless compatibility is presently focused on [Vercel](https://vercel.com) and [OpenFaaS](https://openfaas.com), but there are likely additional platforms we'll be adding support for in the future.
|
8
6
|
|
9
7
|
## Installation
|
10
8
|
|
@@ -32,7 +30,7 @@ Functions are single Ruby files which respond to a URL path (aka `/api/path/to/f
|
|
32
30
|
|
33
31
|
Functions are written as subclasses of `Phaedra::Base` using the name `PhaedraFunction`. The `params` argument is a Hash containing the parsed contents of the incoming query string, form data, or body JSON. The response object returned by your function is typically a Hash which will be transformed into JSON output automatically, but it can also be plain text.
|
34
32
|
|
35
|
-
Some platforms require the function class name to be `Handler`, so you can put that at the bottom of your file for full compatibility.
|
33
|
+
Some platforms such as Vercel require the function class name to be `Handler`, so you can put that at the bottom of your file for full compatibility.
|
36
34
|
|
37
35
|
Here's a basic example:
|
38
36
|
|
@@ -79,6 +77,55 @@ end
|
|
79
77
|
|
80
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.
|
81
79
|
|
80
|
+
## OpenFaaS
|
81
|
+
|
82
|
+
We recommend using OpenFaaS' [ruby-http template](https://github.com/openfaas-incubator/ruby-http). It boots up a Sinatra/WEBrick server and then passes all requests along to a Handler object.
|
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:
|
85
|
+
|
86
|
+
```ruby
|
87
|
+
# testphaedra/handler.rb
|
88
|
+
|
89
|
+
require "phaedra"
|
90
|
+
|
91
|
+
class Handler
|
92
|
+
def run(_body, env)
|
93
|
+
status, headers, body = Phaedra::RackApp.new({
|
94
|
+
"root_dir" => File.join(Dir.pwd, "function")
|
95
|
+
}).call(env)
|
96
|
+
|
97
|
+
# The OpenFaaS ruby-http return array is backwards from Rack :/
|
98
|
+
[body.join(""), headers, status]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
```
|
102
|
+
|
103
|
+
Next, add a YAML file that lives alongside your function folder:
|
104
|
+
|
105
|
+
```yaml
|
106
|
+
# testphaedra.yml
|
107
|
+
|
108
|
+
version: 1.0
|
109
|
+
provider:
|
110
|
+
name: openfaas
|
111
|
+
gateway: http://127.0.0.1:8080
|
112
|
+
functions:
|
113
|
+
testphaedra:
|
114
|
+
lang: ruby-http
|
115
|
+
handler: ./testphaedra
|
116
|
+
image: yourdockerusername/testphaedra:latest
|
117
|
+
```
|
118
|
+
|
119
|
+
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
|
+
|
121
|
+
```sh
|
122
|
+
curl http://127.0.0.1:8080/function/testphaedra/api/run-me
|
123
|
+
|
124
|
+
# output of the Ruby function
|
125
|
+
```
|
126
|
+
|
127
|
+
In case you're wondering: yes, with Phaedra you can write multiple Ruby functions accessible via different URL paths that will all get handled by a single OpenFaaS function. Obviously you're welcome to set up multiple Phaedra projects and deploy them as separate OpenFaaS functions if you wish.
|
128
|
+
|
82
129
|
## Rack
|
83
130
|
|
84
131
|
Booting Phaedra up as Rack app is very simple. All you need is a `config.ru` file alongside your `api` folder:
|
@@ -105,13 +152,13 @@ server.mount_proc "/#{base_api_folder}" do |req, res|
|
|
105
152
|
ruby_path = File.join(full_api_path, api_folder, "#{endpoint}.rb")
|
106
153
|
|
107
154
|
if File.exist?(ruby_path)
|
108
|
-
|
109
|
-
|
155
|
+
if Object.constants.include?(:PhaedraFunction)
|
156
|
+
Object.send(:remove_const, :PhaedraFunction)
|
157
|
+
end
|
110
158
|
load ruby_path
|
111
|
-
$VERBOSE = original_verbosity
|
112
159
|
|
113
|
-
|
114
|
-
|
160
|
+
func = PhaedraFunction.new
|
161
|
+
func.service(req, res)
|
115
162
|
else
|
116
163
|
raise HTTPStatus::NotFound, "`#{req.path}' not found."
|
117
164
|
end
|
data/example/api/the-time.rb
CHANGED
data/lib/phaedra/base.rb
CHANGED
@@ -112,7 +112,7 @@ module Phaedra
|
|
112
112
|
|
113
113
|
def set_initial_status
|
114
114
|
@res.status = 200
|
115
|
-
@res["Content-Type"] =
|
115
|
+
@res["Content-Type"] = "application/json"
|
116
116
|
end
|
117
117
|
|
118
118
|
def call_method_action(params)
|
@@ -121,7 +121,11 @@ module Phaedra
|
|
121
121
|
end
|
122
122
|
|
123
123
|
def complete_response
|
124
|
-
|
124
|
+
if @res.body.is_a?(String) && !@res["Content-Type"].start_with?("text/")
|
125
|
+
@res["Content-Type"] = "text/plain"
|
126
|
+
elsif @res["Content-Type"] == "application/json"
|
127
|
+
@res.body = @res.body.to_json
|
128
|
+
end
|
125
129
|
end
|
126
130
|
|
127
131
|
def error_condition
|
data/lib/phaedra/rack_app.rb
CHANGED
@@ -33,10 +33,10 @@ module Phaedra
|
|
33
33
|
endpoint = File.basename(req.path)
|
34
34
|
ruby_path = File.join(full_api_path, api_folder, "#{endpoint}.rb")
|
35
35
|
if File.exist?(ruby_path)
|
36
|
-
|
37
|
-
|
36
|
+
if Object.constants.include?(:PhaedraFunction)
|
37
|
+
Object.send(:remove_const, :PhaedraFunction)
|
38
|
+
end
|
38
39
|
load ruby_path
|
39
|
-
$VERBOSE = original_verbosity
|
40
40
|
|
41
41
|
func = PhaedraFunction.new
|
42
42
|
func.service(req, res)
|
data/lib/phaedra/version.rb
CHANGED
data/phaedra.gemspec
CHANGED
@@ -6,7 +6,7 @@ Gem::Specification.new do |spec|
|
|
6
6
|
spec.authors = ["Jared White"]
|
7
7
|
spec.email = ["jared@whitefusion.io"]
|
8
8
|
|
9
|
-
spec.summary = %q{Write serverless Ruby functions via a REST microframework compatible with Rack or WEBrick.}
|
9
|
+
spec.summary = %q{Write serverless Ruby functions via a REST-like microframework compatible with Rack or WEBrick.}
|
10
10
|
spec.description = spec.summary
|
11
11
|
spec.homepage = "https://github.com/whitefusionhq/phaedra"
|
12
12
|
spec.license = "MIT"
|
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.
|
4
|
+
version: 0.3.0
|
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-07 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '12.0'
|
69
|
-
description: Write serverless Ruby functions via a REST microframework compatible
|
69
|
+
description: Write serverless Ruby functions via a REST-like microframework compatible
|
70
70
|
with Rack or WEBrick.
|
71
71
|
email:
|
72
72
|
- jared@whitefusion.io
|
@@ -80,6 +80,7 @@ files:
|
|
80
80
|
- LICENSE.txt
|
81
81
|
- README.md
|
82
82
|
- Rakefile
|
83
|
+
- example/api/simple.rb
|
83
84
|
- example/api/the-time.rb
|
84
85
|
- example/config.ru
|
85
86
|
- lib/phaedra.rb
|
@@ -110,6 +111,6 @@ requirements: []
|
|
110
111
|
rubygems_version: 3.0.8
|
111
112
|
signing_key:
|
112
113
|
specification_version: 4
|
113
|
-
summary: Write serverless Ruby functions via a REST microframework compatible
|
114
|
-
Rack or WEBrick.
|
114
|
+
summary: Write serverless Ruby functions via a REST-like microframework compatible
|
115
|
+
with Rack or WEBrick.
|
115
116
|
test_files: []
|