2016-07-17 11:28:21 +00:00
|
|
|
require "http"
|
2017-03-03 20:56:29 +00:00
|
|
|
require "json"
|
|
|
|
require "uri"
|
2015-10-23 19:48:30 +00:00
|
|
|
require "./kemal/*"
|
2017-03-03 21:01:26 +00:00
|
|
|
require "./kemal/ext/*"
|
2016-07-10 10:03:09 +00:00
|
|
|
require "./kemal/helpers/*"
|
2014-06-11 23:41:02 +00:00
|
|
|
|
2016-03-19 13:15:25 +00:00
|
|
|
module Kemal
|
2017-10-06 11:53:53 +00:00
|
|
|
# Overload of `self.run` with the default startup logging.
|
2018-11-30 16:16:08 +00:00
|
|
|
def self.run(port : Int32?, args = ARGV)
|
|
|
|
self.run(port, args) { }
|
2017-01-15 18:41:07 +00:00
|
|
|
end
|
2017-10-04 16:51:14 +00:00
|
|
|
|
2017-10-06 11:53:53 +00:00
|
|
|
# Overload of `self.run` without port.
|
2018-11-30 16:16:08 +00:00
|
|
|
def self.run(args = ARGV)
|
|
|
|
self.run(nil, args: args)
|
2017-09-29 06:25:26 +00:00
|
|
|
end
|
2017-01-15 18:41:07 +00:00
|
|
|
|
2017-10-06 11:53:53 +00:00
|
|
|
# Overload of `self.run` to allow just a block.
|
2018-11-30 16:16:08 +00:00
|
|
|
def self.run(args = ARGV, &block)
|
|
|
|
self.run(nil, args: args, &block)
|
2017-01-15 18:41:07 +00:00
|
|
|
end
|
|
|
|
|
2016-04-18 18:40:48 +00:00
|
|
|
# The command to run a `Kemal` application.
|
2017-10-06 11:53:53 +00:00
|
|
|
#
|
|
|
|
# If *port* is not given Kemal will use `Kemal::Config#port`
|
2018-11-30 16:16:08 +00:00
|
|
|
#
|
2019-02-05 08:23:20 +00:00
|
|
|
# To use custom command line arguments, set args to nil
|
2018-11-30 16:16:08 +00:00
|
|
|
#
|
|
|
|
def self.run(port : Int32? = nil, args = ARGV, &block)
|
|
|
|
Kemal::CLI.new args
|
2016-03-19 13:15:25 +00:00
|
|
|
config = Kemal.config
|
|
|
|
config.setup
|
2016-09-20 23:27:35 +00:00
|
|
|
config.port = port if port
|
2015-11-16 21:55:02 +00:00
|
|
|
|
2018-06-30 11:49:39 +00:00
|
|
|
# Test environment doesn't need to have signal trap and logging.
|
2018-06-30 11:34:41 +00:00
|
|
|
if config.env != "test"
|
|
|
|
setup_404
|
|
|
|
setup_trap_signal
|
2016-04-09 14:33:17 +00:00
|
|
|
end
|
2017-01-15 18:41:07 +00:00
|
|
|
|
2018-06-16 15:03:00 +00:00
|
|
|
server = config.server ||= HTTP::Server.new(config.handlers)
|
2018-03-17 14:58:19 +00:00
|
|
|
|
2017-01-15 18:41:07 +00:00
|
|
|
config.running = true
|
2017-10-04 16:42:41 +00:00
|
|
|
|
2017-01-15 18:41:07 +00:00
|
|
|
yield config
|
2018-06-30 11:16:55 +00:00
|
|
|
|
|
|
|
# Abort if block called `Kemal.stop`
|
|
|
|
return unless config.running
|
|
|
|
|
|
|
|
unless server.each_address { |_| break true }
|
2018-08-08 21:13:45 +00:00
|
|
|
{% if flag?(:without_openssl) %}
|
|
|
|
server.bind_tcp(config.host_binding, config.port)
|
|
|
|
{% else %}
|
|
|
|
if ssl = config.ssl
|
2018-09-20 08:15:05 +00:00
|
|
|
server.bind_tls(config.host_binding, config.port, ssl)
|
2018-08-08 21:13:45 +00:00
|
|
|
else
|
|
|
|
server.bind_tcp(config.host_binding, config.port)
|
|
|
|
end
|
|
|
|
{% end %}
|
2018-06-30 11:16:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
display_startup_message(config, server)
|
|
|
|
|
|
|
|
server.listen unless config.env == "test"
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.display_startup_message(config, server)
|
2021-03-15 05:45:35 +00:00
|
|
|
addresses = server.addresses.join ", " { |address| "#{config.scheme}://#{address}" }
|
2021-04-05 15:05:06 +00:00
|
|
|
log "[#{config.env}] #{config.app_name} is ready to lead at #{addresses}"
|
2015-11-28 10:39:58 +00:00
|
|
|
end
|
2016-12-08 10:50:54 +00:00
|
|
|
|
|
|
|
def self.stop
|
2021-04-05 15:05:06 +00:00
|
|
|
raise "#{Kemal.config.app_name} is already stopped." if !config.running
|
2018-11-01 11:29:05 +00:00
|
|
|
if server = config.server
|
|
|
|
server.close unless server.closed?
|
|
|
|
config.running = false
|
2016-12-08 10:50:54 +00:00
|
|
|
else
|
2018-11-01 11:29:05 +00:00
|
|
|
raise "Kemal.config.server is not set. Please use Kemal.run to set the server."
|
2016-12-08 10:50:54 +00:00
|
|
|
end
|
|
|
|
end
|
2018-06-30 11:34:41 +00:00
|
|
|
|
|
|
|
private def self.setup_404
|
|
|
|
unless Kemal.config.error_handlers.has_key?(404)
|
|
|
|
error 404 do
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private def self.setup_trap_signal
|
|
|
|
Signal::INT.trap do
|
2021-04-05 15:05:06 +00:00
|
|
|
log "#{Kemal.config.app_name} is going to take a rest!" if Kemal.config.shutdown_message
|
2018-06-30 11:34:41 +00:00
|
|
|
Kemal.stop
|
|
|
|
exit
|
|
|
|
end
|
|
|
|
end
|
2016-03-19 13:15:25 +00:00
|
|
|
end
|