pakyow-core 0.10.2 → 0.11.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 +4 -4
- data/pakyow-core/CHANGELOG.md +23 -0
- data/pakyow-core/lib/pakyow/core/app.rb +81 -0
- data/pakyow-core/lib/{core → pakyow/core}/app_context.rb +0 -0
- data/pakyow-core/lib/pakyow/core/base.rb +61 -0
- data/pakyow-core/lib/pakyow/core/call_context.rb +171 -0
- data/pakyow-core/lib/{core → pakyow/core}/config/app.rb +10 -30
- data/pakyow-core/lib/pakyow/core/config/cookies.rb +4 -0
- data/pakyow-core/lib/{core → pakyow/core}/config/logger.rb +11 -16
- data/pakyow-core/lib/pakyow/core/config/reloader.rb +10 -0
- data/pakyow-core/lib/{core → pakyow/core}/config/server.rb +2 -4
- data/pakyow-core/lib/pakyow/core/config/session.rb +41 -0
- data/pakyow-core/lib/{core → pakyow/core}/config.rb +2 -0
- data/pakyow-core/lib/{core → pakyow/core}/errors.rb +0 -0
- data/pakyow-core/lib/pakyow/core/helpers/configuring.rb +142 -0
- data/pakyow-core/lib/pakyow/core/helpers/hooks.rb +106 -0
- data/pakyow-core/lib/pakyow/core/helpers/running.rb +124 -0
- data/pakyow-core/lib/{core → pakyow/core}/helpers.rb +21 -5
- data/pakyow-core/lib/{core → pakyow/core}/loader.rb +1 -1
- data/pakyow-core/lib/{core → pakyow/core}/middleware/logger.rb +8 -2
- data/pakyow-core/lib/pakyow/core/middleware/override.rb +3 -0
- data/pakyow-core/lib/pakyow/core/middleware/reloader.rb +23 -0
- data/pakyow-core/lib/pakyow/core/middleware/req_path_normalizer.rb +49 -0
- data/pakyow-core/lib/pakyow/core/middleware/session.rb +5 -0
- data/pakyow-core/lib/pakyow/core/middleware/static.rb +76 -0
- data/pakyow-core/lib/{core → pakyow/core}/multilog.rb +0 -0
- data/pakyow-core/lib/{core → pakyow/core}/request.rb +7 -3
- data/pakyow-core/lib/{core → pakyow/core}/response.rb +4 -2
- data/pakyow-core/lib/{core → pakyow/core}/route_eval.rb +0 -0
- data/pakyow-core/lib/{core → pakyow/core}/route_expansion_eval.rb +1 -1
- data/pakyow-core/lib/{core → pakyow/core}/route_lookup.rb +0 -0
- data/pakyow-core/lib/{core → pakyow/core}/route_merger.rb +0 -0
- data/pakyow-core/lib/{core → pakyow/core}/route_module.rb +0 -0
- data/pakyow-core/lib/{core → pakyow/core}/route_set.rb +2 -2
- data/pakyow-core/lib/{core → pakyow/core}/route_template_defaults.rb +0 -0
- data/pakyow-core/lib/{core → pakyow/core}/route_template_eval.rb +0 -0
- data/pakyow-core/lib/{core → pakyow/core}/router.rb +4 -0
- data/pakyow-core/lib/pakyow/core.rb +8 -0
- data/pakyow-core/lib/pakyow-core.rb +1 -11
- metadata +41 -34
- data/pakyow-core/lib/core/app.rb +0 -469
- data/pakyow-core/lib/core/base.rb +0 -56
- data/pakyow-core/lib/core/config/cookies.rb +0 -4
- data/pakyow-core/lib/core/middleware/reloader.rb +0 -14
- data/pakyow-core/lib/core/middleware/static.rb +0 -40
- data/pakyow-core/lib/views/errors/404.html +0 -13
- data/pakyow-core/lib/views/errors/500.html +0 -15
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pakyow-core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.11.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bryan Powell
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-04-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pakyow-support
|
@@ -17,14 +17,14 @@ dependencies:
|
|
17
17
|
requirements:
|
18
18
|
- - '='
|
19
19
|
- !ruby/object:Gem::Version
|
20
|
-
version: 0.
|
20
|
+
version: 0.11.0
|
21
21
|
type: :runtime
|
22
22
|
prerelease: false
|
23
23
|
version_requirements: !ruby/object:Gem::Requirement
|
24
24
|
requirements:
|
25
25
|
- - '='
|
26
26
|
- !ruby/object:Gem::Version
|
27
|
-
version: 0.
|
27
|
+
version: 0.11.0
|
28
28
|
- !ruby/object:Gem::Dependency
|
29
29
|
name: rack
|
30
30
|
requirement: !ruby/object:Gem::Requirement
|
@@ -76,35 +76,43 @@ files:
|
|
76
76
|
- pakyow-core/CHANGELOG.md
|
77
77
|
- pakyow-core/LICENSE
|
78
78
|
- pakyow-core/README.md
|
79
|
-
- pakyow-core/lib/core/app.rb
|
80
|
-
- pakyow-core/lib/core/app_context.rb
|
81
|
-
- pakyow-core/lib/core/base.rb
|
82
|
-
- pakyow-core/lib/core/config.rb
|
83
|
-
- pakyow-core/lib/core/config/app.rb
|
84
|
-
- pakyow-core/lib/core/config/cookies.rb
|
85
|
-
- pakyow-core/lib/core/config/logger.rb
|
86
|
-
- pakyow-core/lib/core/config/server.rb
|
87
|
-
- pakyow-core/lib/core/errors.rb
|
88
|
-
- pakyow-core/lib/core/helpers.rb
|
89
|
-
- pakyow-core/lib/core/loader.rb
|
90
|
-
- pakyow-core/lib/core/middleware/logger.rb
|
91
|
-
- pakyow-core/lib/core/middleware/reloader.rb
|
92
|
-
- pakyow-core/lib/core/middleware/static.rb
|
93
|
-
- pakyow-core/lib/core/multilog.rb
|
94
|
-
- pakyow-core/lib/core/request.rb
|
95
|
-
- pakyow-core/lib/core/response.rb
|
96
|
-
- pakyow-core/lib/core/route_eval.rb
|
97
|
-
- pakyow-core/lib/core/route_expansion_eval.rb
|
98
|
-
- pakyow-core/lib/core/route_lookup.rb
|
99
|
-
- pakyow-core/lib/core/route_merger.rb
|
100
|
-
- pakyow-core/lib/core/route_module.rb
|
101
|
-
- pakyow-core/lib/core/route_set.rb
|
102
|
-
- pakyow-core/lib/core/route_template_defaults.rb
|
103
|
-
- pakyow-core/lib/core/route_template_eval.rb
|
104
|
-
- pakyow-core/lib/core/router.rb
|
105
79
|
- pakyow-core/lib/pakyow-core.rb
|
106
|
-
- pakyow-core/lib/
|
107
|
-
- pakyow-core/lib/
|
80
|
+
- pakyow-core/lib/pakyow/core.rb
|
81
|
+
- pakyow-core/lib/pakyow/core/app.rb
|
82
|
+
- pakyow-core/lib/pakyow/core/app_context.rb
|
83
|
+
- pakyow-core/lib/pakyow/core/base.rb
|
84
|
+
- pakyow-core/lib/pakyow/core/call_context.rb
|
85
|
+
- pakyow-core/lib/pakyow/core/config.rb
|
86
|
+
- pakyow-core/lib/pakyow/core/config/app.rb
|
87
|
+
- pakyow-core/lib/pakyow/core/config/cookies.rb
|
88
|
+
- pakyow-core/lib/pakyow/core/config/logger.rb
|
89
|
+
- pakyow-core/lib/pakyow/core/config/reloader.rb
|
90
|
+
- pakyow-core/lib/pakyow/core/config/server.rb
|
91
|
+
- pakyow-core/lib/pakyow/core/config/session.rb
|
92
|
+
- pakyow-core/lib/pakyow/core/errors.rb
|
93
|
+
- pakyow-core/lib/pakyow/core/helpers.rb
|
94
|
+
- pakyow-core/lib/pakyow/core/helpers/configuring.rb
|
95
|
+
- pakyow-core/lib/pakyow/core/helpers/hooks.rb
|
96
|
+
- pakyow-core/lib/pakyow/core/helpers/running.rb
|
97
|
+
- pakyow-core/lib/pakyow/core/loader.rb
|
98
|
+
- pakyow-core/lib/pakyow/core/middleware/logger.rb
|
99
|
+
- pakyow-core/lib/pakyow/core/middleware/override.rb
|
100
|
+
- pakyow-core/lib/pakyow/core/middleware/reloader.rb
|
101
|
+
- pakyow-core/lib/pakyow/core/middleware/req_path_normalizer.rb
|
102
|
+
- pakyow-core/lib/pakyow/core/middleware/session.rb
|
103
|
+
- pakyow-core/lib/pakyow/core/middleware/static.rb
|
104
|
+
- pakyow-core/lib/pakyow/core/multilog.rb
|
105
|
+
- pakyow-core/lib/pakyow/core/request.rb
|
106
|
+
- pakyow-core/lib/pakyow/core/response.rb
|
107
|
+
- pakyow-core/lib/pakyow/core/route_eval.rb
|
108
|
+
- pakyow-core/lib/pakyow/core/route_expansion_eval.rb
|
109
|
+
- pakyow-core/lib/pakyow/core/route_lookup.rb
|
110
|
+
- pakyow-core/lib/pakyow/core/route_merger.rb
|
111
|
+
- pakyow-core/lib/pakyow/core/route_module.rb
|
112
|
+
- pakyow-core/lib/pakyow/core/route_set.rb
|
113
|
+
- pakyow-core/lib/pakyow/core/route_template_defaults.rb
|
114
|
+
- pakyow-core/lib/pakyow/core/route_template_eval.rb
|
115
|
+
- pakyow-core/lib/pakyow/core/router.rb
|
108
116
|
homepage: http://pakyow.org
|
109
117
|
licenses:
|
110
118
|
- MIT
|
@@ -125,9 +133,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
125
133
|
version: '0'
|
126
134
|
requirements: []
|
127
135
|
rubyforge_project:
|
128
|
-
rubygems_version: 2.
|
136
|
+
rubygems_version: 2.5.1
|
129
137
|
signing_key:
|
130
138
|
specification_version: 4
|
131
139
|
summary: Pakyow Core
|
132
140
|
test_files: []
|
133
|
-
has_rdoc:
|
data/pakyow-core/lib/core/app.rb
DELETED
@@ -1,469 +0,0 @@
|
|
1
|
-
module Pakyow
|
2
|
-
class App
|
3
|
-
class << self
|
4
|
-
attr_reader :path
|
5
|
-
|
6
|
-
# Prepares the app for being staged in one or more environments by
|
7
|
-
# loading config(s), middleware, and setting the load path.
|
8
|
-
#
|
9
|
-
def prepare(*env_or_envs)
|
10
|
-
return if prepared?
|
11
|
-
|
12
|
-
# load config for one or more environments
|
13
|
-
load_config(*env_or_envs)
|
14
|
-
|
15
|
-
# load each block from middleware stack
|
16
|
-
load_middleware
|
17
|
-
|
18
|
-
# include pwd in load path
|
19
|
-
$:.unshift(Dir.pwd) unless $:.include? Dir.pwd
|
20
|
-
|
21
|
-
@prepared = true
|
22
|
-
end
|
23
|
-
|
24
|
-
# Stages the app by preparing and returning an instance. This is
|
25
|
-
# essentially everything short of running it.
|
26
|
-
#
|
27
|
-
def stage(*env_or_envs)
|
28
|
-
unless staged?
|
29
|
-
prepare(*env_or_envs)
|
30
|
-
@staged = true
|
31
|
-
end
|
32
|
-
|
33
|
-
self.new
|
34
|
-
end
|
35
|
-
|
36
|
-
# Runs the staged app.
|
37
|
-
#
|
38
|
-
def run(*env_or_envs)
|
39
|
-
return if running?
|
40
|
-
|
41
|
-
@running = true
|
42
|
-
|
43
|
-
builder.run(stage(*env_or_envs))
|
44
|
-
detect_handler.run(builder, Host: config.server.host, Port: config.server.port) do |server|
|
45
|
-
trap(:INT) { stop(server) }
|
46
|
-
trap(:TERM) { stop(server) }
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
# Defines an app
|
51
|
-
#
|
52
|
-
def define(&block)
|
53
|
-
# sets the path to the app file so it can be reloaded later
|
54
|
-
@path = String.parse_path_from_caller(caller[0])
|
55
|
-
self.instance_eval(&block)
|
56
|
-
end
|
57
|
-
|
58
|
-
# Defines a route set.
|
59
|
-
#
|
60
|
-
def routes(set_name = :main, &block)
|
61
|
-
if set_name && block
|
62
|
-
@@routes[set_name] = block
|
63
|
-
else
|
64
|
-
@@routes
|
65
|
-
end
|
66
|
-
end
|
67
|
-
|
68
|
-
# Accepts block to be added to middleware stack.
|
69
|
-
#
|
70
|
-
def middleware(&block)
|
71
|
-
@@middleware << block
|
72
|
-
end
|
73
|
-
|
74
|
-
# Creates an environment.
|
75
|
-
#
|
76
|
-
def configure(env, &block)
|
77
|
-
@@config[env] = block
|
78
|
-
end
|
79
|
-
|
80
|
-
# Fetches a stack (before | after) by name.
|
81
|
-
#
|
82
|
-
def stack(which, name)
|
83
|
-
@@stacks[which][name]
|
84
|
-
end
|
85
|
-
|
86
|
-
# Adds a block to the before stack for `stack_name`.
|
87
|
-
#
|
88
|
-
def before(stack_name, &block)
|
89
|
-
@@stacks[:before][stack_name.to_sym] << block
|
90
|
-
end
|
91
|
-
|
92
|
-
# Adds a block to the after stack for `stack_name`.
|
93
|
-
#
|
94
|
-
def after(stack_name, &block)
|
95
|
-
@@stacks[:after][stack_name.to_sym] << block
|
96
|
-
end
|
97
|
-
|
98
|
-
def builder
|
99
|
-
@builder ||= Rack::Builder.new
|
100
|
-
end
|
101
|
-
|
102
|
-
def prepared?
|
103
|
-
@prepared
|
104
|
-
end
|
105
|
-
|
106
|
-
# Returns true if the application is running.
|
107
|
-
#
|
108
|
-
def running?
|
109
|
-
@running
|
110
|
-
end
|
111
|
-
|
112
|
-
# Returns true if the application is staged.
|
113
|
-
#
|
114
|
-
def staged?
|
115
|
-
@staged
|
116
|
-
end
|
117
|
-
|
118
|
-
# Convenience method for base configuration class.
|
119
|
-
#
|
120
|
-
def config
|
121
|
-
Pakyow::Config
|
122
|
-
end
|
123
|
-
|
124
|
-
def reset
|
125
|
-
@prepared = false
|
126
|
-
@staged = false
|
127
|
-
@running = false
|
128
|
-
|
129
|
-
@@routes = {}
|
130
|
-
@@config = {}
|
131
|
-
@@middleware = []
|
132
|
-
|
133
|
-
@@stacks = {:before => {}, :after => {}}
|
134
|
-
%w(init load process route match error).each {|name|
|
135
|
-
@@stacks[:before][name.to_sym] = []
|
136
|
-
@@stacks[:after][name.to_sym] = []
|
137
|
-
}
|
138
|
-
end
|
139
|
-
|
140
|
-
def load_config(*env_or_envs)
|
141
|
-
envs = Array.ensure(env_or_envs)
|
142
|
-
envs = envs.empty? || envs.first.nil? ? [config.app.default_environment] : envs
|
143
|
-
|
144
|
-
config.app.loaded_envs = envs
|
145
|
-
config.env = envs.first.to_sym
|
146
|
-
|
147
|
-
# run specific config first
|
148
|
-
envs.each do |env|
|
149
|
-
next unless config_proc = @@config[env.to_sym]
|
150
|
-
config.app_config(&config_proc)
|
151
|
-
end
|
152
|
-
|
153
|
-
# then run global config
|
154
|
-
if global_proc = @@config[:global]
|
155
|
-
config.app_config(&global_proc)
|
156
|
-
end
|
157
|
-
end
|
158
|
-
|
159
|
-
protected
|
160
|
-
|
161
|
-
def load_middleware
|
162
|
-
@@middleware.each do |mw|
|
163
|
-
self.instance_exec(builder, &mw)
|
164
|
-
end
|
165
|
-
|
166
|
-
builder.use(Rack::MethodOverride)
|
167
|
-
builder.use(Middleware::Static) if config.app.static
|
168
|
-
builder.use(Middleware::Logger) if config.app.log
|
169
|
-
builder.use(Middleware::Reloader) if config.app.auto_reload
|
170
|
-
end
|
171
|
-
|
172
|
-
def detect_handler
|
173
|
-
handlers = ['puma', 'thin', 'mongrel', 'webrick']
|
174
|
-
handlers.unshift(config.server.handler) if config.server.handler
|
175
|
-
|
176
|
-
handlers.each do |handler|
|
177
|
-
begin
|
178
|
-
return Rack::Handler.get(handler)
|
179
|
-
rescue LoadError
|
180
|
-
rescue NameError
|
181
|
-
end
|
182
|
-
end
|
183
|
-
end
|
184
|
-
|
185
|
-
def stop(server)
|
186
|
-
if server.respond_to?('stop!')
|
187
|
-
server.stop!
|
188
|
-
elsif server.respond_to?('stop')
|
189
|
-
server.stop
|
190
|
-
else
|
191
|
-
# exit ungracefully if necessary...
|
192
|
-
Process.exit!
|
193
|
-
end
|
194
|
-
end
|
195
|
-
end
|
196
|
-
|
197
|
-
include Helpers
|
198
|
-
include AppHelpers
|
199
|
-
|
200
|
-
attr_writer :context
|
201
|
-
|
202
|
-
def initialize
|
203
|
-
Pakyow.app = self
|
204
|
-
Pakyow.configure_logger
|
205
|
-
|
206
|
-
call_stack(:before, :init)
|
207
|
-
|
208
|
-
load_app
|
209
|
-
|
210
|
-
call_stack(:after, :init)
|
211
|
-
end
|
212
|
-
|
213
|
-
# Returns the primary (first) loaded env.
|
214
|
-
#
|
215
|
-
def env
|
216
|
-
config.env
|
217
|
-
end
|
218
|
-
|
219
|
-
def app
|
220
|
-
self
|
221
|
-
end
|
222
|
-
|
223
|
-
def call(env)
|
224
|
-
dup.process(env)
|
225
|
-
end
|
226
|
-
|
227
|
-
# Called on every request.
|
228
|
-
#
|
229
|
-
def process(env)
|
230
|
-
call_stack(:before, :process)
|
231
|
-
|
232
|
-
req = Request.new(env)
|
233
|
-
res = Response.new
|
234
|
-
|
235
|
-
# set response format based on request
|
236
|
-
res.format = req.format
|
237
|
-
|
238
|
-
@context = AppContext.new(req, res)
|
239
|
-
|
240
|
-
set_initial_cookies
|
241
|
-
|
242
|
-
@found = false
|
243
|
-
catch(:halt) {
|
244
|
-
call_stack(:before, :route)
|
245
|
-
|
246
|
-
@found = @router.perform(context, self) {
|
247
|
-
call_stack(:after, :match)
|
248
|
-
}
|
249
|
-
|
250
|
-
call_stack(:after, :route)
|
251
|
-
|
252
|
-
unless found?
|
253
|
-
handle(404, false)
|
254
|
-
|
255
|
-
present_error 404 do |content|
|
256
|
-
path = String.normalize_path(request.path)
|
257
|
-
path = '/' if path.empty?
|
258
|
-
|
259
|
-
content.gsub!('{route_path}', path)
|
260
|
-
content
|
261
|
-
end
|
262
|
-
end
|
263
|
-
}
|
264
|
-
|
265
|
-
set_cookies
|
266
|
-
|
267
|
-
call_stack(:after, :process)
|
268
|
-
|
269
|
-
response.finish
|
270
|
-
rescue StandardError => error
|
271
|
-
request.error = error
|
272
|
-
|
273
|
-
catch :halt do
|
274
|
-
call_stack(:before, :error)
|
275
|
-
|
276
|
-
handle(500, false) unless found?
|
277
|
-
|
278
|
-
present_error 500 do |content|
|
279
|
-
nice_source = error.backtrace[0].match(/^(.+?):(\d+)(|:in `(.+)')$/)
|
280
|
-
|
281
|
-
content.gsub!('{file}', nice_source[1].gsub(File.expand_path(Config.app.root) + '/', ''))
|
282
|
-
content.gsub!('{line}', nice_source[2])
|
283
|
-
|
284
|
-
content.gsub!('{msg}', CGI.escapeHTML("#{error.class}: #{error}"))
|
285
|
-
content.gsub!('{trace}', error.backtrace.map { |bt| CGI.escapeHTML(bt) }.join('<br>'))
|
286
|
-
|
287
|
-
content
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
call_stack(:after, :error)
|
292
|
-
|
293
|
-
response.finish
|
294
|
-
end
|
295
|
-
|
296
|
-
def found?
|
297
|
-
@found
|
298
|
-
end
|
299
|
-
|
300
|
-
# This is NOT a useless method, it's a part of the external api
|
301
|
-
def reload
|
302
|
-
# reload the app file
|
303
|
-
load(config.app.path)
|
304
|
-
|
305
|
-
# reset config
|
306
|
-
envs = config.app.loaded_envs
|
307
|
-
config.reset
|
308
|
-
|
309
|
-
# reload config
|
310
|
-
self.class.load_config(*envs)
|
311
|
-
|
312
|
-
load_app
|
313
|
-
end
|
314
|
-
|
315
|
-
# APP ACTIONS
|
316
|
-
|
317
|
-
# Interrupts the application and returns response immediately.
|
318
|
-
#
|
319
|
-
def halt
|
320
|
-
throw :halt, response
|
321
|
-
end
|
322
|
-
|
323
|
-
# Routes the request to different logic.
|
324
|
-
#
|
325
|
-
def reroute(location, method = nil)
|
326
|
-
location = Router.instance.path(location)
|
327
|
-
request.setup(location, method)
|
328
|
-
|
329
|
-
call_stack(:before, :route)
|
330
|
-
call_stack(:after, :match)
|
331
|
-
@router.reroute(request)
|
332
|
-
call_stack(:after, :route)
|
333
|
-
end
|
334
|
-
|
335
|
-
# Sends data in the response (immediately). Accepts a string of data or a File,
|
336
|
-
# mime-type (auto-detected; defaults to octet-stream), and optional file name.
|
337
|
-
#
|
338
|
-
# If a File, mime type will be guessed. Otherwise mime type and file name will
|
339
|
-
# default to whatever is set in the response.
|
340
|
-
#
|
341
|
-
def send(file_or_data, type = nil, send_as = nil)
|
342
|
-
if file_or_data.class == File
|
343
|
-
data = file_or_data.read
|
344
|
-
|
345
|
-
# auto set type based on file type
|
346
|
-
type ||= Rack::Mime.mime_type("." + String.split_at_last_dot(file_or_data.path)[1])
|
347
|
-
else
|
348
|
-
data = file_or_data
|
349
|
-
end
|
350
|
-
|
351
|
-
headers = {}
|
352
|
-
headers["Content-Type"] = type if type
|
353
|
-
headers["Content-disposition"] = "attachment; filename=#{send_as}" if send_as
|
354
|
-
|
355
|
-
self.context.response = Response.new(data, response.status, response.header.merge(headers))
|
356
|
-
halt
|
357
|
-
end
|
358
|
-
|
359
|
-
# Redirects to location (immediately).
|
360
|
-
#
|
361
|
-
def redirect(location, status_code = 302)
|
362
|
-
location = Router.instance.path(location)
|
363
|
-
|
364
|
-
headers = response ? response.header : {}
|
365
|
-
headers = headers.merge({'Location' => location})
|
366
|
-
|
367
|
-
self.context.response = Response.new('', status_code, headers)
|
368
|
-
halt
|
369
|
-
end
|
370
|
-
|
371
|
-
def handle(name_or_code, from_logic = true)
|
372
|
-
call_stack(:before, :route)
|
373
|
-
@router.handle(name_or_code, self, from_logic)
|
374
|
-
call_stack(:after, :route)
|
375
|
-
end
|
376
|
-
|
377
|
-
# Convenience method for defining routes on an app instance.
|
378
|
-
#
|
379
|
-
def routes(set_name = :main, &block)
|
380
|
-
self.class.routes(set_name, &block)
|
381
|
-
load_routes
|
382
|
-
end
|
383
|
-
|
384
|
-
protected
|
385
|
-
|
386
|
-
def call_stack(which, stack)
|
387
|
-
self.class.stack(which, stack).each {|block|
|
388
|
-
self.instance_exec(&block)
|
389
|
-
}
|
390
|
-
end
|
391
|
-
|
392
|
-
# Reloads all application files in path and presenter (if specified).
|
393
|
-
#
|
394
|
-
def load_app
|
395
|
-
call_stack(:before, :load)
|
396
|
-
|
397
|
-
# load src files
|
398
|
-
@loader ||= Loader.new
|
399
|
-
@loader.load_from_path(config.app.src_dir)
|
400
|
-
|
401
|
-
# load the routes
|
402
|
-
load_routes
|
403
|
-
|
404
|
-
call_stack(:after, :load)
|
405
|
-
end
|
406
|
-
|
407
|
-
def load_routes
|
408
|
-
@router = Router.instance.reset
|
409
|
-
self.class.routes.each_pair {|set_name, block|
|
410
|
-
@router.set(set_name, &block)
|
411
|
-
} unless config.app.ignore_routes
|
412
|
-
end
|
413
|
-
|
414
|
-
def set_cookies
|
415
|
-
request.cookies.each_pair {|k, v|
|
416
|
-
response.delete_cookie(k) if v.nil?
|
417
|
-
|
418
|
-
# cookie is already set with value, ignore
|
419
|
-
next if @initial_cookies.include?(k.to_s) && @initial_cookies[k.to_s] == v
|
420
|
-
|
421
|
-
# set cookie with defaults
|
422
|
-
response.set_cookie(k, {
|
423
|
-
:path => config.cookies.path,
|
424
|
-
:expires => config.cookies.expiration,
|
425
|
-
:value => v
|
426
|
-
})
|
427
|
-
}
|
428
|
-
|
429
|
-
# delete cookies that are no longer present
|
430
|
-
@initial_cookies.each {|k|
|
431
|
-
response.delete_cookie(k) unless request.cookies.key?(k.to_s)
|
432
|
-
}
|
433
|
-
end
|
434
|
-
|
435
|
-
# Stores set cookies at beginning of request cycle
|
436
|
-
# for comparison at the end of the cycle
|
437
|
-
def set_initial_cookies
|
438
|
-
@initial_cookies = {}
|
439
|
-
request.cookies.each {|k,v|
|
440
|
-
@initial_cookies[k] = v
|
441
|
-
}
|
442
|
-
end
|
443
|
-
|
444
|
-
def present_error(code)
|
445
|
-
return unless config.app.errors_in_browser
|
446
|
-
|
447
|
-
response["Content-Type"] = 'text/html'
|
448
|
-
|
449
|
-
if block_given?
|
450
|
-
content = yield(content_for_code(code))
|
451
|
-
end
|
452
|
-
|
453
|
-
response.body = [content]
|
454
|
-
end
|
455
|
-
|
456
|
-
def content_for_code(code)
|
457
|
-
File.open(
|
458
|
-
File.join(
|
459
|
-
'views',
|
460
|
-
'errors',
|
461
|
-
code.to_s + '.html'
|
462
|
-
)
|
463
|
-
).read
|
464
|
-
end
|
465
|
-
|
466
|
-
end
|
467
|
-
|
468
|
-
App.reset
|
469
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
require 'core/helpers'
|
2
|
-
require 'core/multilog'
|
3
|
-
require 'core/app_context'
|
4
|
-
require 'core/request'
|
5
|
-
require 'core/response'
|
6
|
-
require 'core/loader'
|
7
|
-
require 'core/router'
|
8
|
-
require 'core/route_merger'
|
9
|
-
require 'core/route_module'
|
10
|
-
require 'core/route_set'
|
11
|
-
require 'core/route_eval'
|
12
|
-
require 'core/route_expansion_eval'
|
13
|
-
require 'core/route_template_eval'
|
14
|
-
require 'core/route_template_defaults'
|
15
|
-
require 'core/route_lookup'
|
16
|
-
require 'core/app'
|
17
|
-
require 'core/errors'
|
18
|
-
require 'core/config'
|
19
|
-
require 'core/config/app'
|
20
|
-
require 'core/config/server'
|
21
|
-
require 'core/config/cookies'
|
22
|
-
require 'core/config/logger'
|
23
|
-
|
24
|
-
# middlewares
|
25
|
-
require 'core/middleware/logger'
|
26
|
-
require 'core/middleware/static'
|
27
|
-
require 'core/middleware/reloader'
|
28
|
-
|
29
|
-
module Pakyow
|
30
|
-
class << self
|
31
|
-
attr_accessor :app, :logger
|
32
|
-
end
|
33
|
-
|
34
|
-
def self.configure_logger
|
35
|
-
logs = []
|
36
|
-
|
37
|
-
if File.directory?(Config.logger.path)
|
38
|
-
log_path = File.join(Config.logger.path, Config.logger.filename)
|
39
|
-
|
40
|
-
begin
|
41
|
-
log = File.open(log_path, 'a')
|
42
|
-
log.sync if Config.logger.sync
|
43
|
-
|
44
|
-
logs << log
|
45
|
-
rescue StandardError => e
|
46
|
-
warn "Error opening '#{log_path}' for writing"
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
logs << $stdout if Config.app.log_output
|
51
|
-
|
52
|
-
io = logs.count > 1 ? MultiLog.new(*logs) : logs[0]
|
53
|
-
|
54
|
-
Pakyow.logger = Logger.new(io, Config.logger.level, Config.logger.colorize, Config.logger.auto_flush)
|
55
|
-
end
|
56
|
-
end
|
@@ -1,40 +0,0 @@
|
|
1
|
-
module Pakyow
|
2
|
-
module Middleware
|
3
|
-
class Static
|
4
|
-
def initialize(app)
|
5
|
-
@app = app
|
6
|
-
end
|
7
|
-
|
8
|
-
def call(env)
|
9
|
-
static, resource_path = is_static?(env)
|
10
|
-
|
11
|
-
if static
|
12
|
-
catch(:halt) do
|
13
|
-
app = Pakyow.app.dup
|
14
|
-
res = Response.new
|
15
|
-
req = Request.new(env)
|
16
|
-
app.context = AppContext.new(req, res)
|
17
|
-
app.send(File.open(resource_path))
|
18
|
-
end
|
19
|
-
else
|
20
|
-
@app.call(env)
|
21
|
-
end
|
22
|
-
end
|
23
|
-
|
24
|
-
private
|
25
|
-
|
26
|
-
def is_static?(env)
|
27
|
-
return false unless env['PATH_INFO'] =~ /\.(.*)$/
|
28
|
-
|
29
|
-
Config.app.resources.each_pair do |name, path|
|
30
|
-
resource_path = File.join(path, env['PATH_INFO'])
|
31
|
-
next unless File.exists?(resource_path)
|
32
|
-
return true, resource_path
|
33
|
-
end
|
34
|
-
|
35
|
-
return false
|
36
|
-
end
|
37
|
-
end
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|