diff --git a/spec/run_spec.cr b/spec/run_spec.cr new file mode 100644 index 0000000..35fb973 --- /dev/null +++ b/spec/run_spec.cr @@ -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 diff --git a/src/kemal.cr b/src/kemal.cr index 4b3809f..05b638d 100644 --- a/src/kemal.cr +++ b/src/kemal.cr @@ -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