lux-fw 0.2.1 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.version +1 -1
- data/lib/common/class_callbacks.rb +18 -16
- data/lib/lux/application/application.rb +23 -11
- data/lib/lux/application/lib/render.rb +17 -7
- data/lib/lux/current/lib/static_file.rb +15 -13
- data/lib/lux/lux.rb +6 -5
- data/lib/lux/response/response.rb +6 -27
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f17bf48ba9c67072d7a9cba132b0c8c29fb25f79bc424e39f0d4bc39b16981e
|
4
|
+
data.tar.gz: ea0a19540ee7c21cebea07b6ea6ad27e967a448d1865a51097b7fe22f305e244
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a286aecb1572cb1b7929449c914f4bed7faae1b199c63e663300eae36a5a58d40cb3c8480a59974d8a74a1dbc2a3a2269130f070bd2e3eafc6cf5dc64e4729f0
|
7
|
+
data.tar.gz: 4490adeaa98e7ac83bf413511baa383f152ded339ee0fa8c97d29cab141653da1eea76059f210296b4c3e465259e484c68c20bd7d172fe94420c4c9863b688cd
|
data/.version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
@@ -21,34 +21,36 @@ module ClassCallbacks
|
|
21
21
|
@@pointers = {}
|
22
22
|
|
23
23
|
def add klass, unique_id, action, method
|
24
|
-
|
24
|
+
klass = klass.to_s
|
25
|
+
key = Crypt.sha1(unique_id)
|
26
|
+
|
25
27
|
@@pointers[key] = method
|
26
|
-
|
27
|
-
@@methods[klass
|
28
|
-
@@methods[klass
|
28
|
+
|
29
|
+
@@methods[klass] ||= {}
|
30
|
+
@@methods[klass][action] ||= []
|
31
|
+
@@methods[klass][action].tap { |it| it.push(key) unless it.include?(key) }
|
29
32
|
end
|
30
33
|
|
31
34
|
def execute instance_object, action, object=nil
|
32
35
|
object ? instance_object.send(action, object) : instance_object.send(action)
|
33
36
|
|
34
|
-
# execute for self and
|
35
|
-
|
36
|
-
next if
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
37
|
+
# execute for self and parents
|
38
|
+
instance_object.class.ancestors.reverse.map(&:to_s).each do |name|
|
39
|
+
next if name == 'Object'
|
40
|
+
next unless actions = @@methods.dig(name, action)
|
41
|
+
|
42
|
+
for el in actions.map { |o| @@pointers[o] }
|
43
|
+
if el.kind_of?(Symbol)
|
44
|
+
object ? instance_object.send(el, object) : instance_object.send(el)
|
45
|
+
else
|
46
|
+
object ? instance_object.instance_exec(object, &el) : instance_object.instance_exec(&el)
|
45
47
|
end
|
46
48
|
end
|
47
49
|
end
|
48
50
|
end
|
49
51
|
|
50
52
|
def define(klass, *args)
|
51
|
-
|
53
|
+
args.each do |action|
|
52
54
|
klass.class_eval %[
|
53
55
|
def #{action}(duck=nil)
|
54
56
|
end
|
@@ -8,7 +8,8 @@ class Lux::Application
|
|
8
8
|
|
9
9
|
###
|
10
10
|
|
11
|
-
def initialize
|
11
|
+
def initialize current
|
12
|
+
@current = current
|
12
13
|
@route_test = Lux::Application::RouteTest.new self
|
13
14
|
end
|
14
15
|
|
@@ -17,31 +18,32 @@ class Lux::Application
|
|
17
18
|
end
|
18
19
|
|
19
20
|
def body?
|
20
|
-
|
21
|
+
@current.response.body ? true : false
|
21
22
|
end
|
22
23
|
|
23
24
|
def body data
|
24
|
-
|
25
|
+
@current.response.body data
|
25
26
|
end
|
26
27
|
|
27
28
|
def current
|
28
|
-
Lux.current
|
29
|
+
# Lux.current
|
30
|
+
@current
|
29
31
|
end
|
30
32
|
|
31
33
|
def nav
|
32
|
-
|
34
|
+
@current.nav
|
33
35
|
end
|
34
36
|
|
35
37
|
def params
|
36
|
-
|
38
|
+
@current.params
|
37
39
|
end
|
38
40
|
|
39
41
|
def redirect where, msgs={}
|
40
|
-
|
42
|
+
@current.redirect where, msgs
|
41
43
|
end
|
42
44
|
|
43
45
|
def request
|
44
|
-
|
46
|
+
@current.request
|
45
47
|
end
|
46
48
|
|
47
49
|
# gets only root
|
@@ -55,15 +57,15 @@ class Lux::Application
|
|
55
57
|
end
|
56
58
|
|
57
59
|
def done?
|
58
|
-
throw :done if
|
60
|
+
throw :done if @current.response.body
|
59
61
|
end
|
60
62
|
|
61
63
|
def get?
|
62
|
-
|
64
|
+
@current.request.request_method == 'GET'
|
63
65
|
end
|
64
66
|
|
65
67
|
def post?
|
66
|
-
|
68
|
+
@current.request.request_method == 'POST'
|
67
69
|
end
|
68
70
|
|
69
71
|
def plug name, &block
|
@@ -179,4 +181,14 @@ class Lux::Application
|
|
179
181
|
end
|
180
182
|
end
|
181
183
|
|
184
|
+
def render
|
185
|
+
Lux.log "\n#{current.request.request_method.white} #{current.request.path.white}"
|
186
|
+
|
187
|
+
Lux::Config.live_require_check! if Lux.config(:auto_code_reload)
|
188
|
+
|
189
|
+
main
|
190
|
+
|
191
|
+
current.response.render
|
192
|
+
end
|
193
|
+
|
182
194
|
end
|
@@ -4,6 +4,16 @@
|
|
4
4
|
# Hash :qs, Hash :post, String :method, Hash :cookies, Hash :session
|
5
5
|
# https://github.com/rack/rack/blob/master/test/spec_request.rb
|
6
6
|
|
7
|
+
# Lux.app.render('/admin') -> { status: 403, ... }
|
8
|
+
# Lux.app.render('/admin', session: { user_id: User.is_admin.first.id })
|
9
|
+
# {
|
10
|
+
# time: '1ms'
|
11
|
+
# status: 200,
|
12
|
+
# headers: {...},
|
13
|
+
# session: {...},
|
14
|
+
# body: '<html> ...'
|
15
|
+
# }.h
|
16
|
+
|
7
17
|
Lux::Application.class_eval do
|
8
18
|
def self.render path='/mock', in_opts={}, &block
|
9
19
|
allowed_opts = [:qs, :post, :method, :session, :cookies]
|
@@ -35,14 +45,14 @@ Lux::Application.class_eval do
|
|
35
45
|
env[k.to_s.upcase] = v
|
36
46
|
end
|
37
47
|
|
38
|
-
|
39
|
-
|
48
|
+
current = Lux::Current.new(env)
|
49
|
+
current.session.merge!(in_opts[:session]) if in_opts[:session]
|
40
50
|
|
41
|
-
|
42
|
-
|
43
|
-
|
51
|
+
app = new current
|
52
|
+
|
53
|
+
return app.instance_exec &block if block_given?
|
44
54
|
|
45
|
-
response =
|
55
|
+
response = app.render
|
46
56
|
|
47
57
|
body = response[2].join('')
|
48
58
|
body = JSON.parse body if response[1]['content-type'].index('/json')
|
@@ -51,7 +61,7 @@ Lux::Application.class_eval do
|
|
51
61
|
time: response[1]['x-lux-speed'],
|
52
62
|
status: response[0],
|
53
63
|
headers: response[1],
|
54
|
-
session:
|
64
|
+
session: current.session,
|
55
65
|
body: body
|
56
66
|
}.h
|
57
67
|
end
|
@@ -55,6 +55,18 @@ class Lux::Current::StaticFile
|
|
55
55
|
true
|
56
56
|
end
|
57
57
|
|
58
|
+
def resolve_content_type
|
59
|
+
mimme = MIMME_TYPES[@ext.to_sym]
|
60
|
+
|
61
|
+
unless mimme
|
62
|
+
c.response.body('Mimme type not supported')
|
63
|
+
c.response.status(406)
|
64
|
+
return
|
65
|
+
end
|
66
|
+
|
67
|
+
c.response.content_type = mimme
|
68
|
+
end
|
69
|
+
|
58
70
|
def deliver data=nil
|
59
71
|
file = File.exist?(@file) ? @file : Lux.root.join("public#{@file}")
|
60
72
|
|
@@ -66,16 +78,9 @@ class Lux::Current::StaticFile
|
|
66
78
|
end
|
67
79
|
end
|
68
80
|
|
69
|
-
ext = file.to_s.split('.').last
|
70
|
-
mimme = MIMME_TYPES[ext.to_sym]
|
71
|
-
|
72
|
-
unless mimme
|
73
|
-
c.response.body('Mimme type not supported')
|
74
|
-
c.response.status(404)
|
75
|
-
return
|
76
|
-
end
|
81
|
+
@ext = file.to_s.split('.').last
|
77
82
|
|
78
|
-
|
83
|
+
resolve_content_type
|
79
84
|
|
80
85
|
file_mtime = File.mtime(file).utc.to_s
|
81
86
|
key = Crypt.sha1(file+file_mtime.to_s)
|
@@ -91,10 +96,7 @@ class Lux::Current::StaticFile
|
|
91
96
|
return
|
92
97
|
end
|
93
98
|
|
94
|
-
|
95
|
-
|
96
|
-
data ||= File.read(file)
|
97
|
-
c.response.body(data)
|
99
|
+
c.response.body data || File.read(file)
|
98
100
|
|
99
101
|
true
|
100
102
|
end
|
data/lib/lux/lux.rb
CHANGED
@@ -25,11 +25,12 @@ module Lux
|
|
25
25
|
define_method(:cache) { Lux::Cache }
|
26
26
|
|
27
27
|
# main rack response
|
28
|
-
def call
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
28
|
+
def call env=nil
|
29
|
+
state = Lux::Current.new env
|
30
|
+
app = Lux::Application.new state
|
31
|
+
app.render
|
32
|
+
rescue => e
|
33
|
+
raise e if Lux.config(:show_server_errors)
|
33
34
|
|
34
35
|
[500, {}, ['Lux server error']]
|
35
36
|
end
|
@@ -165,34 +165,23 @@ class Lux::Response
|
|
165
165
|
|
166
166
|
def write_response_header
|
167
167
|
domain =
|
168
|
-
if
|
169
|
-
cookie_domain
|
170
|
-
elsif cookie_multidomain && current.domain.index('.')
|
168
|
+
if cookie_multidomain && current.domain.index('.')
|
171
169
|
".#{current.domain}"
|
172
170
|
else
|
173
|
-
current.request.host
|
171
|
+
cookie_domain || current.request.host
|
174
172
|
end
|
175
173
|
|
176
174
|
# cache-control
|
177
175
|
@headers['cache-control'] ||= Proc.new do
|
178
|
-
cc
|
179
|
-
|
180
|
-
if max_age > 0
|
181
|
-
cc.push 'public, no-cache'
|
182
|
-
else
|
183
|
-
cc.push 'private, must-revalidate'
|
184
|
-
end
|
185
|
-
|
176
|
+
cc = ['max-age=%d' % max_age]
|
177
|
+
cc.push max_age > 0 ? 'public, no-cache' : 'private, must-revalidate'
|
186
178
|
cc.join(', ')
|
187
179
|
end.call
|
188
180
|
|
189
181
|
current.session[:lux_flash] = flash.to_h
|
190
182
|
|
191
183
|
# dont annd cookies to public pages (images, etc..)
|
192
|
-
|
193
|
-
add_cookies = false if @headers['cache-control'].index('public')
|
194
|
-
|
195
|
-
if add_cookies
|
184
|
+
unless @headers['cache-control'].index('public')
|
196
185
|
encrypted = Crypt.encrypt(current.session.to_json)
|
197
186
|
|
198
187
|
if current.cookies[Lux.config.session_cookie_name] != encrypted
|
@@ -208,7 +197,7 @@ class Lux::Response
|
|
208
197
|
# if "no-store" is present then HTTP_IF_NONE_MATCH is not sent from browser
|
209
198
|
end
|
210
199
|
|
211
|
-
def
|
200
|
+
def render
|
212
201
|
write_response_body
|
213
202
|
write_response_header
|
214
203
|
|
@@ -224,14 +213,4 @@ class Lux::Response
|
|
224
213
|
[@status, @headers.to_h, [@body]]
|
225
214
|
end
|
226
215
|
|
227
|
-
def render
|
228
|
-
Lux.log "\n#{current.request.request_method.white} #{Lux.current.request.path.white}"
|
229
|
-
|
230
|
-
Lux::Config.live_require_check! if Lux.config(:auto_code_reload)
|
231
|
-
|
232
|
-
Lux::Application.new.main
|
233
|
-
|
234
|
-
write_response
|
235
|
-
end
|
236
|
-
|
237
216
|
end
|