em-midori 0.2.4 → 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/lib/midori.rb +3 -0
- data/lib/midori/api_engine.rb +7 -2
- data/lib/midori/clean_room.rb +1 -1
- data/lib/midori/env.rb +18 -0
- data/lib/midori/extension/redic.rb +13 -2
- data/lib/midori/extension/sequel/postgres.rb +3 -0
- data/lib/midori/logger.rb +16 -0
- data/lib/midori/response.rb +8 -6
- data/lib/midori/runner.rb +1 -0
- data/lib/midori/sandbox.rb +2 -2
- data/lib/midori/server.rb +1 -1
- data/lib/midori/version.rb +1 -1
- data/tutorial/essentials/installation.md +1 -1
- data/tutorial/essentials/request_handling.md +13 -11
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '081416a49873c3fef7ed675ec2640bc3923284cc'
|
4
|
+
data.tar.gz: f2ab1575e5b6664572a2167f7dbdbced1b94a45a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 128bb0d384e2eb10978a03532c68db04d8bf668260a211ce6d94a46d5970bea2fa5e9bad94a9797329843a611e657618858d988313764031c5452cbf37b17458
|
7
|
+
data.tar.gz: 441c0ee3debb05818d91417533ef0a42f1ab609b3dd28a7b422ee28a9ce84b1b30208d6d20927fb8c6d6ccc90cc54b0e13bb7f881e260f9ace5f26c71d25d3de
|
data/lib/midori.rb
CHANGED
@@ -19,6 +19,7 @@ require_relative 'midori/core_ext/safe_require'
|
|
19
19
|
require_relative 'midori/version'
|
20
20
|
require_relative 'midori/const'
|
21
21
|
require_relative 'midori/exception'
|
22
|
+
require_relative 'midori/env'
|
22
23
|
require_relative 'midori/clean_room'
|
23
24
|
require_relative 'midori/server'
|
24
25
|
require_relative 'midori/connection'
|
@@ -33,3 +34,5 @@ require_relative 'midori/eventsource'
|
|
33
34
|
require_relative 'midori/middleware'
|
34
35
|
require_relative 'midori/configure'
|
35
36
|
require_relative 'midori/runner'
|
37
|
+
require_relative 'midori/logger'
|
38
|
+
|
data/lib/midori/api_engine.rb
CHANGED
@@ -52,12 +52,17 @@ class Midori::APIEngine
|
|
52
52
|
clean_room = Midori::CleanRoom.new(request)
|
53
53
|
if request.websocket?
|
54
54
|
# Send 101 Switching Protocol
|
55
|
-
connection.send_data Midori::Response.new(
|
55
|
+
connection.send_data Midori::Response.new(
|
56
|
+
status: 101,
|
57
|
+
header: Midori::APIEngine.websocket_header(request.header['Sec-WebSocket-Key']),
|
58
|
+
body: '')
|
56
59
|
connection.websocket.request = request
|
57
60
|
Midori::Sandbox.run(clean_room, route.function, connection.websocket)
|
58
61
|
return Midori::Response.new
|
59
62
|
elsif request.eventsource?
|
60
|
-
connection.send_data Midori::Response.new(
|
63
|
+
connection.send_data Midori::Response.new(
|
64
|
+
status: 200,
|
65
|
+
header: Midori::Const::EVENTSOURCE_HEADER)
|
61
66
|
Midori::Sandbox.run(clean_room, route.function, connection.eventsource)
|
62
67
|
return Midori::Response.new
|
63
68
|
else
|
data/lib/midori/clean_room.rb
CHANGED
@@ -19,6 +19,6 @@ class Midori::CleanRoom
|
|
19
19
|
# Generate response from variables inside +Midori::CleanRoom+
|
20
20
|
# @return [Midori::Response] midori response
|
21
21
|
def raw_response
|
22
|
-
Midori::Response.new(@status, @header, @body)
|
22
|
+
Midori::Response.new(status: @status, header: @header, body: @body)
|
23
23
|
end
|
24
24
|
end
|
data/lib/midori/env.rb
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Midori
|
2
|
+
# @return [String] midori environment
|
3
|
+
def self.env
|
4
|
+
ENV['MIDORI_ENV'] || 'development'
|
5
|
+
end
|
6
|
+
end
|
7
|
+
|
8
|
+
class String
|
9
|
+
# @return [TrueClass | FalseClass] if string is equal to production
|
10
|
+
def production?
|
11
|
+
self == 'production'
|
12
|
+
end
|
13
|
+
|
14
|
+
#@return [TrueClass | FalseClass] if string is equal to development
|
15
|
+
def development?
|
16
|
+
self == 'development'
|
17
|
+
end
|
18
|
+
end
|
@@ -17,7 +17,7 @@ module Hiredis
|
|
17
17
|
# Meta-programming hiredis for redis async extension
|
18
18
|
class Connection
|
19
19
|
# Do redis query
|
20
|
-
# @param [
|
20
|
+
# @param [Array] args equal to Hiredis write args
|
21
21
|
def query(args)
|
22
22
|
await(Promise.new do |resolve|
|
23
23
|
read_flag = false
|
@@ -65,22 +65,33 @@ end
|
|
65
65
|
|
66
66
|
require 'redic'
|
67
67
|
|
68
|
+
##
|
69
|
+
# Meta-programming Redic for redis async extension
|
68
70
|
class Redic
|
71
|
+
# Meta-programming Redic for redis async extension
|
69
72
|
class Client
|
73
|
+
# Connect redis, yield optional
|
70
74
|
def connect
|
71
75
|
establish_connection unless connected?
|
72
76
|
if block_given?
|
77
|
+
# Redic default yield
|
78
|
+
# :nocov:
|
73
79
|
@semaphore.synchronize do
|
74
80
|
yield
|
75
81
|
end
|
82
|
+
# :nocov:
|
76
83
|
end
|
77
84
|
end
|
78
|
-
|
85
|
+
|
86
|
+
# Call without thread lock
|
87
|
+
# @param [Array] args same params as Redic
|
79
88
|
def call(*args)
|
80
89
|
@connection.query(*args)
|
81
90
|
end
|
82
91
|
end
|
83
92
|
|
93
|
+
# Call without thread lock
|
94
|
+
# @param [Array] args same params as Redic
|
84
95
|
def call(*args)
|
85
96
|
@client.connect
|
86
97
|
@client.call(args)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module Midori
|
2
|
+
class << self
|
3
|
+
# Return current logger midori is using
|
4
|
+
# @return [Logger] the current logger midori is using
|
5
|
+
def logger
|
6
|
+
@logger = ::Logger.new(STDOUT) if @logger.nil?
|
7
|
+
@logger
|
8
|
+
end
|
9
|
+
|
10
|
+
# Return midori's logger
|
11
|
+
# @param [Logger] logger set midori logger
|
12
|
+
def logger=(logger)
|
13
|
+
@logger = logger
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/midori/response.rb
CHANGED
@@ -6,14 +6,16 @@
|
|
6
6
|
class Midori::Response
|
7
7
|
attr_accessor :status, :header, :body
|
8
8
|
|
9
|
-
# @param [
|
10
|
-
# @
|
11
|
-
# @
|
9
|
+
# @param [Hash] options HTTP response
|
10
|
+
# @option options [Integer] code HTTP response code
|
11
|
+
# @option options [Hash] header HTTP response header
|
12
|
+
# @option options [String] body HTTP response body
|
12
13
|
# Init a Response
|
13
|
-
def initialize(
|
14
|
+
def initialize(options = {})
|
15
|
+
code = options[:status] || 200
|
14
16
|
@status = Midori::Const::STATUS_CODE[code]
|
15
|
-
@header = header
|
16
|
-
@body = body
|
17
|
+
@header = options[:header] || Midori::Const::DEFAULT_HEADER.clone
|
18
|
+
@body = options[:body] || ''
|
17
19
|
end
|
18
20
|
|
19
21
|
# Generate header string from hash
|
data/lib/midori/runner.rb
CHANGED
@@ -11,6 +11,7 @@ class Midori::Runner
|
|
11
11
|
# @param [Class] configure inherited from [Midori::Configure]
|
12
12
|
def initialize(api, configure = Midori::Configure)
|
13
13
|
@logger = configure.logger
|
14
|
+
Midori.logger = configure.logger
|
14
15
|
@bind = configure.bind
|
15
16
|
@port = configure.port
|
16
17
|
@api = ((api.is_a?Midori::APIEngine) ? api : Midori::APIEngine.new(api, configure.route_type))
|
data/lib/midori/sandbox.rb
CHANGED
@@ -4,8 +4,8 @@ class Midori::Sandbox
|
|
4
4
|
class << self
|
5
5
|
def class_initialize
|
6
6
|
@handlers = Hash.new
|
7
|
-
@handlers[Midori::Exception::InternalError] = proc {|e| Midori::Response.new(500,
|
8
|
-
@handlers[Midori::Exception::NotFound] = proc {|_e| Midori::Response.new(404,
|
7
|
+
@handlers[Midori::Exception::InternalError] = proc {|e| Midori::Response.new(status: 500, body: "#{e.inspect} #{e.backtrace}")}
|
8
|
+
@handlers[Midori::Exception::NotFound] = proc {|_e| Midori::Response.new(status: 404, body: '404 Not Found')}
|
9
9
|
end
|
10
10
|
|
11
11
|
# Add a rule to Sandbox
|
data/lib/midori/server.rb
CHANGED
data/lib/midori/version.rb
CHANGED
@@ -92,7 +92,7 @@ Using bunlder could make dependency management much easier, which helps a lot in
|
|
92
92
|
|
93
93
|
You may probably meet problems with rubygems due to unstable overseas internet connection issues in China. The most popular way to solve it is to use mirror provided by [RubyChina](https://gems.ruby-china.org/) or [TUNA](https://mirror.tuna.tsinghua.edu.cn/help/rubygems/) as your gem source. This may have some side effects in development, because there's a few minutes' delay in receiving gem updates.
|
94
94
|
|
95
|
-
Alternatively, you could use proxy to connect the main repository directly to avoid the delay problem. But using proxy is a little too complex in production environment.
|
95
|
+
Alternatively, you could use proxy to connect to the main repository directly to avoid the delay problem. But using proxy is a little too complex in production environment.
|
96
96
|
|
97
97
|
Choose the solution better fits your requirements. Mixing the solutions like using proxy in development and using mirror in production is also a good choice.
|
98
98
|
|
@@ -40,7 +40,7 @@ class ExampleAPI < Midori::API
|
|
40
40
|
end
|
41
41
|
|
42
42
|
get '/case_1' do
|
43
|
-
status = 418
|
43
|
+
@status = 418
|
44
44
|
"I\'m a teapot"
|
45
45
|
# HTTP/1.1 418 I'm a teapot
|
46
46
|
# Server: Midori/1.0
|
@@ -51,21 +51,23 @@ class ExampleAPI < Midori::API
|
|
51
51
|
get '/case_2' do
|
52
52
|
header['Example-Header'] = 'Example-Value'
|
53
53
|
'Hello'
|
54
|
+
# HTTP/1.1 200 OK
|
55
|
+
# Server: Midori/1.0
|
56
|
+
# Example-Header: Example-Value
|
57
|
+
#
|
58
|
+
# Hello
|
54
59
|
end
|
55
|
-
# HTTP/1.1 200 OK
|
56
|
-
# Server: Midori/1.0
|
57
|
-
# Example-Header: Example-Value
|
58
|
-
#
|
59
|
-
# Hello
|
60
60
|
|
61
61
|
get '/case_3' do
|
62
|
-
status = 202
|
62
|
+
@status = 202
|
63
63
|
header['Example-Header'] = 'Example-Value'
|
64
|
-
Midori::Response.new(200, {}, 'Hello')
|
64
|
+
Midori::Response.new(status: 200, header: {}, body: 'Hello') # Overrides everything else
|
65
|
+
# HTTP/1.1 200 OK
|
66
|
+
# Server: Midori/1.0
|
67
|
+
# Example-Header: Example-Value
|
68
|
+
#
|
69
|
+
# Hello
|
65
70
|
end
|
66
|
-
# HTTP/1.1 200 OK
|
67
|
-
#
|
68
|
-
# Hello
|
69
71
|
end
|
70
72
|
```
|
71
73
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: em-midori
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- HeckPsi Lab
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-06-
|
11
|
+
date: 2017-06-08 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nio4r
|
@@ -77,6 +77,7 @@ files:
|
|
77
77
|
- lib/midori/core_ext/safe_require.rb
|
78
78
|
- lib/midori/core_ext/string.rb
|
79
79
|
- lib/midori/core_ext/timer.rb
|
80
|
+
- lib/midori/env.rb
|
80
81
|
- lib/midori/eventsource.rb
|
81
82
|
- lib/midori/exception.rb
|
82
83
|
- lib/midori/extension/file.rb
|
@@ -84,6 +85,7 @@ files:
|
|
84
85
|
- lib/midori/extension/redic.rb
|
85
86
|
- lib/midori/extension/sequel/mysql2.rb
|
86
87
|
- lib/midori/extension/sequel/postgres.rb
|
88
|
+
- lib/midori/logger.rb
|
87
89
|
- lib/midori/middleware.rb
|
88
90
|
- lib/midori/request.rb
|
89
91
|
- lib/midori/response.rb
|