This commit is contained in:
Sdogruyol 2016-03-19 15:15:25 +02:00
parent 664673f125
commit fd904cd98d
2 changed files with 33 additions and 25 deletions

View File

@ -1,34 +1,40 @@
require "./kemal/*" require "./kemal/*"
require "./kemal/middleware/*" require "./kemal/middleware/*"
at_exit do module Kemal
Kemal::CLI.new def self.run
config = Kemal.config Kemal::CLI.new
config.setup config = Kemal.config
config.add_handler Kemal::RouteHandler::INSTANCE config.setup
config.add_handler Kemal::RouteHandler::INSTANCE
server = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers) server = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers)
server.ssl = config.ssl server.ssl = config.ssl
Signal::INT.trap { Signal::INT.trap {
config.logger.write "Kemal is going to take a rest!\n" config.logger.write "Kemal is going to take a rest!\n"
server.close server.close
exit exit
} }
# This route serves the built-in images for not_found and exceptions. # This route serves the built-in images for not_found and exceptions.
get "/__kemal__/:image" do |env| get "/__kemal__/:image" do |env|
image = env.params.url["image"] image = env.params.url["image"]
file_path = File.expand_path("libs/kemal/images/#{image}", Dir.current) file_path = File.expand_path("libs/kemal/images/#{image}", Dir.current)
if File.exists? file_path if File.exists? file_path
env.response.headers.add "Content-Type", "application/octet-stream" env.response.headers.add "Content-Type", "application/octet-stream"
env.response.content_length = File.size(file_path) env.response.content_length = File.size(file_path)
File.open(file_path) do |file| File.open(file_path) do |file|
IO.copy(file, env.response) IO.copy(file, env.response)
end
end end
end end
end
config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n" config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
server.listen server.listen
end
end
at_exit do
Kemal.run if Kemal.config.run
end end

View File

@ -2,7 +2,8 @@ module Kemal
class Config class Config
INSTANCE = Config.new INSTANCE = Config.new
HANDLERS = [] of HTTP::Handler HANDLERS = [] of HTTP::Handler
property host_binding, ssl, port, env, public_folder, logging, always_rescue, error_handler, serve_static property host_binding, ssl, port, env, public_folder, logging,
always_rescue, error_handler, serve_static, run
def initialize def initialize
@host_binding = "0.0.0.0" @host_binding = "0.0.0.0"
@ -14,6 +15,7 @@ module Kemal
@logger = nil @logger = nil
@always_rescue = true @always_rescue = true
@error_handler = nil @error_handler = nil
@run = false
end end
def logger def logger