pakyow-core 0.11.3 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (156) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +0 -0
  3. data/LICENSE +4 -0
  4. data/{pakyow-core/README.md → README.md} +3 -4
  5. data/commands/pakyow +13 -0
  6. data/lib/pakyow/app/connection/behavior/session.rb +33 -0
  7. data/lib/pakyow/app/connection/behavior/values.rb +42 -0
  8. data/lib/pakyow/app/connection/behavior/verifier.rb +34 -0
  9. data/lib/pakyow/app/connection/session/abstract.rb +24 -0
  10. data/lib/pakyow/app/connection/session/cookie.rb +56 -0
  11. data/lib/pakyow/app/connection.rb +57 -0
  12. data/lib/pakyow/app.rb +219 -0
  13. data/lib/pakyow/behavior/aspects.rb +45 -0
  14. data/lib/pakyow/behavior/config.rb +72 -0
  15. data/lib/pakyow/behavior/endpoints.rb +41 -0
  16. data/lib/pakyow/behavior/frameworks.rb +45 -0
  17. data/lib/pakyow/behavior/helpers.rb +107 -0
  18. data/lib/pakyow/behavior/initializers.rb +19 -0
  19. data/lib/pakyow/behavior/isolating.rb +69 -0
  20. data/lib/pakyow/behavior/operations.rb +46 -0
  21. data/lib/pakyow/behavior/pipeline.rb +58 -0
  22. data/lib/pakyow/behavior/plugins.rb +148 -0
  23. data/lib/pakyow/behavior/rescuing.rb +51 -0
  24. data/lib/pakyow/behavior/restarting.rb +68 -0
  25. data/lib/pakyow/behavior/sessions.rb +34 -0
  26. data/lib/pakyow/behavior/verification.rb +47 -0
  27. data/lib/pakyow/cli.rb +199 -0
  28. data/lib/pakyow/connection/multipart_input.rb +24 -0
  29. data/lib/pakyow/connection/multipart_parser.rb +112 -0
  30. data/lib/pakyow/connection/params.rb +29 -0
  31. data/lib/pakyow/connection/query_parser.rb +174 -0
  32. data/lib/pakyow/connection/statuses.rb +119 -0
  33. data/lib/pakyow/connection.rb +568 -0
  34. data/lib/pakyow/endpoints.rb +94 -0
  35. data/lib/pakyow/environment/actions/dispatch.rb +37 -0
  36. data/lib/pakyow/environment/actions/input_parser.rb +21 -0
  37. data/lib/pakyow/environment/actions/logger.rb +33 -0
  38. data/lib/pakyow/environment/actions/normalizer.rb +73 -0
  39. data/lib/pakyow/environment/behavior/config.rb +166 -0
  40. data/lib/pakyow/environment/behavior/initializers.rb +21 -0
  41. data/lib/pakyow/environment/behavior/input_parsing.rb +54 -0
  42. data/lib/pakyow/environment/behavior/plugins.rb +23 -0
  43. data/lib/pakyow/environment/behavior/restarting.rb +54 -0
  44. data/lib/pakyow/environment/behavior/running.rb +129 -0
  45. data/lib/pakyow/environment/behavior/silencing.rb +23 -0
  46. data/lib/pakyow/environment/behavior/timezone.rb +21 -0
  47. data/lib/pakyow/environment/behavior/watching.rb +75 -0
  48. data/lib/pakyow/environment.rb +381 -0
  49. data/lib/pakyow/error.rb +273 -0
  50. data/lib/pakyow/errors.rb +97 -0
  51. data/lib/pakyow/framework.rb +39 -0
  52. data/lib/pakyow/generator.rb +138 -0
  53. data/lib/pakyow/generators/project/default/%dot%env.erb +1 -0
  54. data/lib/pakyow/generators/project/default/%dot%gitignore +11 -0
  55. data/lib/pakyow/generators/project/default/%dot%ruby-version.erb +1 -0
  56. data/lib/pakyow/generators/project/default/Gemfile.erb +28 -0
  57. data/lib/pakyow/generators/project/default/README.md +14 -0
  58. data/lib/pakyow/generators/project/default/config/application.rb.erb +17 -0
  59. data/lib/pakyow/generators/project/default/config/environment.rb +11 -0
  60. data/lib/pakyow/generators/project/default/config/initializers/application/keep +0 -0
  61. data/lib/pakyow/generators/project/default/config/initializers/environment/keep +0 -0
  62. data/lib/pakyow/generators/project/default/database/keep +0 -0
  63. data/lib/pakyow/generators/project/default/frontend/assets/images/keep +0 -0
  64. data/lib/pakyow/generators/project/default/frontend/assets/packs/keep +0 -0
  65. data/lib/pakyow/generators/project/default/frontend/assets/styles/keep +0 -0
  66. data/lib/pakyow/generators/project/default/frontend/includes/keep +0 -0
  67. data/lib/pakyow/generators/project/default/frontend/layouts/default.html.erb +19 -0
  68. data/lib/pakyow/generators/project/default/frontend/pages/keep +0 -0
  69. data/lib/pakyow/generators/project/default/public/favicon.ico +0 -0
  70. data/lib/pakyow/generators/project/default/public/robots.txt +1 -0
  71. data/lib/pakyow/generators/project.rb +29 -0
  72. data/lib/pakyow/helpers/app.rb +28 -0
  73. data/lib/pakyow/helpers/connection.rb +45 -0
  74. data/lib/pakyow/info.rb +26 -0
  75. data/lib/pakyow/integrations/bootsnap.rb +18 -0
  76. data/lib/pakyow/integrations/bundler/require.rb +7 -0
  77. data/lib/pakyow/integrations/bundler/setup.rb +7 -0
  78. data/lib/pakyow/integrations/dotenv.rb +9 -0
  79. data/lib/pakyow/loader.rb +31 -0
  80. data/lib/pakyow/logger/colorizer.rb +38 -0
  81. data/lib/pakyow/logger/destination.rb +17 -0
  82. data/lib/pakyow/logger/formatter.rb +22 -0
  83. data/lib/pakyow/logger/formatters/human.rb +101 -0
  84. data/lib/pakyow/logger/formatters/json.rb +99 -0
  85. data/lib/pakyow/logger/formatters/logfmt.rb +50 -0
  86. data/lib/pakyow/logger/multiplexed.rb +19 -0
  87. data/lib/pakyow/logger/thread_local.rb +29 -0
  88. data/lib/pakyow/logger/timekeeper.rb +64 -0
  89. data/lib/pakyow/logger.rb +125 -0
  90. data/lib/pakyow/operation.rb +71 -0
  91. data/lib/pakyow/plugin/helper_caller.rb +20 -0
  92. data/lib/pakyow/plugin/lookup.rb +33 -0
  93. data/lib/pakyow/plugin/state.rb +68 -0
  94. data/lib/pakyow/plugin.rb +367 -0
  95. data/lib/pakyow/process_manager.rb +53 -0
  96. data/lib/pakyow/processes/proxy.rb +67 -0
  97. data/lib/pakyow/processes/server.rb +32 -0
  98. data/lib/pakyow/rack/compatibility.rb +68 -0
  99. data/lib/pakyow/task.rb +315 -0
  100. data/lib/pakyow/tasks/boot.rake +21 -0
  101. data/lib/pakyow/tasks/create.rake +32 -0
  102. data/lib/pakyow/tasks/help.rake +17 -0
  103. data/lib/pakyow/tasks/info/endpoints.rake +72 -0
  104. data/lib/pakyow/tasks/info.rake +31 -0
  105. data/lib/pakyow/tasks/irb.rake +11 -0
  106. data/lib/pakyow/tasks/prelaunch.rake +37 -0
  107. data/lib/pakyow/types.rb +32 -0
  108. data/lib/pakyow/validations/acceptance.rb +37 -0
  109. data/lib/pakyow/validations/email.rb +28 -0
  110. data/lib/pakyow/validations/inline.rb +32 -0
  111. data/lib/pakyow/validations/length.rb +44 -0
  112. data/lib/pakyow/validations/presence.rb +41 -0
  113. data/lib/pakyow/validations.rb +8 -0
  114. data/lib/pakyow/validator.rb +81 -0
  115. data/lib/pakyow/verifier.rb +177 -0
  116. data/lib/pakyow.rb +5 -0
  117. metadata +267 -65
  118. data/pakyow-core/CHANGELOG.md +0 -128
  119. data/pakyow-core/LICENSE +0 -20
  120. data/pakyow-core/lib/pakyow/core/app.rb +0 -82
  121. data/pakyow-core/lib/pakyow/core/app_context.rb +0 -10
  122. data/pakyow-core/lib/pakyow/core/base.rb +0 -61
  123. data/pakyow-core/lib/pakyow/core/call_context.rb +0 -171
  124. data/pakyow-core/lib/pakyow/core/config/app.rb +0 -49
  125. data/pakyow-core/lib/pakyow/core/config/cookies.rb +0 -4
  126. data/pakyow-core/lib/pakyow/core/config/logger.rb +0 -34
  127. data/pakyow-core/lib/pakyow/core/config/reloader.rb +0 -10
  128. data/pakyow-core/lib/pakyow/core/config/server.rb +0 -10
  129. data/pakyow-core/lib/pakyow/core/config/session.rb +0 -41
  130. data/pakyow-core/lib/pakyow/core/config.rb +0 -95
  131. data/pakyow-core/lib/pakyow/core/errors.rb +0 -16
  132. data/pakyow-core/lib/pakyow/core/helpers/configuring.rb +0 -142
  133. data/pakyow-core/lib/pakyow/core/helpers/hooks.rb +0 -106
  134. data/pakyow-core/lib/pakyow/core/helpers/running.rb +0 -124
  135. data/pakyow-core/lib/pakyow/core/helpers.rb +0 -61
  136. data/pakyow-core/lib/pakyow/core/loader.rb +0 -32
  137. data/pakyow-core/lib/pakyow/core/middleware/logger.rb +0 -146
  138. data/pakyow-core/lib/pakyow/core/middleware/override.rb +0 -3
  139. data/pakyow-core/lib/pakyow/core/middleware/reloader.rb +0 -23
  140. data/pakyow-core/lib/pakyow/core/middleware/req_path_normalizer.rb +0 -49
  141. data/pakyow-core/lib/pakyow/core/middleware/session.rb +0 -5
  142. data/pakyow-core/lib/pakyow/core/middleware/static.rb +0 -76
  143. data/pakyow-core/lib/pakyow/core/multilog.rb +0 -19
  144. data/pakyow-core/lib/pakyow/core/request.rb +0 -119
  145. data/pakyow-core/lib/pakyow/core/response.rb +0 -135
  146. data/pakyow-core/lib/pakyow/core/route_eval.rb +0 -254
  147. data/pakyow-core/lib/pakyow/core/route_expansion_eval.rb +0 -115
  148. data/pakyow-core/lib/pakyow/core/route_lookup.rb +0 -41
  149. data/pakyow-core/lib/pakyow/core/route_merger.rb +0 -76
  150. data/pakyow-core/lib/pakyow/core/route_module.rb +0 -21
  151. data/pakyow-core/lib/pakyow/core/route_set.rb +0 -74
  152. data/pakyow-core/lib/pakyow/core/route_template_defaults.rb +0 -43
  153. data/pakyow-core/lib/pakyow/core/route_template_eval.rb +0 -72
  154. data/pakyow-core/lib/pakyow/core/router.rb +0 -181
  155. data/pakyow-core/lib/pakyow/core.rb +0 -8
  156. data/pakyow-core/lib/pakyow-core.rb +0 -1
