Refactor `Kemal.run` to allow custom port binding (#463)

This commit is contained in:
Johannes Müller 2018-06-30 13:16:55 +02:00 committed by Serdar Dogruyol
parent eed97877a7
commit bc661d58ae
2 changed files with 56 additions and 15 deletions

View File

@ -1,20 +1,48 @@
require "./spec_helper"
private def run(code)
code = <<-CR
require "./src/kemal"
#{code}
CR
String.build do |stdout|
stderr = String.build do |stderr|
Process.new("crystal", ["eval"], input: IO::Memory.new(code), output: stdout, error: stderr).wait
end
unless stderr.empty?
fail(stderr)
end
end
end
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
run(<<-CR).should eq "started\nstopped\n"
Kemal.config.env = "test"
Kemal.run do
puts "started"
Kemal.stop
puts "stopped"
end
CR
end
it "runs without a block being specified" do
Kemal.config.env = "test"
Kemal.run
Kemal.config.running.should eq true
Kemal.stop
run(<<-CR).should eq "[test] Kemal is ready to lead at http://0.0.0.0:3000\ntrue\n"
Kemal.config.env = "test"
Kemal.run
puts Kemal.config.running
CR
end
it "allows custom HTTP::Server bind" do
run(<<-CR).should eq "[test] Kemal is ready to lead at http://127.0.0.1:3000, http://0.0.0.0:3001\n"
Kemal.config.env = "test"
Kemal.run do |config|
server = config.server.not_nil!
server.bind_tcp "127.0.0.1", 3000, reuse_port: true
server.bind_tcp "0.0.0.0", 3001, reuse_port: true
end
CR
end
end

View File

@ -9,9 +9,7 @@ require "./kemal/helpers/*"
module Kemal
# Overload of `self.run` with the default startup logging.
def self.run(port : Int32?)
self.run port do
log "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}"
end
self.run(port) { }
end
# Overload of `self.run` without port.
@ -68,7 +66,22 @@ module Kemal
config.running = true
yield config
server.listen(config.host_binding, config.port) if config.env != "test"
# Abort if block called `Kemal.stop`
return unless config.running
unless server.each_address { |_| break true }
server.bind_tcp(config.host_binding, config.port)
end
display_startup_message(config, server)
server.listen unless config.env == "test"
end
def self.display_startup_message(config, server)
addresses = server.addresses.map { |address| "#{config.scheme}://#{address}" }.join ", "
log "[#{config.env}] Kemal is ready to lead at #{addresses}"
end
def self.stop