mirror of
https://gitea.invidious.io/iv-org/shard-kemal.git
synced 2024-08-15 00:53:36 +00:00
Extract default app behavior from Kemal::Base to Kemal::Application
This commit is contained in:
parent
1cd329b92f
commit
f5c80c7b67
5 changed files with 87 additions and 41 deletions
|
@ -27,11 +27,18 @@ describe "Config" do
|
|||
config.host_binding.should eq "127.0.0.1"
|
||||
end
|
||||
|
||||
it "adds a custom handler" do
|
||||
it "adds a custom handler to Base" do
|
||||
application = Kemal::Base.new
|
||||
application.add_handler CustomTestHandler.new
|
||||
application.setup
|
||||
application.handlers.size.should eq(8)
|
||||
application.handlers.size.should eq 6
|
||||
end
|
||||
|
||||
it "adds a custom handler to Application" do
|
||||
application = Kemal::Application.new
|
||||
application.add_handler CustomTestHandler.new
|
||||
application.setup
|
||||
application.handlers.size.should eq 9
|
||||
end
|
||||
|
||||
it "toggles the shutdown message" do
|
||||
|
|
37
src/kemal/application.cr
Normal file
37
src/kemal/application.cr
Normal file
|
@ -0,0 +1,37 @@
|
|||
class Kemal::Application < Kemal::Base
|
||||
def initialize(config = Config.default)
|
||||
super config
|
||||
add_filter_handler(filter_handler)
|
||||
end
|
||||
|
||||
# Overload of self.run with the default startup logging
|
||||
def run(port = nil)
|
||||
run port do
|
||||
log "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{port || config.port}"
|
||||
end
|
||||
end
|
||||
|
||||
private def prepare_for_server_start
|
||||
super
|
||||
|
||||
unless error_handlers.has_key?(404)
|
||||
error 404 do |env|
|
||||
render_404
|
||||
end
|
||||
end
|
||||
|
||||
# Test environment doesn't need to have signal trap, built-in images, and logging.
|
||||
unless @config.env == "test"
|
||||
# This route serves the built-in images for not_found and exceptions.
|
||||
get "/__kemal__/:image" do |env|
|
||||
image = env.params.url["image"]
|
||||
file_path = File.expand_path("lib/kemal/images/#{image}", Dir.current)
|
||||
if File.exists? file_path
|
||||
send_file env, file_path
|
||||
else
|
||||
halt env, 404
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -30,7 +30,7 @@ class Kemal::Base
|
|||
property! server : HTTP::Server
|
||||
property? running = false
|
||||
|
||||
def initialize(@config = Config.new)
|
||||
def initialize(@config = Config.base)
|
||||
@logger = if @config.logging?
|
||||
Kemal::LogHandler.new
|
||||
else
|
||||
|
@ -161,61 +161,47 @@ class Kemal::Base
|
|||
end
|
||||
|
||||
# Overload of self.run with the default startup logging
|
||||
def run(port = nil)
|
||||
def run(port : Int32? = nil)
|
||||
run port do
|
||||
log "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}"
|
||||
end
|
||||
end
|
||||
|
||||
# Overload of self.run to allow just a block
|
||||
def run(&block)
|
||||
run nil, &block
|
||||
end
|
||||
|
||||
# The command to run a `Kemal` application.
|
||||
# The port can be given to `#run` but is optional.
|
||||
# If not given Kemal will use `Kemal::Config#port`
|
||||
def run(port = nil, &block)
|
||||
@config.port = port if port
|
||||
|
||||
def run(port : Int32? = nil)
|
||||
setup
|
||||
|
||||
@server = server = HTTP::Server.new(@config.host_binding, @config.port, @handlers)
|
||||
prepare_for_server_start
|
||||
|
||||
start_server(port) do
|
||||
yield self
|
||||
end
|
||||
end
|
||||
|
||||
private def prepare_for_server_start
|
||||
unless @config.env == "test"
|
||||
Signal::INT.trap do
|
||||
log "Kemal is going to take a rest!" if @config.shutdown_message?
|
||||
stop if running?
|
||||
exit
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
private def start_server(port)
|
||||
@server = server = HTTP::Server.new(@config.host_binding, port || @config.port, @handlers)
|
||||
{% if !flag?(:without_openssl) %}
|
||||
server.tls = config.ssl
|
||||
{% end %}
|
||||
|
||||
unless error_handlers.has_key?(404)
|
||||
error 404 do |env|
|
||||
render_404
|
||||
end
|
||||
end
|
||||
|
||||
# Test environment doesn't need to have signal trap, built-in images, and logging.
|
||||
unless config.env == "test"
|
||||
Signal::INT.trap do
|
||||
log "Kemal is going to take a rest!" if config.shutdown_message?
|
||||
Kemal.stop if running?
|
||||
exit
|
||||
end
|
||||
|
||||
# This route serves the built-in images for not_found and exceptions.
|
||||
get "/__kemal__/:image" do |env|
|
||||
image = env.params.url["image"]
|
||||
file_path = File.expand_path("lib/kemal/images/#{image}", Dir.current)
|
||||
if File.exists? file_path
|
||||
send_file env, file_path
|
||||
else
|
||||
halt env, 404
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
server.bind
|
||||
@running = true
|
||||
|
||||
yield self
|
||||
yield
|
||||
|
||||
server.listen if @config.env != "test"
|
||||
server.listen unless @config.env == "test"
|
||||
end
|
||||
|
||||
def stop
|
||||
|
|
|
@ -57,5 +57,20 @@ module Kemal
|
|||
|
||||
def extra_options(&@extra_options : OptionParser ->)
|
||||
end
|
||||
|
||||
# Create a config with default values
|
||||
def self.default
|
||||
new
|
||||
end
|
||||
|
||||
# Creates a config with basic value (disabled logging, disabled serve_static, disabled shutdown_message)
|
||||
def self.base
|
||||
new.tap do |config|
|
||||
config.logging = false
|
||||
config.serve_static = false
|
||||
config.shutdown_message = false
|
||||
config.always_rescue = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,6 +6,7 @@
|
|||
# - WebSocket(ws)
|
||||
# - before_*
|
||||
# - error
|
||||
require "../kemal"
|
||||
require "./dsl/*"
|
||||
|
||||
{% for method in Kemal::Base::HTTP_METHODS %}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue