Tidy up Kemal.run
This commit is contained in:
parent
bc661d58ae
commit
621342170a
1 changed files with 29 additions and 23 deletions
52
src/kemal.cr
52
src/kemal.cr
|
@ -31,30 +31,10 @@ module Kemal
|
||||||
config.setup
|
config.setup
|
||||||
config.port = port if port
|
config.port = port if port
|
||||||
|
|
||||||
unless Kemal.config.error_handlers.has_key?(404)
|
|
||||||
error 404 do
|
|
||||||
render_404
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
# Test environment doesn't need to have signal trap, built-in images, and logging.
|
# Test environment doesn't need to have signal trap, built-in images, and logging.
|
||||||
unless config.env == "test"
|
if config.env != "test"
|
||||||
Signal::INT.trap do
|
setup_404
|
||||||
log "Kemal is going to take a rest!" if config.shutdown_message
|
setup_trap_signal
|
||||||
Kemal.stop
|
|
||||||
exit
|
|
||||||
end
|
|
||||||
|
|
||||||
# This route serves the built-in images for not_found and exceptions.
|
|
||||||
get "/__kemal__/404.png" do |env|
|
|
||||||
file_path = File.expand_path("lib/kemal/images/404.png", Dir.current)
|
|
||||||
|
|
||||||
if File.exists? file_path
|
|
||||||
send_file env, file_path
|
|
||||||
else
|
|
||||||
halt env, 404
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
server = config.server ||= HTTP::Server.new(config.handlers)
|
server = config.server ||= HTTP::Server.new(config.handlers)
|
||||||
|
@ -96,4 +76,30 @@ module Kemal
|
||||||
raise "Kemal is already stopped."
|
raise "Kemal is already stopped."
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def self.setup_404
|
||||||
|
unless Kemal.config.error_handlers.has_key?(404)
|
||||||
|
get "/__kemal__/404.png" do |env|
|
||||||
|
file_path = File.expand_path("lib/kemal/images/404.png", Dir.current)
|
||||||
|
|
||||||
|
if File.exists? file_path
|
||||||
|
send_file env, file_path
|
||||||
|
else
|
||||||
|
halt env, 404
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
error 404 do
|
||||||
|
render_404
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private def self.setup_trap_signal
|
||||||
|
Signal::INT.trap do
|
||||||
|
log "Kemal is going to take a rest!" if Kemal.config.shutdown_message
|
||||||
|
Kemal.stop
|
||||||
|
exit
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue