added configure serve_static
This commit is contained in:
parent
0f609f6718
commit
ed769ad700
3 changed files with 21 additions and 8 deletions
|
@ -4,9 +4,7 @@ require "./kemal/middleware/*"
|
||||||
at_exit do
|
at_exit do
|
||||||
Kemal::CLI.new
|
Kemal::CLI.new
|
||||||
config = Kemal.config
|
config = Kemal.config
|
||||||
config.setup_logging
|
config.setup
|
||||||
config.setup_error_handler
|
|
||||||
config.add_handler Kemal::StaticFileHandler.new(config.public_folder)
|
|
||||||
config.add_handler Kemal::RouteHandler::INSTANCE
|
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)
|
||||||
|
@ -30,7 +28,7 @@ at_exit do
|
||||||
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
|
||||||
|
|
|
@ -2,12 +2,13 @@ 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
|
property host_binding, ssl, port, env, public_folder, logging, always_rescue, error_handler, serve_static
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@host_binding = "0.0.0.0" unless @host_binding
|
@host_binding = "0.0.0.0"
|
||||||
@port = 3000
|
@port = 3000
|
||||||
@env = "development" unless @env
|
@env = "development"
|
||||||
|
@serve_static = true
|
||||||
@public_folder = "./public"
|
@public_folder = "./public"
|
||||||
@logging = true
|
@logging = true
|
||||||
@logger = nil
|
@logger = nil
|
||||||
|
@ -39,6 +40,12 @@ module Kemal
|
||||||
HANDLERS << handler
|
HANDLERS << handler
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def setup
|
||||||
|
setup_logging
|
||||||
|
setup_error_handler
|
||||||
|
setup_public_folder
|
||||||
|
end
|
||||||
|
|
||||||
def setup_logging
|
def setup_logging
|
||||||
if @logging
|
if @logging
|
||||||
@logger ||= Kemal::CommonLogHandler.new(@env)
|
@logger ||= Kemal::CommonLogHandler.new(@env)
|
||||||
|
@ -49,12 +56,16 @@ module Kemal
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def setup_error_handler
|
private def setup_error_handler
|
||||||
if @always_rescue
|
if @always_rescue
|
||||||
@error_handler ||= Kemal::CommonErrorHandler::INSTANCE
|
@error_handler ||= Kemal::CommonErrorHandler::INSTANCE
|
||||||
HANDLERS << @error_handler.not_nil!
|
HANDLERS << @error_handler.not_nil!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private def setup_public_folder
|
||||||
|
HANDLERS << Kemal::StaticFileHandler.new(@public_folder) if @serve_static
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.config
|
def self.config
|
||||||
|
|
|
@ -46,3 +46,7 @@ def logger(logger)
|
||||||
Kemal.config.logger = logger
|
Kemal.config.logger = logger
|
||||||
Kemal.config.add_handler logger
|
Kemal.config.add_handler logger
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def serve_static(status)
|
||||||
|
Kemal.config.serve_static = status
|
||||||
|
end
|
||||||
|
|
Loading…
Reference in a new issue