2015-10-23 19:48:30 +00:00
|
|
|
require "./kemal/*"
|
2015-12-27 09:59:07 +00:00
|
|
|
require "./kemal/middleware/*"
|
2014-06-11 23:41:02 +00:00
|
|
|
|
2014-07-30 22:17:53 +00:00
|
|
|
at_exit do
|
2016-01-13 20:22:34 +00:00
|
|
|
Kemal::CLI.new
|
2015-10-23 18:33:26 +00:00
|
|
|
config = Kemal.config
|
2016-01-17 11:08:12 +00:00
|
|
|
if config.logging
|
|
|
|
logger = Kemal::Logger.new
|
2016-01-17 12:17:27 +00:00
|
|
|
config.logger = logger
|
|
|
|
config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
|
2016-01-17 11:08:12 +00:00
|
|
|
end
|
2015-12-12 18:27:46 +00:00
|
|
|
config.add_handler Kemal::StaticFileHandler.new(config.public_folder)
|
2015-11-12 20:48:22 +00:00
|
|
|
config.add_handler Kemal::Handler::INSTANCE
|
2015-11-16 21:55:02 +00:00
|
|
|
|
2016-01-03 14:04:11 +00:00
|
|
|
server = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers)
|
2014-07-30 22:17:53 +00:00
|
|
|
server.ssl = config.ssl
|
2015-11-19 18:54:58 +00:00
|
|
|
|
|
|
|
Signal::INT.trap {
|
2016-01-18 15:25:52 +00:00
|
|
|
if config.logging
|
|
|
|
config.logger.write "Kemal is going to take a rest!\n"
|
|
|
|
config.logger.handler.close
|
|
|
|
end
|
2015-11-19 18:54:58 +00:00
|
|
|
server.close
|
|
|
|
exit
|
|
|
|
}
|
|
|
|
|
2015-11-28 10:39:58 +00:00
|
|
|
# This route serves the built-in images for not_found and exceptions.
|
|
|
|
get "/__kemal__/:image" do |env|
|
|
|
|
image = env.params["image"]
|
2015-12-24 15:25:00 +00:00
|
|
|
file_path = File.expand_path("libs/kemal/images/#{image}", Dir.current)
|
2016-01-24 16:13:04 +00:00
|
|
|
if File.exists? file_path
|
|
|
|
env.response.headers.add "Content-Type", "application/octet-stream"
|
|
|
|
env.response.content_length = File.size(file_path)
|
|
|
|
File.open(file_path) do |file|
|
|
|
|
IO.copy(file, env.response)
|
|
|
|
end
|
|
|
|
end
|
2015-11-28 10:39:58 +00:00
|
|
|
end
|
|
|
|
|
2015-12-19 08:00:29 +00:00
|
|
|
server.listen
|
2014-06-11 23:41:02 +00:00
|
|
|
end
|