Skip to content

Commit 22ba29c

Browse files
committed
Update the application module, I dislike the syntax of inheritance
1 parent 0a3c69e commit 22ba29c

File tree

5 files changed

+122
-145
lines changed

5 files changed

+122
-145
lines changed

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,21 +69,21 @@ class IndexController
6969
end
7070
end
7171
72-
class Application < Grip::Application
72+
class Application
73+
include Grip::Application
74+
7375
def initialize
76+
# You can include as many handlers as you want,
77+
# for now we will just log the requests and provide an HTTP handler.
78+
property handlers : Array(HTTP::Handler) = [
79+
Grip::Handlers::Log.new,
80+
Grip::Handlers::HTTP.new
81+
] of HTTP::Handler
82+
7483
# By default the environment is set to "development".
75-
super(
76-
environment: ENV["ENVIRONMENT"]? || "production"
77-
handlers: [
78-
Grip::Handlers::Log.new,
79-
Grip::Handlers::HTTP.new
80-
] of HTTP::Handler
81-
)
82-
83-
routes()
84-
end
84+
property environment : String =
85+
ENV["ENVIRONMENT"]? || "production"
8586
86-
def routes()
8787
scope "/api" do
8888
scope "/v1" do
8989
get "/", IndexController

shard.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: grip
2-
version: 4.0.0
2+
version: 5.0.0
33

44
authors:
55
- Grip and its Contributors <https://github.com/grip-framework/>

spec/spec_helper.cr

Lines changed: 33 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,55 +12,52 @@ class ErrorController
1212
end
1313
end
1414

15-
class ErrorApplication < Grip::Application
16-
def initialize
17-
super(
18-
environment: "test",
19-
handlers: [
20-
Grip::Handlers::Exception.new,
21-
] of HTTP::Handler
22-
)
15+
class ErrorApplication
16+
include Grip::Application
2317

24-
exception Grip::Exceptions::NotFound, ErrorController
25-
end
18+
property handlers : Array(HTTP::Handler) = [
19+
Grip::Handlers::Exception.new
20+
] of HTTP::Handler
21+
22+
property environment : String = "test"
23+
property host : String = "0.0.0.0"
24+
property port : Int32 = 0
2625

27-
def port
28-
0
26+
def initialize
27+
exception Grip::Exceptions::NotFound, ErrorController
2928
end
3029
end
3130

32-
class HttpApplication < Grip::Application
33-
def initialize
34-
super(
35-
environment: "test",
36-
handlers: [
37-
Grip::Handlers::HTTP.new,
38-
] of HTTP::Handler
39-
)
31+
class HttpApplication
32+
include Grip::Application
33+
34+
property handlers : Array(HTTP::Handler) = [
35+
Grip::Handlers::HTTP.new
36+
] of HTTP::Handler
37+
38+
property environment : String = "test"
39+
property host : String = "0.0.0.0"
40+
property port : Int32 = 0
4041

42+
def initialize
4143
get "/", ExampleController
4244
get "/:id", ExampleController, as: :index
4345
end
44-
45-
def port
46-
0
47-
end
4846
end
4947

50-
class WebSocketApplication < Grip::Application
51-
def initialize
52-
super(
53-
environment: "test",
54-
handlers: [
55-
Grip::Handlers::WebSocket.new,
56-
] of HTTP::Handler
57-
)
48+
class WebSocketApplication
49+
include Grip::Application
5850

59-
ws "/", MatchController
60-
end
51+
property handlers : Array(HTTP::Handler) = [
52+
Grip::Handlers::WebSocket.new
53+
] of HTTP::Handler
54+
55+
property environment : String = "test"
56+
property host : String = "0.0.0.0"
57+
property port : Int32 = 0
6158

62-
def port
63-
0
59+
def initialize
60+
ws "/", MatchController
6461
end
6562
end
6663

src/grip/application.cr

