sinarey 1.0.4 → 1.0.5
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/README.md +3 -0
- data/demo/app.rb +26 -3
- data/demo/app2.rb +1 -2
- data/demo/config.ru +4 -1
- data/demo/notfound.rb +1 -2
- data/lib/sinarey/base.rb +1194 -1219
- data/lib/sinarey/router.rb +120 -125
- data/lib/sinarey/version.rb +3 -3
- metadata +10 -10
data/lib/sinarey/router.rb
CHANGED
@@ -1,125 +1,120 @@
|
|
1
|
-
module Sinarey
|
2
|
-
class Router
|
3
|
-
def initialize(*args, &block)
|
4
|
-
@notfound_app = lambda { |env| [404, {}, ['404']] }
|
5
|
-
@apps = {}
|
6
|
-
@turbo_routes = {}
|
7
|
-
@routes = {}
|
8
|
-
instance_eval(&block) if block
|
9
|
-
build_routing_table
|
10
|
-
end
|
11
|
-
|
12
|
-
def call(env)
|
13
|
-
route = env["PATH_INFO"]
|
14
|
-
route.chop! if (char=route[-1]) and char=='/' # ignore last '/' char
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
end
|
27
|
-
|
28
|
-
def
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
#
|
74
|
-
@
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
end
|
123
|
-
|
124
|
-
end
|
125
|
-
end
|
1
|
+
module Sinarey
|
2
|
+
class Router
|
3
|
+
def initialize(*args, &block)
|
4
|
+
@notfound_app = lambda { |env| [404, {}, ['404']] }
|
5
|
+
@apps = {}
|
6
|
+
@turbo_routes = {}
|
7
|
+
@routes = {}
|
8
|
+
instance_eval(&block) if block
|
9
|
+
build_routing_table
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(env)
|
13
|
+
route = env["PATH_INFO"]
|
14
|
+
route.chop! if (char=route[-1]) and char=='/' # ignore last '/' char
|
15
|
+
|
16
|
+
if response = apps_route(env["REQUEST_METHOD"], route, env)
|
17
|
+
response
|
18
|
+
else
|
19
|
+
@notfound_app.call(env)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def mount(app)
|
24
|
+
app_id = @apps.size + 1
|
25
|
+
@apps[app_id] = app
|
26
|
+
end
|
27
|
+
|
28
|
+
def notfound(app)
|
29
|
+
@notfound_app = app
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def build_routing_table
|
35
|
+
@apps.each do |app_id, app|
|
36
|
+
regedit_turbo_routes(app_id, app)
|
37
|
+
regedit_basic_routes(app_id, app)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def regedit_turbo_routes(app_id, app)
|
42
|
+
return unless app.respond_to?(:turbo_routes)
|
43
|
+
app.turbo_routes.each do |verb, routes|
|
44
|
+
routes.each do |path, route|
|
45
|
+
route.tap do |block_id|
|
46
|
+
tmp = @turbo_routes[verb] ||= {}
|
47
|
+
tmp[path] = [block_id, app_id] unless tmp[path]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def regedit_basic_routes(app_id, app)
|
54
|
+
return unless app.respond_to?(:routes)
|
55
|
+
app.routes.each do |verb, routes|
|
56
|
+
routes.each do |pattern, keys, conditions, block_id|
|
57
|
+
(@routes[verb] ||= []) << [pattern, keys, conditions, block_id, app_id]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
case ENV["RACK_ENV"]
|
63
|
+
when 'development'
|
64
|
+
|
65
|
+
#development need support sinarey reloader.so here use dev logic.
|
66
|
+
def apps_route(verb, path, env)
|
67
|
+
|
68
|
+
#auto reload modified code
|
69
|
+
@apps.each do |index,app|
|
70
|
+
app.auto_reload if app.respond_to?(:auto_reload)
|
71
|
+
end
|
72
|
+
|
73
|
+
#rebuild route table
|
74
|
+
@turbo_routes = {}
|
75
|
+
@routes = {}
|
76
|
+
build_routing_table
|
77
|
+
|
78
|
+
if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
|
79
|
+
turbo_route.tap do |block_id,app_id|
|
80
|
+
env['sinarey.router'] = {type: :turbo, block_id: block_id}
|
81
|
+
status, headers, response = @apps[app_id].call(env)
|
82
|
+
return status, headers, response
|
83
|
+
end
|
84
|
+
elsif routes = @routes[verb]
|
85
|
+
routes.each do |pattern, keys, conditions, block_id, app_id|
|
86
|
+
if match = pattern.match(path)
|
87
|
+
env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
|
88
|
+
status, headers, response = @apps[app_id].call(env)
|
89
|
+
return status, headers, response
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
nil
|
94
|
+
end
|
95
|
+
|
96
|
+
else
|
97
|
+
|
98
|
+
def apps_route(verb, path, env)
|
99
|
+
if turbo_route = (turbo_routes = @turbo_routes[verb]) && turbo_routes[path]
|
100
|
+
turbo_route.tap do |block_id,app_id|
|
101
|
+
env['sinarey.router'] = {type: :turbo, block_id: block_id}
|
102
|
+
status, headers, response = @apps[app_id].call(env)
|
103
|
+
return status, headers, response
|
104
|
+
end
|
105
|
+
elsif routes = @routes[verb]
|
106
|
+
routes.each do |pattern, keys, conditions, block_id, app_id|
|
107
|
+
if match = pattern.match(path)
|
108
|
+
env['sinarey.router'] = {type: :normal, match: match, keys: keys, conditions: conditions, block_id: block_id}
|
109
|
+
status, headers, response = @apps[app_id].call(env)
|
110
|
+
return status, headers, response
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
nil
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
data/lib/sinarey/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
module Sinarey
|
2
|
-
VERSION = '1.0.
|
3
|
-
end
|
1
|
+
module Sinarey
|
2
|
+
VERSION = '1.0.5'
|
3
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinarey
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jeffrey
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2015-09-09 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sinatra
|
@@ -31,17 +31,17 @@ executables: []
|
|
31
31
|
extensions: []
|
32
32
|
extra_rdoc_files: []
|
33
33
|
files:
|
34
|
-
-
|
35
|
-
- lib/sinarey/version.rb
|
36
|
-
- lib/sinarey/base.rb
|
37
|
-
- lib/sinarey/router.rb
|
38
|
-
- lib/sinarey.rb
|
34
|
+
- README.md
|
39
35
|
- demo/app.rb
|
40
36
|
- demo/app2.rb
|
41
|
-
- demo/notfound.rb
|
42
37
|
- demo/config.ru
|
38
|
+
- demo/notfound.rb
|
39
|
+
- lib/sinarey.rb
|
40
|
+
- lib/sinarey/base.rb
|
41
|
+
- lib/sinarey/router.rb
|
42
|
+
- lib/sinarey/version.rb
|
43
|
+
- lib/sinatra/sinarey_reloader.rb
|
43
44
|
- sinarey.gemspec
|
44
|
-
- README.md
|
45
45
|
homepage: https://github.com/maymay25/sinarey
|
46
46
|
licenses:
|
47
47
|
- MIT
|
@@ -62,7 +62,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
62
62
|
version: '0'
|
63
63
|
requirements: []
|
64
64
|
rubyforge_project:
|
65
|
-
rubygems_version: 2.
|
65
|
+
rubygems_version: 2.4.5.1
|
66
66
|
signing_key:
|
67
67
|
specification_version: 4
|
68
68
|
summary: Sinarey, use for large rack project.
|