Add configurable startup logging as a block (#291)

Add overload for Kemal.run with just a block. This removes default logging.
This commit is contained in:
Michael Barrett 2017-01-15 13:41:07 -05:00 committed by Serdar Dogruyol
parent 42827c9a9d
commit 209a9e576c
2 changed files with 49 additions and 5 deletions

30
spec/run_spec.cr Normal file
View File

@ -0,0 +1,30 @@
require "./spec_helper"
describe "Run" do
it "runs a code block after starting" do
Kemal.config.env = "test"
make_me_true = false
Kemal.run do
make_me_true = true
Kemal.stop
end
make_me_true.should eq true
end
it "runs without a block being specified" do
Kemal.config.env = "test"
Kemal.run
Kemal.config.running.should eq true
Kemal.stop
end
it "runs with just a block" do
Kemal.config.env = "test"
make_me_true = false
Kemal.run do
make_me_true = true
Kemal.stop
end
make_me_true.should eq true
end
end

View File

@ -4,10 +4,24 @@ require "./kemal/*"
require "./kemal/helpers/*"
module Kemal
# Overload of self.run with the default startup logging
def self.run(port = nil)
self.run port do
log "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
end
end
# Overload of self.run to allow just a block
def self.run(&block)
self.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 self.run(port = nil)
def self.run(port = nil, &block)
Kemal::CLI.new
config = Kemal.config
config.setup
@ -42,11 +56,11 @@ module Kemal
halt env, 404
end
end
log "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}\n"
config.running = true
config.server.listen
end
config.running = true
yield config
config.server.listen if config.env != "test"
end
def self.stop