@@ -1,106 +0,0 @@
1
- module Pakyow
2
- module Helpers
3
- # Methods to register and call hooks before and after particular triggers.
4
- #
5
- # @api public
6
- module Hooks
7
- TYPES = %i(before after)
8
- TRIGGERS = %i(init load process route match error configure reload)
9
-
10
- module InstanceMethods
11
- protected
12
-
13
- def hook_around(trigger)
14
- call_hooks :before, trigger
15
- yield
16
- call_hooks :after, trigger
17
- end
18
-
19
- def call_hooks(type, trigger)
20
- self.class.hook(type, trigger).each do |block|
21
- instance_exec(&block)
22
- end
23
- end
24
- end
25
-
26
- def self.extended(object)
27
- object.send(:include, InstanceMethods)
28
- end
29
-
30
- # Registers a before hook for a particular trigger.
31
- #
32
- # @api public
33
- def before(trigger, &block)
34
- register_hook(:before, trigger, block)
35
- end
36
-
37
- # Registers an after hook for a particular trigger.
38
- #
39
- # @api public
40
- def after(trigger, &block)
41
- register_hook(:after, trigger, block)
42
- end
43
-
44
- # Fetches a hook by type (before | after) and trigger.
45
- #
46
- # @api private
47
- def hook(type, trigger)
48
- check_hook_type(type)
49
- check_trigger(trigger)
50
-
51
- hooks[type.to_sym][trigger.to_sym]
52
- end
53
-
54
- protected
55
-
56
- def hooks
57
- return @hooks unless @hooks.nil?
58
-
59
- @hooks = {
60
- before: {},
61
- after: {}
62
- }
63
-
64
- TRIGGERS.each do |name|
65
- @hooks[:before][name.to_sym] = []
66
- @hooks[:after][name.to_sym] = []
67
- end
68
-
69
- @hooks
70
- end
71
-
72
- def register_hook(type, trigger, block)
73
- raise ArgumentError, 'Expected a block' if block.nil?
74
-
75
- trigger = trigger.to_sym
76
-
77
- check_trigger(trigger)
78
- check_hook_type(type)
79
-
80
- hooks[type][trigger] << block
81
- end
82
-
83
- def hook_around(trigger)
84
- call_hooks :before, trigger
85
- yield
86
- call_hooks :after, trigger
87
- end
88
-
89
- def call_hooks(type, trigger)
90
- hook(type, trigger).each do |block|
91
- instance_exec(&block)
92
- end
93
- end
94
-
95
- def check_trigger(trigger)
96
- return true if TRIGGERS.include?(trigger)
97
- raise ArgumentError, "Hook trigger #{trigger} doesn't exist"
98
- end
99
-
100
- def check_hook_type(type)
101
- return true if TYPES.include?(type)
102
- raise ArgumentError, "Hook type #{type} doesn't exist"
103
- end
104
- end
105
- end
106
- end
@@ -1,124 +0,0 @@
1
- require 'rack/builder'
2
- require 'rack/handler'
3
-
4
- module Pakyow
5
- module Helpers
6
- # Methods run running an app.
7
- #
8
- # @api public
9
- module Running
10
- STOP_METHODS = ['stop!', 'stop']
11
- HANDLERS = ['puma', 'thin', 'mongrel', 'webrick']
12
- SIGNALS = [:INT, :TERM]
13
-
14
- # Prepares the app for being staged in one or more environments by
15
- # loading config(s), middleware, and setting the load path.
16
- #
17
- # @api public
18
- def prepare(*env_or_envs)
19
- return true if prepared?
20
-
21
- # load config for one or more environments
22
- load_env_config(*env_or_envs)
23
-
24
- # load each block from middleware stack
25
- load_middleware
26
-
27
- # add app/lib to load path
28
- $LOAD_PATH.unshift config.app.src_dir
29
-
30
- @prepared = true
31
- end
32
-
33
- # Stages the app by preparing and returning an instance. This is
34
- # essentially everything short of running it.
35
- #
36
- # @api public
37
- def stage(*env_or_envs)
38
- prepare(*env_or_envs)
39
- self.new
40
- end
41
-
42
- # Runs the staged app.
43
- #
44
- # @api public
45
- def run(*env_or_envs)
46
- return true if running?
47
- @running = true
48
- builder.run(stage(*env_or_envs))
49
- detect_handler.run(builder, Host: config.server.host, Port: config.server.port) do |server|
50
- SIGNALS.each do |signal|
51
- trap(signal) { stop(server) }
52
- end
53
- end
54
- end
55
-
56
- # Returns true if the application is prepared.
57
- #
58
- # @api public
59
- def prepared?
60
- @prepared == true
61
- end
62
-
63
- # Returns true if the application is running.
64
- #
65
- # @api public
66
- def running?
67
- @running == true
68
- end
69
-
70
- # Returns true if the application is staged.
71
- #
72
- # @api public
73
- def staged?
74
- !Pakyow.app.nil?
75
- end
76
-
77
- # Returns a rack builder instance.
78
- #
79
- # @api public
80
- def builder
81
- @builder ||= Rack::Builder.new
82
- end
83
-
84
- # Returns an instance of the rack handler.
85
- #
86
- # @api private
87
- def detect_handler
88
- if config.server.handler
89
- HANDLERS.unshift(config.server.handler).uniq!
90
- end
91
-
92
- HANDLERS.each do |handler_name|
93
- begin
94
- handler = Rack::Handler.get(handler_name)
95
- return handler unless handler.nil?
96
- rescue LoadError
97
- rescue NameError
98
- end
99
- end
100
-
101
- raise 'No handler found'
102
- end
103
-
104
- protected
105
-
106
- def stop(server)
107
- STOP_METHODS.each do |method|
108
- if server.respond_to?(method)
109
- return server.send(method)
110
- end
111
- end
112
-
113
- # exit ungracefully
114
- Process.exit!
115
- end
116
-
117
- def load_middleware
118
- middleware.each do |block|
119
- instance_exec(builder, &block)
120
- end
121
- end
122
- end
123
- end
124
- end
@@ -1,61 +0,0 @@
1
- module Pakyow
2
- # Helpers available anywhere
3
- #
4
- # @api public
5
- module Helpers
6
- def context
7
- @context or raise NoContextError
8
- end
9
-
10
- def logger
11
- request.logger || Pakyow.logger
12
- end
13
-
14
- def router
15
- RouteLookup.new
16
- end
17
-
18
- def request
19
- context ? context.request : nil
20
- end
21
- alias_method :req, :request
22
-
23
- def response
24
- context ? context.response : nil
25
- end
26
- alias_method :res, :response
27
-
28
- def params
29
- request ? request.params : {}
30
- end
31
-
32
- def session
33
- request ? request.session : {}
34
- end
35
-
36
- def cookies
37
- request ? request.cookies : {}
38
- end
39
-
40
- def config
41
- Pakyow::Config
42
- end
43
-
44
- # Returns the primary app environment.
45
- #
46
- # @api public
47
- def env
48
- config.env
49
- end
50
-
51
- # Helpers for Pakyow::App
52
- #
53
- # @api public
54
- module App; end
55
-
56
- # Helpers for Pakyow::CallContext
57
- #
58
- # @api public
59
- module Context; end
60
- end
61
- end
@@ -1,32 +0,0 @@
1
- module Pakyow
2
- # Handles the loading and reloading of a Pakyow application. If in development
3
- # mode, files are automatically reloaded if modified.
4
- class Loader
5
-
6
- # Loads files in the provided path, decending into child directories.
7
- def load_from_path(path)
8
- require_recursively(path)
9
- end
10
-
11
- protected
12
-
13
- def require_recursively(dir)
14
- @times ||= {}
15
- if File.exists?(dir)
16
- Dir.walk(dir) do |path|
17
- next if FileTest.directory?(path)
18
- next if path.split('.')[-1] != 'rb'
19
-
20
- if Config.reloader.enabled
21
- if !@times[path] || (@times[path] && File.mtime(path) - @times[path] > 0)
22
- load(path)
23
- @times[path] = File.mtime(path)
24
- end
25
- else
26
- require path
27
- end
28
- end
29
- end
30
- end
31
- end
32
- end
@@ -1,146 +0,0 @@
1
- module Pakyow
2
- class Logger
3
- LEVELS = {
4
- :debug => 0,
5
- :info => 1,
6
- :warn => 2,
7
- :error => 3,
8
- :fatal => 4,
9
- :unknown => 5,
10
- }
11
-
12
- LEVEL_COLORS = {
13
- :debug => :cyan,
14
- :info => :green,
15
- :warn => :yellow,
16
- :error => :red,
17
- :fatal => :red,
18
- # :unknown => nil,
19
- }
20
-
21
- COLOR_TABLE = [
22
- :black,
23
- :red,
24
- :green,
25
- :yellow,
26
- :blue,
27
- :magenta,
28
- :cyan,
29
- :white,
30
- ]
31
-
32
- RESET_SEQ = "\033[0m"
33
- COLOR_SEQ = "\033[%dm"
34
- BOLD_SEQ = "\033[1m"
35
-
36
- def initialize(log = $stdout, level = 0, format = false, auto_flush = false)
37
- level = LEVELS[level] if level.is_a?(Symbol)
38
- @log, @level, @format, @auto_flush = log, level, format, auto_flush
39
- @mutex = Mutex.new
40
- end
41
-
42
- def <<(msg = nil, severity = :unknown)
43
- return if @log.nil?
44
- (msg || "") << "\n"
45
-
46
- msg = format(msg, severity) if @format
47
- @mutex.synchronize do
48
- @log.write msg
49
- @log.flush if @auto_flush
50
- end
51
- end
52
-
53
- alias :write :<<
54
-
55
- def add(severity, msg = nil)
56
- severity ||= LEVELS[:unknown]
57
- return if severity < @level
58
-
59
- write(msg, severity)
60
- end
61
-
62
- alias :log :add
63
-
64
- def debug(msg = nil)
65
- add(LEVELS[:debug], msg)
66
- end
67
-
68
- def info(msg = nil)
69
- add(LEVELS[:info], msg)
70
- end
71
-
72
- def warn(msg = nil)
73
- add(LEVELS[:warn], msg)
74
- end
75
-
76
- def error(msg = nil)
77
- add(LEVELS[:error], msg)
78
- end
79
-
80
- def fatal(msg = nil)
81
- add(LEVELS[:fatal], msg)
82
- end
83
-
84
- def format(msg, level)
85
- return msg unless color = level_color(level)
86
- return COLOR_SEQ % (30 + COLOR_TABLE.index(color)) + (msg || "") + RESET_SEQ
87
- end
88
-
89
- def level_color(level)
90
- LEVEL_COLORS[LEVELS.key(level)]
91
- end
92
-
93
- def close
94
- @log.close
95
- end
96
-
97
- end
98
- end
99
-
100
- module Pakyow
101
- module Middleware
102
- Pakyow::App.middleware do |builder|
103
- if Pakyow::Config.logger.enabled
104
- builder.use Pakyow::Middleware::Logger
105
- end
106
- end
107
-
108
- class Logger
109
- # handles logging after an error occurs
110
- Pakyow::App.after(:error) {
111
- error = request.error
112
- Pakyow.logger.error "[500] #{error.class}: #{error}\n" + error.backtrace.join("\n") + "\n\n"
113
- }
114
-
115
- def initialize(app)
116
- @app = app
117
- end
118
-
119
- def call(env)
120
- env['rack.logger'] = Pakyow.logger
121
-
122
- result = nil
123
- difference = time { |began_at|
124
- Pakyow.logger << "#{env['REQUEST_METHOD']} #{env['REQUEST_URI']} for #{env['REMOTE_ADDR']} at #{began_at}"
125
- result = @app.call(env)
126
- }
127
-
128
- status = result[0]
129
- Pakyow.logger << "#{status} (#{nice_status(status)}) in #{difference}ms\n"
130
-
131
- result
132
- end
133
-
134
- def time
135
- s = Time.now
136
- yield(s)
137
- time = ((Time.now.to_f - s.to_f) * 1000.0)
138
- (time * 10**2).round / (10**2).to_f
139
- end
140
-
141
- def nice_status(status)
142
- Pakyow::Response::STATUS_CODE_NAMES[status] || '?'
143
- end
144
- end
145
- end
146
- end
@@ -1,3 +0,0 @@
1
- Pakyow::App.middleware do |builder|
2
- builder.use Rack::MethodOverride
3
- end
@@ -1,23 +0,0 @@
1
- module Pakyow
2
- module Middleware
3
- Pakyow::App.middleware do |builder|
4
- if Pakyow::Config.reloader.enabled
5
- builder.use Pakyow::Middleware::Reloader
6
- end
7
- end
8
-
9
- # Rack compatible middleware that tells app to reload on each request.
10
- #
11
- # @api public
12
- class Reloader
13
- def initialize(app)
14
- @app = app
15
- end
16
-
17
- def call(env)
18
- Pakyow.app.reload
19
- @app.call(env)
20
- end
21
- end
22
- end
23
- end
@@ -1,49 +0,0 @@
1
- require 'pakyow/core/call_context'
2
-
3
- module Pakyow
4
- module Middleware
5
- Pakyow::App.middleware do |builder|
6
- builder.use Pakyow::Middleware::ReqPathNormalizer
7
- end
8
-
9
- # Rack compatible middleware that normalize the path if contains '//'
10
- # or has a trailing '/', it replace '//' with '/', remove trailing `/`
11
- # and issue a 301 redirect to the normalized path.
12
- #
13
- # @api public
14
- class ReqPathNormalizer
15
- TAIL_SLASH_REPLACE_REGEX = /(\/)+$/
16
- TAIL_SLASH_REGEX = /(.)+(\/)+$/
17
-
18
- def initialize(app)
19
- @app = app
20
- end
21
-
22
- def call(env)
23
- path = env['PATH_INFO']
24
-
25
- if double_slash?(path) || tail_slash?(path)
26
- catch :halt do
27
- CallContext.new(env).redirect(normalize_path(path), 301)
28
- end
29
- else
30
- @app.call(env)
31
- end
32
- end
33
-
34
- def normalize_path(path)
35
- normalized = path
36
- .gsub('//', '/')
37
- .gsub(TAIL_SLASH_REPLACE_REGEX, '')
38
- end
39
-
40
- def double_slash?(path)
41
- path.include?('//')
42
- end
43
-
44
- def tail_slash?(path)
45
- (TAIL_SLASH_REGEX =~ path).nil? ? false : true
46
- end
47
- end
48
- end
49
- end
@@ -1,5 +0,0 @@
1
- Pakyow::App.middleware do |builder|
2
- if Pakyow::Config.session.enabled
3
- builder.use Pakyow::Config.session.object, Pakyow::Config.session.options
4
- end
5
- end
@@ -1,76 +0,0 @@
1
- require 'pakyow/core/config'
2
- require 'pakyow/core/call_context'
3
-
4
- module Pakyow
5
- module Middleware
6
- # Rack compatible middleware that serves static files from one or more configured resource stores.
7
- #
8
- # @example
9
- # Pakyow::Config.app.resources = {
10
- # default: './public'
11
- # }
12
- #
13
- # Pakyow::App.builder.use Pakyow::Middleware::Static
14
- #
15
- # # Assuming './public/foo.png' exists, a GET request to '/foo.png' will
16
- # # result in this middleware responding with the static file.
17
- #
18
- # @api public
19
- class Static
20
- Pakyow::App.middleware do |builder|
21
- if Pakyow::Config.app.static
22
- builder.use Pakyow::Middleware::Static
23
- end
24
- end
25
-
26
- def initialize(app)
27
- @app = app
28
- end
29
-
30
- def call(env)
31
- static, resource_path = self.class.static?(env)
32
- return @app.call(env) unless static
33
-
34
- catch :halt do
35
- CallContext.new(env).send(File.open(resource_path))
36
- end
37
- end
38
-
39
- class << self
40
- STATIC_REGEX = /\.(.*)$/
41
- STATIC_HTTP_METHODS = %w(GET)
42
-
43
- # Checks if `path` can be found in any configured resource store.
44
- #
45
- # @api public
46
- def static?(env)
47
- path, method = env.values_at('PATH_INFO', 'REQUEST_METHOD')
48
-
49
- return false unless STATIC_HTTP_METHODS.include?(method)
50
- return false unless static_path?(path)
51
-
52
- resources_contain?(path)
53
- end
54
-
55
- protected
56
-
57
- def static_path?(path)
58
- path =~ STATIC_REGEX
59
- end
60
-
61
- def resources_contain?(path)
62
- resources.each_pair do |_, resource_path|
63
- full_path = File.join(resource_path, path)
64
- return true, full_path if File.exists?(full_path)
65
- end
66
-
67
- false
68
- end
69
-
70
- def resources
71
- Config.app.resources
72
- end
73
- end
74
- end
75
- end
76
- end
@@ -1,19 +0,0 @@
1
- module Pakyow
2
- class MultiLog
3
- def initialize(*targets)
4
- @targets = targets
5
- end
6
-
7
- def write(*args)
8
- @targets.each { |t| t.write(*args) }
9
- end
10
-
11
- def close
12
- @targets.each(&:close)
13
- end
14
-
15
- def flush
16
- @targets.each(&:flush)
17
- end
18
- end
19
- end