Lines changed: 73 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,98 @@
11
module Grip
22
# `Grip::Application` is a building class which initializes the crucial parts of the
33
# web-framework.
4-
class Application
5-
include Grip::Macros::Dsl
4+
module Application
5+
macro included
6+
include Grip::Macros::Dsl
67

7-
DEFAULT_HOST = "0.0.0.0"
8-
DEFAULT_PORT = 4004
9-
DEFAULT_ENVIRONMENT = "development"
10-
DEFAULT_REUSE_PORT = false
8+
DEFAULT_HOST = "0.0.0.0"
9+
DEFAULT_PORT = 4004
10+
DEFAULT_ENVIRONMENT = "development"
11+
DEFAULT_REUSE_PORT = false
1112

12-
property host : String
13-
property port : Int32
14-
property environment : String
15-
property? reuse_port : Bool
13+
property environment : String = DEFAULT_ENVIRONMENT
14+
property host : String = DEFAULT_HOST
15+
property port : Int32 = DEFAULT_PORT
16+
property? reuse_port : Bool = DEFAULT_REUSE_PORT
1617

17-
getter scopes : Array(String) = [] of String
18-
getter valves : Array(Symbol) = [] of Symbol
19-
getter valve : Symbol? = nil
18+
getter scopes : Array(String) = [] of String
19+
getter valves : Array(Symbol) = [] of Symbol
20+
getter valve : Symbol? = nil
2021

21-
getter handlers : Array(HTTP::Handler) = [] of HTTP::Handler
22+
getter handlers : Array(HTTP::Handler) = [] of HTTP::Handler
2223

23-
def initialize(
24-
@environment : String = DEFAULT_ENVIRONMENT,
25-
@host : String = DEFAULT_HOST,
26-
@port : Int32 = DEFAULT_PORT,
27-
@reuse_port : Bool = DEFAULT_REUSE_PORT,
28-
@handlers : Array(HTTP::Handler) = [] of HTTP::Handler
29-
)
30-
exception_handler = @handlers.find { |handler| handler.is_a?(Grip::Handlers::Exception) }
31-
32-
if exception_handler
33-
exception_handler
34-
.as(Grip::Handlers::Exception)
35-
.environment = @environment
24+
# SSL/TLS configuration
25+
def key_file : String
26+
ENV["KEY"]? || ""
3627
end
37-
end
3828

39-
# SSL/TLS configuration
40-
def key_file : String
41-
ENV["KEY"]? || ""
42-
end
29+
def cert_file : String
30+
ENV["CERTIFICATE"]? || ""
31+
end
4332

44-
def cert_file : String
45-
ENV["CERTIFICATE"]? || ""
46-
end
33+
{% unless flag?(:ssl) %}
34+
def ssl : Bool
35+
false
36+
end
37+
{% else %}
38+
def ssl : OpenSSL::SSL::Context::Server?
39+
return nil if key_file.empty? || cert_file.empty?
40+
41+
context = OpenSSL::SSL::Context::Server.new
42+
context.private_key = key_file
43+
context.certificate_chain = cert_file
44+
context
45+
end
46+
{% end %}
4747

48-
{% unless flag?(:ssl) %}
49-
def ssl : Bool
50-
false
48+
def scheme : String
49+
ssl ? "https" : "http"
5150
end
52-
{% else %}
53-
def ssl : OpenSSL::SSL::Context::Server?
54-
return nil if key_file.empty? || cert_file.empty?
5551

56-
context = OpenSSL::SSL::Context::Server.new
57-
context.private_key = key_file
58-
context.certificate_chain = cert_file
59-
context
52+
# Server setup and running
53+
def server : HTTP::Server
54+
HTTP::Server.new(@handlers)
6055
end
61-
{% end %}
62-
63-
def scheme : String
64-
ssl ? "https" : "http"
65-
end
66-
67-
# Server setup and running
68-
def server : HTTP::Server
69-
HTTP::Server.new(@handlers)
70-
end
7156

