lennarb 0.5.0 → 0.5.1
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/lib/lennarb/application/base.rb +54 -38
- data/lib/lennarb/version.rb +1 -1
- metadata +2 -30
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ff30294350acea0efb5cf2c074d256472f99300d46fee6fe0ea9cadfb375e151
|
4
|
+
data.tar.gz: d96ea3d9ade277730f83fa0b011d3396d73be3ba8692229c5ad98cfd72f48ea4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5aad4351361de62499992df5f53ac8ec5f8b42f3cd1b207d108ac137404ab6e4e103f394193ae58c847195cc97fe53d475d275e542840360b4d61dfe74474c4
|
7
|
+
data.tar.gz: 55661804a4cb178b3365534c5df7a3c7653ecbe07e598b04ae4edfda64f0ada02edcd03ff08cc6f3004a519f5b1df80da4eccc1f4b2a6162d14166e6786f471e
|
@@ -3,6 +3,8 @@
|
|
3
3
|
# Released under the MIT License.
|
4
4
|
# Copyright, 2023-2024, by Aristóteles Coutinho.
|
5
5
|
|
6
|
+
require 'colorize'
|
7
|
+
|
6
8
|
class Lennarb
|
7
9
|
module Application
|
8
10
|
class Base
|
@@ -91,39 +93,13 @@ class Lennarb
|
|
91
93
|
end
|
92
94
|
end
|
93
95
|
|
94
|
-
# Call the application
|
95
|
-
#
|
96
|
-
# @parameter [Hash] env
|
97
|
-
#
|
98
|
-
# @returns [Array] response
|
99
|
-
#
|
100
|
-
def self.call(env)
|
101
|
-
response =
|
102
|
-
catch(:halt) do
|
103
|
-
execute_hooks(@_before_hooks, env, :before)
|
104
|
-
|
105
|
-
res = @_route.call(env)
|
106
|
-
|
107
|
-
execute_hooks(@_after_hooks, env, :after)
|
108
|
-
res
|
109
|
-
end
|
110
|
-
|
111
|
-
case response
|
112
|
-
in [404 | 404, _, _] if html_request?(env) && File.exist?('public/404.html')
|
113
|
-
env['PATH_INFO'] = '/404.html'
|
114
|
-
Rack::Static.new(-> { response }, urls: ['/404.html'], root: 'public').call(env)
|
115
|
-
in [404 | 404, _, _] then response
|
116
|
-
else
|
117
|
-
response.is_a?(Array) ? response : response.finish
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
96
|
# Run the Application
|
122
97
|
#
|
123
98
|
# @returns [Base] self
|
124
99
|
#
|
125
100
|
# When you use this method, the application will be frozen. And you can't add more routes after that.
|
126
|
-
# This method is used to run the application in a Rack server so, you can use the `rackup` command
|
101
|
+
# This method is used to run the application in a Rack server so, you can use the `rackup` command
|
102
|
+
# to run the application.
|
127
103
|
# Ex. rackup -p 3000
|
128
104
|
# This command will use the following middleware:
|
129
105
|
# - Rack::ShowExceptions
|
@@ -143,34 +119,74 @@ class Lennarb
|
|
143
119
|
use Rack::ContentLength
|
144
120
|
use Rack::Static,
|
145
121
|
root: 'public',
|
146
|
-
urls: ['/404.html'],
|
122
|
+
urls: ['/404.html', '/500.html'],
|
147
123
|
header_rules: [
|
148
|
-
[200, %w[html], { 'content-type' => 'text/html; charset=utf-8' }]
|
124
|
+
[200, %w[html], { 'content-type' => 'text/html; charset=utf-8' }],
|
125
|
+
[400, %w[html], { 'content-type' => 'text/html; charset=utf-8' }],
|
126
|
+
[500, %w[html], { 'content-type' => 'text/html; charset=utf-8' }]
|
149
127
|
]
|
150
128
|
|
151
129
|
middlewares.each do |(middleware, args, block)|
|
152
130
|
stack.use(middleware, *args, &block)
|
153
131
|
end
|
154
132
|
|
155
|
-
stack.run
|
133
|
+
stack.run ->(env) do
|
134
|
+
catch(:halt) do
|
135
|
+
begin
|
136
|
+
execute_hooks(@_before_hooks, env, :before)
|
137
|
+
res = @_route.call(env)
|
138
|
+
execute_hooks(@_after_hooks, env, :after)
|
139
|
+
res
|
140
|
+
rescue StandardError => e
|
141
|
+
render_error if production?
|
142
|
+
|
143
|
+
puts e.message.red
|
144
|
+
puts e.backtrace
|
145
|
+
raise e
|
146
|
+
end.then do |response|
|
147
|
+
case response
|
148
|
+
in [400..499, _, _] then render_not_found
|
149
|
+
else response
|
150
|
+
end
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
156
154
|
|
157
155
|
@_route.freeze!
|
158
156
|
|
159
|
-
|
157
|
+
stack.to_app
|
158
|
+
end
|
159
|
+
|
160
|
+
def self.test? = ENV['RACK_ENV'] == 'test' || ENV['LENNARB_ENV'] == 'test'
|
161
|
+
def self.production? = ENV['RACK_ENV'] == 'production' || ENV['LENNARB_ENV'] == 'production'
|
162
|
+
def self.development? = ENV['RACK_ENV'] == 'development' || ENV['LENNARB_ENV'] == 'development'
|
163
|
+
|
164
|
+
# Render a not found
|
165
|
+
#
|
166
|
+
# @returns [void]
|
167
|
+
#
|
168
|
+
def self.render_not_found(content = nil)
|
169
|
+
default = File.exist?('public/404.html')
|
170
|
+
body = content || default || 'Not Found'
|
171
|
+
throw :halt, [404, { 'content-type' => 'text/html' }, [body]]
|
172
|
+
end
|
160
173
|
|
161
|
-
|
174
|
+
# Render an error
|
175
|
+
#
|
176
|
+
# @returns [void]
|
177
|
+
#
|
178
|
+
def self.render_error(content = nil)
|
179
|
+
default = File.exist?('public/500.html')
|
180
|
+
body = content || default || 'Internal Server Error'
|
181
|
+
throw :halt, [500, { 'content-type' => 'text/html' }, [body]]
|
162
182
|
end
|
163
183
|
|
164
184
|
# Redirect to a path
|
165
185
|
#
|
166
186
|
# @parameter [String] path
|
167
187
|
# @parameter [Integer] status default is 302
|
168
|
-
# @parameter [Hash] env (optional)
|
169
188
|
#
|
170
|
-
def self.redirect(path, status = 302,
|
171
|
-
response = Rack::Response.new([], status, 'location' => path)
|
172
|
-
throw :halt, response.finish
|
173
|
-
end
|
189
|
+
def self.redirect(path, status = 302) = throw :halt, [status, { 'location' => path }, []]
|
174
190
|
|
175
191
|
# Execute the hooks
|
176
192
|
#
|
data/lib/lennarb/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lennarb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Aristóteles Coutinho
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-05-
|
11
|
+
date: 2024-05-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: colorize
|
@@ -44,34 +44,6 @@ dependencies:
|
|
44
44
|
- - ">="
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: 3.0.8
|
47
|
-
- !ruby/object:Gem::Dependency
|
48
|
-
name: rack-protection
|
49
|
-
requirement: !ruby/object:Gem::Requirement
|
50
|
-
requirements:
|
51
|
-
- - "~>"
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: '4.0'
|
54
|
-
type: :runtime
|
55
|
-
prerelease: false
|
56
|
-
version_requirements: !ruby/object:Gem::Requirement
|
57
|
-
requirements:
|
58
|
-
- - "~>"
|
59
|
-
- !ruby/object:Gem::Version
|
60
|
-
version: '4.0'
|
61
|
-
- !ruby/object:Gem::Dependency
|
62
|
-
name: rack-session
|
63
|
-
requirement: !ruby/object:Gem::Requirement
|
64
|
-
requirements:
|
65
|
-
- - "~>"
|
66
|
-
- !ruby/object:Gem::Version
|
67
|
-
version: '2.0'
|
68
|
-
type: :runtime
|
69
|
-
prerelease: false
|
70
|
-
version_requirements: !ruby/object:Gem::Requirement
|
71
|
-
requirements:
|
72
|
-
- - "~>"
|
73
|
-
- !ruby/object:Gem::Version
|
74
|
-
version: '2.0'
|
75
47
|
- !ruby/object:Gem::Dependency
|
76
48
|
name: bake
|
77
49
|
requirement: !ruby/object:Gem::Requirement
|