Allow config.server to be lazily initiliazed. Fixes #396

This commit is contained in:
Serdar Dogruyol 2017-10-04 19:42:41 +03:00
parent 61caa077b7
commit ad692357d4
2 changed files with 10 additions and 10 deletions

View file

@ -14,7 +14,7 @@ module Kemal
end
end
# Overload of self.run without port - fixex #399
def self.run
def self.run
self.run(nil)
end
@ -32,11 +32,6 @@ module Kemal
config.setup
config.port = port if port
config.server = HTTP::Server.new(config.host_binding, config.port, config.handlers)
{% if !flag?(:without_openssl) %}
config.server.tls = config.ssl
{% end %}
unless Kemal.config.error_handlers.has_key?(404)
error 404 do |env|
render_404
@ -63,15 +58,21 @@ module Kemal
end
end
server = HTTP::Server.new(config.host_binding, config.port, config.handlers)
{% if !flag?(:without_openssl) %}
server.tls = config.ssl
{% end %}
config.server ||= server
config.running = true
yield config
config.server.listen if config.env != "test"
config.server.not_nil!.listen if config.env != "test" && config.server
end
def self.stop
if config.running
if config.server
config.server.close
config.server.not_nil!.close
config.running = false
else
raise "Kemal.config.server is not set. Please use Kemal.run to set the server."

View file

@ -20,7 +20,7 @@ module Kemal
getter custom_handler_position
property host_binding, ssl, port, env, public_folder, logging, running
property always_rescue, server, extra_options, shutdown_message
property always_rescue, server : HTTP::Server?, extra_options, shutdown_message
property serve_static : (Bool | Hash(String, Bool))
property static_headers : (HTTP::Server::Response, String, File::Stat -> Void)?
@ -34,7 +34,6 @@ module Kemal
@logger = nil
@error_handler = nil
@always_rescue = true
@server = uninitialized HTTP::Server
@router_included = false
@default_handlers_setup = false
@running = false