72-
def run
73-
server_instance = server
74-
bind_server(server_instance)
57+
def run
58+
server_instance = server
59+
bind_server(server_instance)
7560

76-
Log.info { "Listening at #{scheme}://#{host}:#{port}" }
77-
setup_signal_handling unless environment == "test"
78-
server_instance.listen unless environment == "test"
79-
end
61+
Log.info { "Listening at #{scheme}://#{host}:#{port}" }
62+
setup_signal_handling unless environment == "test"
63+
server_instance.listen unless environment == "test"
64+
end
8065

81-
private def bind_server(server : HTTP::Server)
82-
unless server.each_address { |_| break true }
83-
{% if flag?(:ssl) %}
84-
if ssl_context = ssl
85-
server.bind_tls(host, port, ssl_context, reuse_port?)
86-
else
66+
private def bind_server(server : HTTP::Server)
67+
unless server.each_address { |_| break true }
68+
{% if flag?(:ssl) %}
69+
if ssl_context = ssl
70+
server.bind_tls(host, port, ssl_context, reuse_port?)
71+
else
72+
server.bind_tcp(host, port, reuse_port?)
73+
end
74+
{% else %}
8775
server.bind_tcp(host, port, reuse_port?)
88-
end
89-
{% else %}
90-
server.bind_tcp(host, port, reuse_port?)
91-
{% end %}
76+
{% end %}
77+
end
9278
end
93-
end
94-
95-
private def setup_signal_handling
96-
{% begin %}
97-
{% version = Crystal::VERSION.gsub(/[^0-9.]/, "").split(".").map(&.to_i) %}
98-
{% major = version[0] %}
99-
{% minor = version[1] %}
10079

101-
# Crystal version-specific signal handling
102-
{% if major < 1 %}
103-
Signal::INT.trap { exit }
104-
{% elsif major == 1 && minor < 12 %}
105-
Process.on_interrupt { exit }
106-
{% else %}
107-
Process.on_terminate { exit }
80+
private def setup_signal_handling
81+
{% begin %}
82+
{% version = Crystal::VERSION.gsub(/[^0-9.]/, "").split(".").map(&.to_i) %}
83+
{% major = version[0] %}
84+
{% minor = version[1] %}
85+
86+
# Crystal version-specific signal handling
87+
{% if major < 1 %}
88+
Signal::INT.trap { exit }
89+
{% elsif major == 1 && minor < 12 %}
90+
Process.on_interrupt { exit }
91+
{% else %}
92+
Process.on_terminate { exit }
93+
{% end %}
10894
{% end %}
109-
{% end %}
95+
end
11096
end
11197
end
11298
end

src/grip/handlers/exception.cr

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ module Grip
33
class Exception < Base
44
alias ExceptionHandler = ::HTTP::Handler
55

6-
property environment : String = Grip::Application::DEFAULT_ENVIRONMENT
76
property handlers : Hash(String, ExceptionHandler)
87

98
def initialize
@@ -78,14 +77,9 @@ module Grip
7877
exception : ::Exception,
7978
status_code : Int32
8079
) : ::HTTP::Server::Context
81-
if environment == "development"
82-
context.response.status_code = status_code.clamp(400, 599)
83-
context.response.headers.merge!({"Content-Type" => "text/html; charset=UTF-8"})
84-
context.response.print(Grip::Minuscule::ExceptionPage.new(context, exception))
85-
else
86-
context.response.status_code = 500
87-
context.response.print("500 Internal Server Error")
88-
end
80+
context.response.status_code = status_code.clamp(400, 599)
81+
context.response.headers.merge!({"Content-Type" => "text/html; charset=UTF-8"})
82+
context.response.print(Grip::Minuscule::ExceptionPage.new(context, exception))
8983

9084
context.response.close
9185
context

0 commit comments

Comments
 (0)