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:
parent
42827c9a9d
commit
209a9e576c
2 changed files with 49 additions and 5 deletions
30
spec/run_spec.cr
Normal file
30
spec/run_spec.cr
Normal 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
|
24
src/kemal.cr
24
src/kemal.cr
|
@ -4,10 +4,24 @@ require "./kemal/*"
|
||||||
require "./kemal/helpers/*"
|
require "./kemal/helpers/*"
|
||||||
|
|
||||||
module Kemal
|
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 command to run a `Kemal` application.
|
||||||
# The port can be given to `#run` but is optional.
|
# The port can be given to `#run` but is optional.
|
||||||
# If not given Kemal will use `Kemal::Config#port`
|
# If not given Kemal will use `Kemal::Config#port`
|
||||||
def self.run(port = nil)
|
def self.run(port = nil, &block)
|
||||||
Kemal::CLI.new
|
Kemal::CLI.new
|
||||||
config = Kemal.config
|
config = Kemal.config
|
||||||
config.setup
|
config.setup
|
||||||
|
@ -42,11 +56,11 @@ module Kemal
|
||||||
halt env, 404
|
halt env, 404
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
|
config.running = true
|
||||||
|
yield config
|
||||||
|
config.server.listen if config.env != "test"
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.stop
|
def self.stop
|
||||||
|
|
Loading…
Reference in a new issue