added configure serve_static

This commit is contained in:
Joris Moriau 2016-02-15 08:01:41 +01:00
parent 0f609f6718
commit ed769ad700
3 changed files with 21 additions and 8 deletions

View file

@ -4,9 +4,7 @@ require "./kemal/middleware/*"
at_exit do
Kemal::CLI.new
config = Kemal.config
config.setup_logging
config.setup_error_handler
config.add_handler Kemal::StaticFileHandler.new(config.public_folder)
config.setup
config.add_handler Kemal::RouteHandler::INSTANCE
server = HTTP::Server.new(config.host_binding.not_nil!.to_slice, config.port, config.handlers)
@ -30,7 +28,7 @@ at_exit do
end
end
end
config.logger.write "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
server.listen
end

View file

@ -2,12 +2,13 @@ module Kemal
class Config
INSTANCE = Config.new
HANDLERS = [] of HTTP::Handler
property host_binding, ssl, port, env, public_folder, logging, always_rescue, error_handler
property host_binding, ssl, port, env, public_folder, logging, always_rescue, error_handler, serve_static
def initialize
@host_binding = "0.0.0.0" unless @host_binding
@host_binding = "0.0.0.0"
@port = 3000
@env = "development" unless @env
@env = "development"
@serve_static = true
@public_folder = "./public"
@logging = true
@logger = nil
@ -39,6 +40,12 @@ module Kemal
HANDLERS << handler
end
def setup
setup_logging
setup_error_handler
setup_public_folder
end
def setup_logging
if @logging
@logger ||= Kemal::CommonLogHandler.new(@env)
@ -49,12 +56,16 @@ module Kemal
end
end
def setup_error_handler
private def setup_error_handler
if @always_rescue
@error_handler ||= Kemal::CommonErrorHandler::INSTANCE
HANDLERS << @error_handler.not_nil!
end
end
private def setup_public_folder
HANDLERS << Kemal::StaticFileHandler.new(@public_folder) if @serve_static
end
end
def self.config

View file

@ -46,3 +46,7 @@ def logger(logger)
Kemal.config.logger = logger
Kemal.config.add_handler logger
end
def serve_static(status)
Kemal.config.serve_static = status
end