faastruby 0.4.2 → 0.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/Gemfile.lock +1 -1
- data/exe/faastruby-server +74 -47
- data/faastruby.gemspec +1 -1
- data/lib/faastruby/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89c1ce111c17f6eb8b69d8d47cffc7dea7a6a48e373d43bd30cd7303c097d21f
|
4
|
+
data.tar.gz: e616406ad0c57004cc55e63b3c5db761164fb4ff4d3f40cab8474fcdafe7a3bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 464be81d0ccc2a374a69111e61c29a7ee7f6797d3aeb4ce1c9e30bc76578fa07d313930a5034e566b5ce46a57356c8ab6897e49dea307e22530f2c0960626786
|
7
|
+
data.tar.gz: c2158f4ad8209e2028519c97edac9434e170158d801dc941160a5ea12ac25569aaa7b1efe01f056d5faf89238160eb31cee021261bc7ce73be05d6c326dad378
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/exe/faastruby-server
CHANGED
@@ -6,57 +6,84 @@ require 'yaml'
|
|
6
6
|
require 'oj'
|
7
7
|
require 'faastruby-rpc'
|
8
8
|
|
9
|
-
module
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
}
|
18
|
-
respond_with(Oj.dump(body), status: 500, headers: {'Content-Type' => 'application/json'})
|
19
|
-
rescue Exception => e
|
20
|
-
body = {
|
21
|
-
'error' => e.message,
|
22
|
-
'location' => e.backtrace&.first,
|
23
|
-
}
|
24
|
-
respond_with(Oj.dump(body), status: 500, headers: {'Content-Type' => 'application/json'})
|
9
|
+
module FaaStRuby
|
10
|
+
class DoubleRenderError < StandardError; end
|
11
|
+
end
|
12
|
+
|
13
|
+
module FaaStRuby
|
14
|
+
class Runner
|
15
|
+
def initialize
|
16
|
+
@rendered = false
|
25
17
|
end
|
26
|
-
end
|
27
|
-
def self.respond_with(body, status: 200, headers: {})
|
28
|
-
r = FaaStRubyFunction::Response.new(:body, :status, :headers)
|
29
|
-
r.new(body, status, headers)
|
30
|
-
end
|
31
18
|
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
19
|
+
def call(workspace_name, function_name, event, args)
|
20
|
+
begin
|
21
|
+
load "./#{workspace_name}/#{function_name}/handler.rb"
|
22
|
+
response = handler(event, *args)
|
23
|
+
return response if response.is_a?(FaaStRuby::Response)
|
24
|
+
body = {
|
25
|
+
'error' => "Please use the helpers 'render' or 'respond_with' as your function return value."
|
26
|
+
}
|
27
|
+
FaaStRuby::Response.new(body: Oj.dump(body), status: 500, headers: {'Content-Type' => 'application/json'})
|
28
|
+
rescue Exception => e
|
29
|
+
body = {
|
30
|
+
'error' => e.message,
|
31
|
+
'location' => e.backtrace&.first,
|
32
|
+
}
|
33
|
+
FaaStRuby::Response.new(body: Oj.dump(body), status: 500, headers: {'Content-Type' => 'application/json'})
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def rendered!
|
38
|
+
@rendered = true
|
39
|
+
end
|
40
|
+
def rendered?
|
41
|
+
@rendered
|
42
|
+
end
|
43
|
+
|
44
|
+
def respond_with(body, status: 200, headers: {})
|
45
|
+
raise FaaStRuby::DoubleRenderError.new("You called 'render' or 'respond_with' twice in your handler method") if rendered?
|
46
|
+
response = FaaStRuby::Response.new(body: body, status: status, headers: headers)
|
47
|
+
rendered!
|
48
|
+
response
|
49
|
+
end
|
50
|
+
|
51
|
+
def render(js: nil, body: nil, inline: nil, html: nil, json: nil, yaml: nil, text: nil, status: 200, headers: {}, content_type: nil)
|
52
|
+
headers["Content-Type"] = content_type if content_type
|
53
|
+
case
|
54
|
+
when json
|
55
|
+
headers["Content-Type"] ||= "application/json"
|
56
|
+
resp_body = json.is_a?(String) ? json : Oj.dump(json)
|
57
|
+
when html, inline
|
58
|
+
headers["Content-Type"] ||= "text/html"
|
59
|
+
resp_body = html
|
60
|
+
when text
|
61
|
+
headers["Content-Type"] ||= "text/plain"
|
62
|
+
resp_body = text
|
63
|
+
when yaml
|
64
|
+
headers["Content-Type"] ||= "application/yaml"
|
65
|
+
resp_body = yaml.is_a?(String) ? yaml : YAML.load(yaml)
|
66
|
+
when body
|
67
|
+
headers["Content-Type"] ||= "application/octet-stream"
|
68
|
+
resp_body = raw
|
69
|
+
when js
|
70
|
+
headers["Content-Type"] ||= "text/javascript"
|
71
|
+
resp_body = js
|
72
|
+
end
|
73
|
+
respond_with(resp_body, status: status, headers: headers)
|
53
74
|
end
|
54
|
-
respond_with(resp_body, status: status, headers: headers)
|
55
75
|
end
|
76
|
+
|
56
77
|
class Event < Struct
|
57
78
|
end
|
58
79
|
|
59
|
-
class Response
|
80
|
+
class Response
|
81
|
+
attr_accessor :body, :status, :headers
|
82
|
+
def initialize(body:, status: 200, headers: {})
|
83
|
+
@body = body
|
84
|
+
@status = status
|
85
|
+
@headers = headers
|
86
|
+
end
|
60
87
|
end
|
61
88
|
end
|
62
89
|
|
@@ -71,11 +98,11 @@ class FaaStRubyServer < Sinatra::Application
|
|
71
98
|
end
|
72
99
|
set :server, %w[puma]
|
73
100
|
set :run, false
|
74
|
-
set :show_exceptions,
|
101
|
+
set :show_exceptions, true
|
75
102
|
|
76
103
|
register Sinatra::MultiRoute
|
77
104
|
route :get, :post, :put, :patch, :delete, '/:workspace_name/:function_name' do
|
78
|
-
e =
|
105
|
+
e = FaaStRuby::Event.new(:body, :query_params, :headers, :context)
|
79
106
|
headers = env.select { |key, value| key.include?('HTTP_') || ['CONTENT_TYPE', 'CONTENT_LENGTH', 'REMOTE_ADDR', 'REQUEST_METHOD', 'QUERY_STRING'].include?(key) }
|
80
107
|
if headers.has_key?("HTTP_FAASTRUBY_RPC")
|
81
108
|
body = nil
|
@@ -87,7 +114,7 @@ class FaaStRubyServer < Sinatra::Application
|
|
87
114
|
query_params = parse_query(request.query_string)
|
88
115
|
context = set_context(params[:workspace_name], params[:function_name])
|
89
116
|
event = e.new(body, query_params, headers, context)
|
90
|
-
response =
|
117
|
+
response = FaaStRuby::Runner.new.call(params[:workspace_name], params[:function_name], event, rpc_args)
|
91
118
|
status response.status
|
92
119
|
headers response.headers
|
93
120
|
body response.body
|
data/faastruby.gemspec
CHANGED
@@ -20,7 +20,7 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.add_runtime_dependency 'sinatra', '~> 2.0'
|
21
21
|
spec.add_runtime_dependency 'sinatra-contrib', '~> 2.0'
|
22
22
|
spec.add_runtime_dependency 'puma', '~> 3.12'
|
23
|
-
spec.add_runtime_dependency 'faastruby-rpc', '~> 0.
|
23
|
+
spec.add_runtime_dependency 'faastruby-rpc', '~> 0.2.0'
|
24
24
|
|
25
25
|
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
26
26
|
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
data/lib/faastruby/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: faastruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paulo Arruda
|
@@ -142,14 +142,14 @@ dependencies:
|
|
142
142
|
requirements:
|
143
143
|
- - "~>"
|
144
144
|
- !ruby/object:Gem::Version
|
145
|
-
version: 0.
|
145
|
+
version: 0.2.0
|
146
146
|
type: :runtime
|
147
147
|
prerelease: false
|
148
148
|
version_requirements: !ruby/object:Gem::Requirement
|
149
149
|
requirements:
|
150
150
|
- - "~>"
|
151
151
|
- !ruby/object:Gem::Version
|
152
|
-
version: 0.
|
152
|
+
version: 0.2.0
|
153
153
|
- !ruby/object:Gem::Dependency
|
154
154
|
name: bundler
|
155
155
|
requirement: !ruby/object:Gem::Requirement
|