2016-07-17 11:28:21 +00:00
|
|
|
require "http"
|
2015-10-23 19:48:30 +00:00
|
|
|
require "./kemal/*"
|
2016-07-10 10:03:09 +00:00
|
|
|
require "./kemal/helpers/*"
|
2015-12-27 09:59:07 +00:00
|
|
|
require "./kemal/middleware/*"
|
2014-06-11 23:41:02 +00:00
|
|
|
|
2016-03-19 13:15:25 +00:00
|
|
|
module Kemal
|
2016-04-18 18:40:48 +00:00
|
|
|
# The command to run a `Kemal` application.
|
2016-03-19 13:15:25 +00:00
|
|
|
def self.run
|
|
|
|
Kemal::CLI.new
|
|
|
|
config = Kemal.config
|
|
|
|
config.setup
|
|
|
|
config.add_handler Kemal::RouteHandler::INSTANCE
|
2015-11-16 21:55:02 +00:00
|
|
|
|
2016-04-09 14:20:39 +00:00
|
|
|
config.server = HTTP::Server.new(config.host_binding.not_nil!, config.port, config.handlers)
|
2016-06-14 21:18:00 +00:00
|
|
|
config.server.not_nil!.tls = config.ssl
|
2015-11-19 18:54:58 +00:00
|
|
|
|
2016-06-27 21:37:40 +00:00
|
|
|
Kemal::Sessions.run_reaper!
|
|
|
|
|
2016-05-15 11:58:09 +00:00
|
|
|
unless Kemal.config.error_handlers.has_key?(404)
|
|
|
|
error 404 do |env|
|
|
|
|
render_404
|
|
|
|
end
|
2016-05-05 19:35:36 +00:00
|
|
|
end
|
|
|
|
|
2016-04-18 18:40:48 +00:00
|
|
|
# Test environment doesn't need to have signal trap, built-in images, and logging.
|
2016-04-09 14:33:17 +00:00
|
|
|
unless config.env == "test"
|
|
|
|
Signal::INT.trap {
|
|
|
|
config.logger.write "Kemal is going to take a rest!\n"
|
2016-04-16 21:04:53 +00:00
|
|
|
config.server.not_nil!.close
|
2016-04-09 14:33:17 +00:00
|
|
|
exit
|
|
|
|
}
|
2015-11-19 18:54:58 +00:00
|
|
|
|
2016-04-09 14:33:17 +00:00
|
|
|
# This route serves the built-in images for not_found and exceptions.
|
|
|
|
get "/__kemal__/:image" do |env|
|
|
|
|
image = env.params.url["image"]
|
|
|
|
file_path = File.expand_path("libs/kemal/images/#{image}", Dir.current)
|
|
|
|
if File.exists? file_path
|
2016-07-19 18:01:49 +00:00
|
|
|
send_file env, file_path
|
2016-03-19 13:15:25 +00:00
|
|
|
end
|
2016-01-24 16:13:04 +00:00
|
|
|
end
|
2016-03-19 13:15:25 +00:00
|
|
|
|
2016-04-09 14:33:17 +00:00
|
|
|
config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
|
2016-04-16 21:04:53 +00:00
|
|
|
config.server.not_nil!.listen
|
2016-04-09 14:33:17 +00:00
|
|
|
end
|
2015-11-28 10:39:58 +00:00
|
|
|
end
|
2016-03-19 13:15:25 +00:00
|
|
|
end
|