|
1 | 1 | module Grip |
2 | 2 | # `Grip::Application` is a building class which initializes the crucial parts of the |
3 | 3 | # web-framework. |
4 | | - class Application |
5 | | - include Grip::Macros::Dsl |
| 4 | + module Application |
| 5 | + macro included |
| 6 | + include Grip::Macros::Dsl |
6 | 7 |
|
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 |
11 | 12 |
|
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 |
16 | 17 |
|
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 |
20 | 21 |
|
21 | | - getter handlers : Array(HTTP::Handler) = [] of HTTP::Handler |
| 22 | + getter handlers : Array(HTTP::Handler) = [] of HTTP::Handler |
22 | 23 |
|
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"]? || "" |
36 | 27 | end |
37 | | - end |
38 | 28 |
|
39 | | - # SSL/TLS configuration |
40 | | - def key_file : String |
41 | | - ENV["KEY"]? || "" |
42 | | - end |
| 29 | + def cert_file : String |
| 30 | + ENV["CERTIFICATE"]? || "" |
| 31 | + end |
43 | 32 |
|
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 %} |
47 | 47 |
|
48 | | - {% unless flag?(:ssl) %} |
49 | | - def ssl : Bool |
50 | | - false |
| 48 | + def scheme : String |
| 49 | + ssl ? "https" : "http" |
51 | 50 | end |
52 | | - {% else %} |
53 | | - def ssl : OpenSSL::SSL::Context::Server? |
54 | | - return nil if key_file.empty? || cert_file.empty? |
55 | 51 |
|
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) |
60 | 55 | 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 |
71 | 56 |
|
72 | | - def run |
73 | | - server_instance = server |
74 | | - bind_server(server_instance) |
| 57 | + def run |
| 58 | + server_instance = server |
| 59 | + bind_server(server_instance) |
75 | 60 |
|
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 |
80 | 65 |
|
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 %} |
87 | 75 | 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 |
92 | 78 | 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] %} |
100 | 79 |
|
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 %} |
108 | 94 | {% end %} |
109 | | - {% end %} |
| 95 | + end |
110 | 96 | end |
111 | 97 | end |
112 | 98 | end |
0 commit comments