midori.rb 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 +7 -0
- data/.editorconfig +9 -0
- data/LICENSE +21 -0
- data/ext/midori/extconf.rb +4 -0
- data/ext/midori/websocket.c +32 -0
- data/lib/midori/api.rb +426 -0
- data/lib/midori/api_engine.rb +109 -0
- data/lib/midori/clean_room.rb +24 -0
- data/lib/midori/configure.rb +12 -0
- data/lib/midori/connection.rb +59 -0
- data/lib/midori/const.rb +118 -0
- data/lib/midori/core_ext/configurable.rb +33 -0
- data/lib/midori/core_ext/define_class.rb +29 -0
- data/lib/midori/core_ext/proc.rb +13 -0
- data/lib/midori/core_ext/string.rb +29 -0
- data/lib/midori/env.rb +18 -0
- data/lib/midori/eventsource.rb +20 -0
- data/lib/midori/exception.rb +22 -0
- data/lib/midori/logger.rb +15 -0
- data/lib/midori/middleware.rb +31 -0
- data/lib/midori/request.rb +115 -0
- data/lib/midori/response.rb +34 -0
- data/lib/midori/route.rb +20 -0
- data/lib/midori/runner.rb +63 -0
- data/lib/midori/sandbox.rb +46 -0
- data/lib/midori/server.rb +106 -0
- data/lib/midori/version.rb +5 -0
- data/lib/midori/websocket.rb +105 -0
- data/lib/midori.rb +37 -0
- data/midori.sublime-project +16 -0
- data/tutorial/README.md +11 -0
- data/tutorial/SUMMARY.md +28 -0
- data/tutorial/advanced/custom_extensions.md +0 -0
- data/tutorial/advanced/deploying_for_production.md +0 -0
- data/tutorial/advanced/error_handling.md +0 -0
- data/tutorial/advanced/rerl.md +0 -0
- data/tutorial/advanced/scaling_project.md +0 -0
- data/tutorial/advanced/unit_testing.md +0 -0
- data/tutorial/essentials/extensions.md +31 -0
- data/tutorial/essentials/getting_started.md +27 -0
- data/tutorial/essentials/installation.md +93 -0
- data/tutorial/essentials/middlewares.md +88 -0
- data/tutorial/essentials/request_handling.md +74 -0
- data/tutorial/essentials/routing.md +136 -0
- data/tutorial/essentials/runner.md +59 -0
- data/tutorial/meta/comparison_with_other_frameworks.md +0 -0
- data/tutorial/meta/join_the_midori_community.md +0 -0
- data/tutorial/meta/next_steps.md +0 -0
- metadata +155 -0
@@ -0,0 +1,136 @@
|
|
1
|
+
# Routing
|
2
|
+
|
3
|
+
## Basic Usage
|
4
|
+
|
5
|
+
Routes should be defined inside a class inherited from `Midori::API`. Midori doesn't support defining routes globally like sinatra to avoid scope pollution, which affects a lot in scaling project.
|
6
|
+
|
7
|
+
In midori, a route is an HTTP method with a URL-matching pattern. Each route is associated with a block:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class ExampleAPI < Midori::API
|
11
|
+
get '/' do
|
12
|
+
#.. show something ..
|
13
|
+
end
|
14
|
+
|
15
|
+
post '/' do
|
16
|
+
#.. create something ..
|
17
|
+
end
|
18
|
+
|
19
|
+
put '/' do
|
20
|
+
#.. replace something ..
|
21
|
+
end
|
22
|
+
|
23
|
+
delete '/' do
|
24
|
+
#.. annihilate something ..
|
25
|
+
end
|
26
|
+
|
27
|
+
options '/' do
|
28
|
+
#.. appease something ..
|
29
|
+
end
|
30
|
+
|
31
|
+
link '/' do
|
32
|
+
#.. affiliate something ..
|
33
|
+
end
|
34
|
+
|
35
|
+
unlink '/' do
|
36
|
+
#.. separate something ..
|
37
|
+
end
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
41
|
+
Routes are matched in the order they are defined. The first route that matches the request is invoked.
|
42
|
+
|
43
|
+
Midori not only supports the methods above, it supports almost every method provided in RFC standards. You could look it up in [API doc](http://www.rubydoc.info/gems/em-midori/Midori/API) for more details.
|
44
|
+
|
45
|
+
## Params
|
46
|
+
|
47
|
+
Routes patterns may include named parameters, accessible via the `request.params` hash:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
class ExampleAPI < Midori::API
|
51
|
+
get '/hello/:name' do
|
52
|
+
"Ohayou #{request.params['name']}"
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
Route patterns may also include splat (or wildcard) parameters, accessible via the `request.params['splat']` array:
|
58
|
+
|
59
|
+
```ruby
|
60
|
+
class ExampleAPI < Midori::API
|
61
|
+
get '/say/*/to/*' do
|
62
|
+
# matches /say/hello/to/world
|
63
|
+
request.params['splat'] # => ["hello", "world"]
|
64
|
+
end
|
65
|
+
|
66
|
+
get '/download/*.*' do
|
67
|
+
# matches /download/path/to/file.xml
|
68
|
+
request.params['splat'] # => ["path/to/file", "xml"]
|
69
|
+
end
|
70
|
+
end
|
71
|
+
```
|
72
|
+
|
73
|
+
Routes may also utilize query string:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class ExampleAPI < Midori::API
|
77
|
+
get '/posts' do
|
78
|
+
# matches "GET /posts?title=foo&author=bar"
|
79
|
+
request.query_string # => title=foo&author=bar
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
84
|
+
## WebSocket & EventSource
|
85
|
+
|
86
|
+
`WebSocket` connection uses `GET` method in HTTP protocol, but indeed, it behaves totally different from `GET` requests. You don't need to care about the protocol details. In midori, you could easily manage websocket connections easily.
|
87
|
+
|
88
|
+
Here's a chatroom example using websocket in midori:
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
CONNECTION_POOL = []
|
92
|
+
|
93
|
+
class ExampleAPI < Midori::API
|
94
|
+
websocket '/' do |ws|
|
95
|
+
ws.on :open do
|
96
|
+
ws.send 'Ohayo Midori'
|
97
|
+
CONNECTION_POOL << ws
|
98
|
+
end
|
99
|
+
|
100
|
+
ws.on :message do |msg|
|
101
|
+
CONNECTION_POOL.map do |client|
|
102
|
+
client.send msg
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
ws.on :close do
|
107
|
+
CONNECTION_POOL.delete(ws)
|
108
|
+
puts 'Oyasumi midori'
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
```
|
113
|
+
|
114
|
+
midori also supports `EventSource` connection as part of your route.
|
115
|
+
|
116
|
+
Here's a chatroom example using eventsource in midori:
|
117
|
+
|
118
|
+
```ruby
|
119
|
+
CONNECTION_POOL = []
|
120
|
+
|
121
|
+
class ExampleAPI < Midori::API
|
122
|
+
post '/pub' do
|
123
|
+
clients = CONNECTION_POOL.clone
|
124
|
+
CONNECTION_POOL.clear
|
125
|
+
# EventSource connection disconnects every time message sent, DO NOT reuse connection pool
|
126
|
+
clients.map do |client|
|
127
|
+
client.send request.body
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
eventsource '/sub' do |es|
|
132
|
+
CONNECTION_POOL << es
|
133
|
+
end
|
134
|
+
end
|
135
|
+
```
|
136
|
+
|
@@ -0,0 +1,59 @@
|
|
1
|
+
# Runner
|
2
|
+
|
3
|
+
## Introdution
|
4
|
+
|
5
|
+
`Runner` is the container of midori server. You could create, start, stop midori instance by `Runner`.
|
6
|
+
|
7
|
+
`Runner` use `Midori::Configure` as its configuration by default.
|
8
|
+
|
9
|
+
## Examples
|
10
|
+
|
11
|
+
Here're some examples for common usages
|
12
|
+
|
13
|
+
### Port Binding
|
14
|
+
|
15
|
+
Start midori instance with port `4567` instead of the default `8080`.
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
require 'midori'
|
19
|
+
class API < Midori::API
|
20
|
+
get '/' do
|
21
|
+
'Hello World'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
Midori::Configure.set :port, 4567
|
26
|
+
Midori::Runner.new(API).start
|
27
|
+
```
|
28
|
+
|
29
|
+
### Address Binding
|
30
|
+
|
31
|
+
Start midori instance listening to all IP addresses.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
require 'midori'
|
35
|
+
class API < Midori::API
|
36
|
+
get '/' do
|
37
|
+
'Hello World'
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
Midori::Configure.set :bind, '0.0.0.0'
|
42
|
+
Midori::Runner.new(API).start
|
43
|
+
```
|
44
|
+
|
45
|
+
### Stop Midori
|
46
|
+
|
47
|
+
Stop midori instance when specified route been called.
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require 'midori'
|
51
|
+
$runner = nil
|
52
|
+
class API < Midori::API
|
53
|
+
get '/stop' do
|
54
|
+
$runner.stop
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
$runner = Midori::Runner.new(API).start
|
59
|
+
```
|
File without changes
|
File without changes
|
File without changes
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: midori.rb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.3
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- HeckPsi Lab
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-10-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: murasaki
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.1'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.1'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: mustermann
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: midori_http_parser
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.6.1
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.6.1
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake-compiler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.0'
|
69
|
+
description: Midori is a Ruby Web Framework, providing high performance and proper
|
70
|
+
abstraction.
|
71
|
+
email:
|
72
|
+
- business@heckpsi.com
|
73
|
+
executables: []
|
74
|
+
extensions:
|
75
|
+
- ext/midori/extconf.rb
|
76
|
+
extra_rdoc_files: []
|
77
|
+
files:
|
78
|
+
- ".editorconfig"
|
79
|
+
- LICENSE
|
80
|
+
- ext/midori/extconf.rb
|
81
|
+
- ext/midori/websocket.c
|
82
|
+
- lib/midori.rb
|
83
|
+
- lib/midori/api.rb
|
84
|
+
- lib/midori/api_engine.rb
|
85
|
+
- lib/midori/clean_room.rb
|
86
|
+
- lib/midori/configure.rb
|
87
|
+
- lib/midori/connection.rb
|
88
|
+
- lib/midori/const.rb
|
89
|
+
- lib/midori/core_ext/configurable.rb
|
90
|
+
- lib/midori/core_ext/define_class.rb
|
91
|
+
- lib/midori/core_ext/proc.rb
|
92
|
+
- lib/midori/core_ext/string.rb
|
93
|
+
- lib/midori/env.rb
|
94
|
+
- lib/midori/eventsource.rb
|
95
|
+
- lib/midori/exception.rb
|
96
|
+
- lib/midori/logger.rb
|
97
|
+
- lib/midori/middleware.rb
|
98
|
+
- lib/midori/request.rb
|
99
|
+
- lib/midori/response.rb
|
100
|
+
- lib/midori/route.rb
|
101
|
+
- lib/midori/runner.rb
|
102
|
+
- lib/midori/sandbox.rb
|
103
|
+
- lib/midori/server.rb
|
104
|
+
- lib/midori/version.rb
|
105
|
+
- lib/midori/websocket.rb
|
106
|
+
- midori.sublime-project
|
107
|
+
- tutorial/README.md
|
108
|
+
- tutorial/SUMMARY.md
|
109
|
+
- tutorial/advanced/custom_extensions.md
|
110
|
+
- tutorial/advanced/deploying_for_production.md
|
111
|
+
- tutorial/advanced/error_handling.md
|
112
|
+
- tutorial/advanced/rerl.md
|
113
|
+
- tutorial/advanced/scaling_project.md
|
114
|
+
- tutorial/advanced/unit_testing.md
|
115
|
+
- tutorial/essentials/extensions.md
|
116
|
+
- tutorial/essentials/getting_started.md
|
117
|
+
- tutorial/essentials/installation.md
|
118
|
+
- tutorial/essentials/middlewares.md
|
119
|
+
- tutorial/essentials/request_handling.md
|
120
|
+
- tutorial/essentials/routing.md
|
121
|
+
- tutorial/essentials/runner.md
|
122
|
+
- tutorial/meta/comparison_with_other_frameworks.md
|
123
|
+
- tutorial/meta/join_the_midori_community.md
|
124
|
+
- tutorial/meta/next_steps.md
|
125
|
+
homepage: https://github.com/midori-rb/midori.rb
|
126
|
+
licenses:
|
127
|
+
- MIT
|
128
|
+
metadata:
|
129
|
+
issue_tracker: https://github.com/midori-rb/midori.rb/issues
|
130
|
+
post_install_message: "\n _____ _ \n| _ | | \n|
|
131
|
+
| | | |__ __ _ _ _ ___ \n| | | | '_ \\ / _` | | | |/ _ \\ \n\\ \\_/ / | |
|
132
|
+
| (_| | |_| | (_) |\n \\___/|_| |_|\\__,_|\\__, |\\___/ \n __/
|
133
|
+
| \n |___/ \n _ _ _ \n (_)
|
134
|
+
\ | | (_)\n _ __ ___ _ __| | ___ _ __ _ \n| '_ ` _ \\| |/ _` |/ _ \\|
|
135
|
+
'__| |\n| | | | | | | (_| | (_) | | | |\n|_| |_| |_|_|\\__,_|\\___/|_| |_|\n\n"
|
136
|
+
rdoc_options: []
|
137
|
+
require_paths:
|
138
|
+
- lib
|
139
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
140
|
+
requirements:
|
141
|
+
- - ">="
|
142
|
+
- !ruby/object:Gem::Version
|
143
|
+
version: 2.2.6
|
144
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
145
|
+
requirements:
|
146
|
+
- - ">="
|
147
|
+
- !ruby/object:Gem::Version
|
148
|
+
version: '0'
|
149
|
+
requirements: []
|
150
|
+
rubyforge_project:
|
151
|
+
rubygems_version: 2.6.10
|
152
|
+
signing_key:
|
153
|
+
specification_version: 4
|
154
|
+
summary: High performance ruby web framework
|
155
|
+
test_files: []
|