getto-roda 0.1.0
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 +7 -0
- data/.envrc +5 -0
- data/.gitignore +9 -0
- data/.gitlab-ci.yml +12 -0
- data/.travis.yml +12 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +36 -0
- data/LICENSE +21 -0
- data/README.md +279 -0
- data/Rakefile +10 -0
- data/getto-roda.gemspec +38 -0
- data/lib/getto/roda/config.rb +71 -0
- data/lib/getto/roda/decode.rb +32 -0
- data/lib/getto/roda/entry_point.rb +117 -0
- data/lib/getto/roda/http_error_helper.rb +21 -0
- data/lib/getto/roda/logger.rb +12 -0
- data/lib/getto/roda/time.rb +18 -0
- data/lib/getto/roda/version.rb +5 -0
- metadata +151 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 46d5dee3d87cd51411b8ed9a907d3e19a7321bbd155e16e0e2533122826a022d
|
4
|
+
data.tar.gz: c169513f1858666b926327ff6ccd78fc4c0a2f6605aa2574300e89a36f6a8b7a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: af36533169e0b32e263d217b3d7d863b6382c365d89f93c3d2c99e7190b4c89702f6bb40483dd9c98fea618d4ab59d0fbe64dcd0692c5ca4fa44f2309b08d55d
|
7
|
+
data.tar.gz: 18fc7e6ac76dd403a1872cab93dd467cf6202bc50c2fa7bf08d008faaa773865dad307a8cc33a42ec5d100a9a58d7c4e34230c11b667ebc2f99a3d716839216b
|
data/.envrc
ADDED
data/.gitignore
ADDED
data/.gitlab-ci.yml
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
sudo: false
|
2
|
+
language: ruby
|
3
|
+
rvm:
|
4
|
+
- 2.5.1
|
5
|
+
before_install: gem install bundler -v 1.16.2
|
6
|
+
deploy:
|
7
|
+
provider: rubygems
|
8
|
+
gem: getto-roda
|
9
|
+
on:
|
10
|
+
tags: true
|
11
|
+
api_key:
|
12
|
+
secure: "kMzCWXDXI953tD0/SVlCRf393TvaLdVuoQTPP4OZAk7Ro+2w2Nzgjhck/z/EiWLOi4T0GNI2nLSOXEj4c1OuoA/CNsmLuWWkMCVRLWf2TwapDErb+7c6jtPLG/KKhq832E+QrgIyA6gNSjupl3Bahkz+X3RzYbuuuWOwwjq+RBW0Op1fnyKUNXaPb3xOgiM+iebE57pjzyp/tey5YFcQllZbNanpJLwY/xFMlJAePDAN42qdGbT2FaKyK8YxI/zzs6zqWconFwls2R1Y5WxghQRrNaSJqcHd3/j+bSF7Hk3Da9l+N/DN5pClyOSTLRUWRhpeY0dHNMlHHjM+isG0FlNVbmKdIQFaqWOq6f+iAlsbVGPWjVD1sTeRrXP7EF1Xu8VKEMkmL1wbuJf2CKJTODvpeGCQaxAt1dw1klPTHdjI2yg8F2Mhwxv1zG8nG+9kAzSmz4Iwx5x0n4d+6yEWn9McxOI2criokY0/hV53rlSCmVWBB0oIc7eqw69BVWsa/uFqdxp4bsW4rIqd3eEuidLJCbM1HnRacOgDTnH8Z876UVGPNCWAJ2vTsQS6bGIJzACbN3KenbTPMvWZkGnB0riRUX+DcRQYwDL+FmsXhbFTNHmNyERlrm5l41wB1uL2h0GB83xvRb/HYFXDb6sD1i/nxobbwjvTHFtCt/07t/0="
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
getto-roda (0.1.0)
|
5
|
+
rack (~> 2.0)
|
6
|
+
tzinfo (~> 1.1)
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
docile (1.3.1)
|
12
|
+
json (2.1.0)
|
13
|
+
minitest (5.11.3)
|
14
|
+
rack (2.0.5)
|
15
|
+
rake (10.5.0)
|
16
|
+
simplecov (0.16.1)
|
17
|
+
docile (~> 1.1)
|
18
|
+
json (>= 1.8, < 3)
|
19
|
+
simplecov-html (~> 0.10.0)
|
20
|
+
simplecov-html (0.10.2)
|
21
|
+
thread_safe (0.3.6)
|
22
|
+
tzinfo (1.2.5)
|
23
|
+
thread_safe (~> 0.1)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
bundler (~> 1.16)
|
30
|
+
getto-roda!
|
31
|
+
minitest (~> 5.0)
|
32
|
+
rake (~> 10.0)
|
33
|
+
simplecov (~> 0.16)
|
34
|
+
|
35
|
+
BUNDLED WITH
|
36
|
+
1.16.2
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 shun@getto.systems
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,279 @@
|
|
1
|
+
# getto-roda
|
2
|
+
|
3
|
+
[rubygems: getto-roda](https://rubygems.org/gems/getto-roda)
|
4
|
+
|
5
|
+
The web app entry point helper for [Roda](https://roda.jeremyevans.net/index.html)
|
6
|
+
|
7
|
+
|
8
|
+
###### Table of Contents
|
9
|
+
|
10
|
+
- [Requirements](#Requirements)
|
11
|
+
- [Usage](#Usage)
|
12
|
+
- [License](#License)
|
13
|
+
|
14
|
+
<a id="Requirements"></a>
|
15
|
+
## Requirements
|
16
|
+
|
17
|
+
- developed on ruby: 2.5.1
|
18
|
+
- [Roda](https://roda.jeremyevans.net/index.html)
|
19
|
+
|
20
|
+
|
21
|
+
<a id="Usage"></a>
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
### entry point
|
25
|
+
|
26
|
+
```ruby
|
27
|
+
require "getto/roda/entry_point"
|
28
|
+
|
29
|
+
require "logger"
|
30
|
+
require "exception_notifier"
|
31
|
+
require "jwt"
|
32
|
+
require "sequel"
|
33
|
+
|
34
|
+
class AppError < RuntimeError
|
35
|
+
extend Getto::Roda::HttpErrorHelper
|
36
|
+
|
37
|
+
error 401, :unauthorized
|
38
|
+
... # other errors
|
39
|
+
end
|
40
|
+
|
41
|
+
module MyApp
|
42
|
+
class EntryPoint < Getto::Roda::EntryPoint
|
43
|
+
|
44
|
+
# Getto::Roda::EntryPoint
|
45
|
+
# def initialize(
|
46
|
+
# error:, time_zone:,
|
47
|
+
# app:, request:,
|
48
|
+
# config:, params:,
|
49
|
+
# request_logger:, exception_notifier:)
|
50
|
+
# ...
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# attr_reader(
|
54
|
+
# :app, # => Roda app object
|
55
|
+
# :request, # => Roda request object
|
56
|
+
# :error,
|
57
|
+
# :config,
|
58
|
+
# :params,
|
59
|
+
# :account,
|
60
|
+
# :exception_notifier,
|
61
|
+
# :time, # => Getto::Time
|
62
|
+
# :logger, # => Getto::Logger
|
63
|
+
# )
|
64
|
+
|
65
|
+
def initialize(config:, **args)
|
66
|
+
super(
|
67
|
+
error: AppError,
|
68
|
+
config: config, # Getto::Roda::Config
|
69
|
+
time_zone: config.time.zone, # TZInfo::TimeZone
|
70
|
+
|
71
|
+
request_logger: ::Logger.new(STDOUT),
|
72
|
+
exception_notifier: ::ExceptionNotifier,
|
73
|
+
|
74
|
+
**args,
|
75
|
+
)
|
76
|
+
end
|
77
|
+
|
78
|
+
private
|
79
|
+
|
80
|
+
def init_controller(controller)
|
81
|
+
# initialize controller class
|
82
|
+
controller.new(
|
83
|
+
error: error,
|
84
|
+
time: time,
|
85
|
+
logger: logger,
|
86
|
+
params: params,
|
87
|
+
config: config,
|
88
|
+
account: account, # <= detect_account
|
89
|
+
sequel: ::Sequel.connect(config.db),
|
90
|
+
)
|
91
|
+
end
|
92
|
+
|
93
|
+
def detect_account
|
94
|
+
::JWT.decode(
|
95
|
+
app.env["HTTP_" + config.authorized.header],
|
96
|
+
config.authorized.secret,
|
97
|
+
true,
|
98
|
+
{
|
99
|
+
algorithm: config.authorized.algorithm,
|
100
|
+
}
|
101
|
+
).first.transform_keys(&:to_sym)
|
102
|
+
rescue ::JWT::DecodeError => e
|
103
|
+
error.invalid_token! e.message
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
|
109
|
+
module MyApp
|
110
|
+
class Main < Roda
|
111
|
+
config = MyApp.config # Getto::Roda::Config
|
112
|
+
|
113
|
+
launch = ->(app,r, path, **params){
|
114
|
+
require "my_app/controller/#{path}"
|
115
|
+
EntryPoint.new(app: app, request: r, config: config, params: params)
|
116
|
+
.handle(
|
117
|
+
path.split("/")
|
118
|
+
.map{|name|
|
119
|
+
name.gsub(%r{_.}){|c| c[1].upcase}
|
120
|
+
.sub(%r{\A.}){|c| c.upcase}
|
121
|
+
.to_sym
|
122
|
+
}
|
123
|
+
.inject(Controller){|klass,name| klass.const_get(name)}
|
124
|
+
).to_json
|
125
|
+
}
|
126
|
+
|
127
|
+
route do |r|
|
128
|
+
r.root { VERSION }
|
129
|
+
|
130
|
+
r.get("information"){ launch[self,r, "information"] }
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
```
|
135
|
+
|
136
|
+
### config definition
|
137
|
+
|
138
|
+
```ruby
|
139
|
+
require "getto/roda/config"
|
140
|
+
|
141
|
+
config =
|
142
|
+
Getto::Roda::Config.configure(
|
143
|
+
name: String,
|
144
|
+
key: {
|
145
|
+
roles: Array,
|
146
|
+
config: Hash,
|
147
|
+
},
|
148
|
+
) do |c|
|
149
|
+
c.name = "Name"
|
150
|
+
c.group :key do |sub|
|
151
|
+
sub.roles = [:user, :system]
|
152
|
+
sub.config = {
|
153
|
+
path: :path,
|
154
|
+
url: :url,
|
155
|
+
}
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
config.name # => "Name"
|
160
|
+
config.key.roles # => [:user, :system]
|
161
|
+
config.key.config # => { path: :path, url: :url }
|
162
|
+
```
|
163
|
+
|
164
|
+
### decode params
|
165
|
+
|
166
|
+
```ruby
|
167
|
+
require "getto/roda/decode"
|
168
|
+
|
169
|
+
# decode post body with content-type info
|
170
|
+
params = Getto::Roda::Decode::Post.new("application/json", '{"key":"value"}').to_h
|
171
|
+
# => {"key" => "value"}
|
172
|
+
|
173
|
+
# nil if content-type is not "application/json"
|
174
|
+
params = Getto::Roda::Decode::Post.new("text/plain", '{"key":"value"}').to_h
|
175
|
+
# => nil
|
176
|
+
|
177
|
+
# decode get query string
|
178
|
+
params = Getto::Roda::Decode::Get.new("key=value").to_h
|
179
|
+
# => {"key" => "value"}
|
180
|
+
```
|
181
|
+
|
182
|
+
### http error
|
183
|
+
|
184
|
+
```ruby
|
185
|
+
require "getto/roda/http_error_helper"
|
186
|
+
|
187
|
+
class AppError < RuntimeError
|
188
|
+
extend Getto::Roda::HttpErrorHelper
|
189
|
+
|
190
|
+
error 401, :unauthorized
|
191
|
+
end
|
192
|
+
|
193
|
+
begin
|
194
|
+
AppError.unauthorized!
|
195
|
+
rescue AppError => e
|
196
|
+
e.status # => 401
|
197
|
+
e.message # => "unauthorized"
|
198
|
+
end
|
199
|
+
|
200
|
+
begin
|
201
|
+
AppError.unauthorized!("login required", "login_id or password not valid")
|
202
|
+
rescue AppError => e
|
203
|
+
e.status # => 401
|
204
|
+
e.message # => "unauthorized: login required: login_id or password not valid"
|
205
|
+
end
|
206
|
+
```
|
207
|
+
|
208
|
+
### logger
|
209
|
+
|
210
|
+
```ruby
|
211
|
+
require "getto/roda/logger"
|
212
|
+
|
213
|
+
logger = Getto::Roda::Logger.new
|
214
|
+
|
215
|
+
logger.log(
|
216
|
+
name: "value",
|
217
|
+
)
|
218
|
+
logger.log("message")
|
219
|
+
logger.log([
|
220
|
+
"value1",
|
221
|
+
"value2",
|
222
|
+
"value3",
|
223
|
+
])
|
224
|
+
|
225
|
+
logger.data
|
226
|
+
# => [
|
227
|
+
# {
|
228
|
+
# name: "value",
|
229
|
+
# },
|
230
|
+
# "message",
|
231
|
+
# [
|
232
|
+
# "value1",
|
233
|
+
# "value2",
|
234
|
+
# "value3",
|
235
|
+
# ],
|
236
|
+
# ]
|
237
|
+
```
|
238
|
+
|
239
|
+
### time
|
240
|
+
|
241
|
+
```ruby
|
242
|
+
require "getto/roda/time"
|
243
|
+
|
244
|
+
now = Time.now
|
245
|
+
time_zone = TZInfo::Timezone.get("Asia/Tokyo")
|
246
|
+
|
247
|
+
Getto::Roda::Time.new(now: now, time_zone: time_zone).now # => now, not Time.now
|
248
|
+
|
249
|
+
Getto::Roda::Time.new(now: now, time_zone: time_zone).parse("2018-10-10 10:09:30")
|
250
|
+
# => Time.parse("2018-10-10 01:09:30") : parse with time-zone
|
251
|
+
```
|
252
|
+
|
253
|
+
## Install
|
254
|
+
|
255
|
+
Add this line to your application's Gemfile:
|
256
|
+
|
257
|
+
```ruby
|
258
|
+
gem 'getto-roda'
|
259
|
+
```
|
260
|
+
|
261
|
+
And then execute:
|
262
|
+
|
263
|
+
```
|
264
|
+
$ bundle
|
265
|
+
```
|
266
|
+
|
267
|
+
Or install it yourself as:
|
268
|
+
|
269
|
+
```
|
270
|
+
$ gem install getto-roda
|
271
|
+
```
|
272
|
+
|
273
|
+
|
274
|
+
<a id="License"></a>
|
275
|
+
## License
|
276
|
+
|
277
|
+
getto/roda is licensed under the [MIT](LICENSE) license.
|
278
|
+
|
279
|
+
Copyright © since 2018 shun@getto.systems
|
data/Rakefile
ADDED
data/getto-roda.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "getto/roda/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.required_ruby_version = ">= 2.5.1"
|
7
|
+
|
8
|
+
spec.name = "getto-roda"
|
9
|
+
spec.version = Getto::Roda::VERSION
|
10
|
+
spec.authors = ["shun@getto.systems"]
|
11
|
+
spec.email = ["shun@getto.systems"]
|
12
|
+
|
13
|
+
spec.summary = %q{Roda helper}
|
14
|
+
spec.description = %q{The web app entry point helper}
|
15
|
+
spec.homepage = "https://github.com/getto-systems/getto-roda"
|
16
|
+
spec.license = "MIT"
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = "exe"
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ["lib"]
|
26
|
+
|
27
|
+
|
28
|
+
spec.extra_rdoc_files = ['README.md', 'LICENSE']
|
29
|
+
spec.rdoc_options = %w[--title Getto::Roda --main README.md]
|
30
|
+
|
31
|
+
spec.add_runtime_dependency "rack", "~> 2.0"
|
32
|
+
spec.add_runtime_dependency "tzinfo", "~> 1.1"
|
33
|
+
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "minitest", "~> 5.0"
|
37
|
+
spec.add_development_dependency "simplecov", "~> 0.16"
|
38
|
+
end
|
@@ -0,0 +1,71 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
module Getto
|
4
|
+
module Roda
|
5
|
+
class Config
|
6
|
+
def self.configure(schema)
|
7
|
+
c = Struct.new
|
8
|
+
yield c
|
9
|
+
c.freeze
|
10
|
+
c.tap do
|
11
|
+
Checker.new(schema).validate!(c)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
class Struct < OpenStruct
|
16
|
+
def group(name)
|
17
|
+
self[name] ||= Struct.new
|
18
|
+
yield self[name]
|
19
|
+
end
|
20
|
+
|
21
|
+
def freeze
|
22
|
+
each_pair.each do |_,value|
|
23
|
+
value.freeze
|
24
|
+
end
|
25
|
+
super
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
class Checker
|
30
|
+
def initialize(schema)
|
31
|
+
@schema = schema
|
32
|
+
end
|
33
|
+
|
34
|
+
def validate!(config)
|
35
|
+
validate_config!([], @schema, config)
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def validate_config!(path, schema, config)
|
41
|
+
unless config
|
42
|
+
raise "#{path.join("/")} is nil"
|
43
|
+
end
|
44
|
+
|
45
|
+
schema.each do |key,spec|
|
46
|
+
case spec
|
47
|
+
when ::Class
|
48
|
+
unless config[key].is_a?(spec)
|
49
|
+
raise "#{full_path(path,key)} is not a #{spec}"
|
50
|
+
end
|
51
|
+
when ::Array
|
52
|
+
unless spec.include?(config[key])
|
53
|
+
raise "#{full_path(path,key)} is not in [#{spec.join(",")}]"
|
54
|
+
end
|
55
|
+
when ::Hash
|
56
|
+
validate_config!([*path,key], spec, config[key])
|
57
|
+
else
|
58
|
+
# :nocov:
|
59
|
+
raise "invalid schema: #{full_path(path,key)} : #{spec}"
|
60
|
+
# :nocov:
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def full_path(path,key)
|
66
|
+
[*path, key].join("/")
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
require "rack"
|
2
|
+
require "json"
|
3
|
+
|
4
|
+
module Getto
|
5
|
+
module Roda
|
6
|
+
module Decode
|
7
|
+
class Post
|
8
|
+
def initialize(content_type, body)
|
9
|
+
@content_type = content_type
|
10
|
+
@body = body
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_h
|
14
|
+
return unless @content_type == "application/json"
|
15
|
+
::JSON.parse @body
|
16
|
+
rescue ::JSON::ParserError
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class Get
|
22
|
+
def initialize(query_string)
|
23
|
+
@query_string = query_string
|
24
|
+
end
|
25
|
+
|
26
|
+
def to_h
|
27
|
+
::Rack::Utils.parse_nested_query(@query_string)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
require "getto/roda/time"
|
2
|
+
require "getto/roda/decode"
|
3
|
+
require "getto/roda/logger"
|
4
|
+
|
5
|
+
module Getto
|
6
|
+
module Roda
|
7
|
+
class EntryPoint
|
8
|
+
def initialize(
|
9
|
+
error:, time_zone:,
|
10
|
+
app:, request:,
|
11
|
+
config:, params:,
|
12
|
+
request_logger:, exception_notifier:)
|
13
|
+
|
14
|
+
@error = error
|
15
|
+
@time = Getto::Roda::Time.new(now: ::Time.now, time_zone: time_zone)
|
16
|
+
|
17
|
+
@app = app
|
18
|
+
@request = request
|
19
|
+
@config = config
|
20
|
+
@params = params
|
21
|
+
|
22
|
+
@request_logger = request_logger
|
23
|
+
@exception_notifier = exception_notifier
|
24
|
+
|
25
|
+
@logger = Getto::Roda::Logger.new
|
26
|
+
end
|
27
|
+
|
28
|
+
attr_reader :error, :time,
|
29
|
+
:app, :request,
|
30
|
+
:config, :params, :account,
|
31
|
+
:logger, :exception_notifier
|
32
|
+
|
33
|
+
|
34
|
+
def handle(controller)
|
35
|
+
params.merge! parse_params.to_h
|
36
|
+
@account = detect_account
|
37
|
+
|
38
|
+
init_controller(controller).action
|
39
|
+
rescue error => e
|
40
|
+
error! e
|
41
|
+
|
42
|
+
if e.status >= 500
|
43
|
+
exception_notifier.notify(e, data: request_data)
|
44
|
+
end
|
45
|
+
|
46
|
+
app.response.status = e.status
|
47
|
+
|
48
|
+
{message: e.message.split(":").first}
|
49
|
+
rescue Exception => e
|
50
|
+
error! e
|
51
|
+
exception_notifier.notify_exception(e, data: request_data)
|
52
|
+
raise e
|
53
|
+
ensure
|
54
|
+
if @error_data
|
55
|
+
@request_logger.error(name: :handle){ request_data }
|
56
|
+
else
|
57
|
+
@request_logger.info(name: :handle){ request_data }
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def detect_account
|
64
|
+
error.not_implemented!
|
65
|
+
end
|
66
|
+
|
67
|
+
def init_controller(controller)
|
68
|
+
error.not_implemented!
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
def parse_params
|
73
|
+
if app.env["REQUEST_METHOD"].upcase == "GET"
|
74
|
+
parse_query_string!
|
75
|
+
else
|
76
|
+
parse_request_body!
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
def parse_request_body!
|
81
|
+
Getto::Roda::Decode::Post.new(app.env["CONTENT_TYPE"], request.body.read)
|
82
|
+
.to_h or error.invalid_params!
|
83
|
+
end
|
84
|
+
|
85
|
+
def parse_query_string!
|
86
|
+
Getto::Roda::Decode::Get.new(app.env["QUERY_STRING"])
|
87
|
+
.to_h or error.invalid_params!
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
def error!(e)
|
92
|
+
@error_data = {
|
93
|
+
error: e.class.to_s,
|
94
|
+
message: e.message,
|
95
|
+
}
|
96
|
+
end
|
97
|
+
|
98
|
+
def request_data
|
99
|
+
{
|
100
|
+
start: time.now.iso8601,
|
101
|
+
remote: app.env['HTTP_X_FORWARDED_FOR'] || app.env["REMOTE_ADDR"] || "-",
|
102
|
+
account: account,
|
103
|
+
method: app.env["REQUEST_METHOD"],
|
104
|
+
uri: "#{app.env["PATH_INFO"]}#{app.env["QUERY_STRING"].tap{|s| break s.empty? ? s : "?#{s}" }}",
|
105
|
+
elapsed: "%.3f" % (::Time.now.to_f - time.now.to_f),
|
106
|
+
app: @logger.data,
|
107
|
+
}.tap{|data|
|
108
|
+
if @error_data
|
109
|
+
data[:error] = @error_data
|
110
|
+
else
|
111
|
+
data[:result] = :ok
|
112
|
+
end
|
113
|
+
}
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module Getto
|
2
|
+
module Roda
|
3
|
+
module HttpErrorHelper
|
4
|
+
def error(code, name, &block)
|
5
|
+
error_class_name = :"E#{code}"
|
6
|
+
unless self.const_defined? error_class_name
|
7
|
+
self.const_set(error_class_name, Class.new(self).tap{|klass|
|
8
|
+
klass.class_eval{ define_method(:status){code} }
|
9
|
+
})
|
10
|
+
end
|
11
|
+
|
12
|
+
self.singleton_class.class_eval do
|
13
|
+
define_method(:"#{name}!") do |*args|
|
14
|
+
args.unshift ":" unless args.empty?
|
15
|
+
raise self.const_get(error_class_name), "#{name}#{args.join " "}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
require "time"
|
2
|
+
|
3
|
+
module Getto
|
4
|
+
module Roda
|
5
|
+
class Time
|
6
|
+
def initialize(now:, time_zone:)
|
7
|
+
@time_zone = time_zone
|
8
|
+
@now = now
|
9
|
+
end
|
10
|
+
|
11
|
+
attr_reader :now
|
12
|
+
|
13
|
+
def parse(str)
|
14
|
+
@time_zone.local_to_utc ::Time.parse(str)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
metadata
ADDED
@@ -0,0 +1,151 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: getto-roda
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- shun@getto.systems
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-10-28 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: tzinfo
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.16'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.16'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: minitest
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '5.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '5.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.16'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.16'
|
97
|
+
description: The web app entry point helper
|
98
|
+
email:
|
99
|
+
- shun@getto.systems
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files:
|
103
|
+
- README.md
|
104
|
+
- LICENSE
|
105
|
+
files:
|
106
|
+
- ".envrc"
|
107
|
+
- ".gitignore"
|
108
|
+
- ".gitlab-ci.yml"
|
109
|
+
- ".travis.yml"
|
110
|
+
- Gemfile
|
111
|
+
- Gemfile.lock
|
112
|
+
- LICENSE
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- getto-roda.gemspec
|
116
|
+
- lib/getto/roda/config.rb
|
117
|
+
- lib/getto/roda/decode.rb
|
118
|
+
- lib/getto/roda/entry_point.rb
|
119
|
+
- lib/getto/roda/http_error_helper.rb
|
120
|
+
- lib/getto/roda/logger.rb
|
121
|
+
- lib/getto/roda/time.rb
|
122
|
+
- lib/getto/roda/version.rb
|
123
|
+
homepage: https://github.com/getto-systems/getto-roda
|
124
|
+
licenses:
|
125
|
+
- MIT
|
126
|
+
metadata: {}
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options:
|
129
|
+
- "--title"
|
130
|
+
- Getto::Roda
|
131
|
+
- "--main"
|
132
|
+
- README.md
|
133
|
+
require_paths:
|
134
|
+
- lib
|
135
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
136
|
+
requirements:
|
137
|
+
- - ">="
|
138
|
+
- !ruby/object:Gem::Version
|
139
|
+
version: 2.5.1
|
140
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
requirements: []
|
146
|
+
rubyforge_project:
|
147
|
+
rubygems_version: 2.7.7
|
148
|
+
signing_key:
|
149
|
+
specification_version: 4
|
150
|
+
summary: Roda helper
|
151
|
+
test_files: []
|