2016-07-17 11:28:21 +00:00
|
|
|
require "http"
|
2017-03-03 20:56:29 +00:00
|
|
|
require "json"
|
|
|
|
require "uri"
|
|
|
|
require "tempfile"
|
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.
|
2017-09-29 06:25:26 +00:00
|
|
|
def self.run(port : Int32?)
|
2017-01-15 18:41:07 +00:00
|
|
|
self.run port do
|
2017-03-12 12:37:23 +00:00
|
|
|
log "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}"
|
2017-01-15 18:41:07 +00:00
|
|
|
end
|
|
|
|
end
|
2017-10-04 16:51:14 +00:00
|
|
|
|
2017-10-06 11:53:53 +00:00
|
|
|
# Overload of `self.run` without port.
|
2017-10-04 16:42:41 +00:00
|
|
|
def self.run
|
2017-09-29 06:25:26 +00:00
|
|
|
self.run(nil)
|
|
|
|
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.
|
2017-01-15 18:41:07 +00:00
|
|
|
def self.run(&block)
|
2017-02-06 17:16:51 +00:00
|
|
|
self.run nil, &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`
|
2017-08-24 20:32:43 +00:00
|
|
|
def self.run(port : Int32? = nil, &block)
|
2016-03-19 13:15:25 +00:00
|
|
|
Kemal::CLI.new
|
|
|
|
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
|
|
|
|
2016-05-15 11:58:09 +00:00
|
|
|
unless Kemal.config.error_handlers.has_key?(404)
|
2018-05-17 08:07:40 +00:00
|
|
|
error 404 do
|
2016-05-15 11:58:09 +00:00
|
|
|
render_404
|
|
|
|
end
|
2016-05-05 19:35:36 +00:00
|
|
|
end
|
|
|
|
|
2016-04-18 18:40:48 +00:00
|
|
|
# Test environment doesn't need to have signal trap, built-in images, and logging.
|
2016-04-09 14:33:17 +00:00
|
|
|
unless config.env == "test"
|
2017-06-23 09:18:40 +00:00
|
|
|
Signal::INT.trap do
|
2017-03-12 12:37:23 +00:00
|
|
|
log "Kemal is going to take a rest!" if config.shutdown_message
|
2016-12-08 10:50:54 +00:00
|
|
|
Kemal.stop
|
2016-04-09 14:33:17 +00:00
|
|
|
exit
|
2017-06-23 09:18:40 +00:00
|
|
|
end
|
2015-11-19 18:54:58 +00:00
|
|
|
|
2016-04-09 14:33:17 +00:00
|
|
|
# This route serves the built-in images for not_found and exceptions.
|
2018-03-19 19:44:24 +00:00
|
|
|
get "/__kemal__/404.png" do |env|
|
|
|
|
file_path = File.expand_path("lib/kemal/images/404.png", Dir.current)
|
2018-05-17 08:07:40 +00:00
|
|
|
|
2016-04-09 14:33:17 +00:00
|
|
|
if File.exists? file_path
|
2016-07-19 18:01:49 +00:00
|
|
|
send_file env, file_path
|
2016-11-25 19:19:23 +00:00
|
|
|
else
|
|
|
|
halt env, 404
|
2016-03-19 13:15:25 +00:00
|
|
|
end
|
2016-01-24 16:13:04 +00:00
|
|
|
end
|
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-10-04 16:42:41 +00:00
|
|
|
{% if !flag?(:without_openssl) %}
|
2017-10-06 11:53:53 +00:00
|
|
|
config.server.not_nil!.tls = config.ssl
|
2017-10-04 16:42:41 +00:00
|
|
|
{% end %}
|
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-16 15:03:00 +00:00
|
|
|
server.listen(config.host_binding, config.port) if config.env != "test"
|
2015-11-28 10:39:58 +00:00
|
|
|
end
|
2016-12-08 10:50:54 +00:00
|
|
|
|
|
|
|
def self.stop
|
|
|
|
if config.running
|
2018-06-16 15:03:00 +00:00
|
|
|
if server = config.server
|
|
|
|
server.close unless server.closed?
|
2016-12-08 10:50:54 +00:00
|
|
|
config.running = false
|
|
|
|
else
|
2016-12-08 19:15:18 +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
|
|
|
|
else
|
|
|
|
raise "Kemal is already stopped."
|
|
|
|
end
|
|
|
|
end
|
2016-03-19 13:15:25 +00:00
|
|
|
end
|