Refactor `Kemal.run` to allow custom port binding (#463)
This commit is contained in:
parent
eed97877a7
commit
bc661d58ae
|
@ -1,20 +1,48 @@
|
||||||
require "./spec_helper"
|
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
|
describe "Run" do
|
||||||
it "runs a code block after starting" do
|
it "runs a code block after starting" do
|
||||||
Kemal.config.env = "test"
|
run(<<-CR).should eq "started\nstopped\n"
|
||||||
make_me_true = false
|
Kemal.config.env = "test"
|
||||||
Kemal.run do
|
Kemal.run do
|
||||||
make_me_true = true
|
puts "started"
|
||||||
Kemal.stop
|
Kemal.stop
|
||||||
end
|
puts "stopped"
|
||||||
make_me_true.should eq true
|
end
|
||||||
|
CR
|
||||||
end
|
end
|
||||||
|
|
||||||
it "runs without a block being specified" do
|
it "runs without a block being specified" do
|
||||||
Kemal.config.env = "test"
|
run(<<-CR).should eq "[test] Kemal is ready to lead at http://0.0.0.0:3000\ntrue\n"
|
||||||
Kemal.run
|
Kemal.config.env = "test"
|
||||||
Kemal.config.running.should eq true
|
Kemal.run
|
||||||
Kemal.stop
|
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
|
||||||
end
|
end
|
||||||
|
|
21
src/kemal.cr
21
src/kemal.cr
|
@ -9,9 +9,7 @@ require "./kemal/helpers/*"
|
||||||
module Kemal
|
module Kemal
|
||||||
# Overload of `self.run` with the default startup logging.
|
# Overload of `self.run` with the default startup logging.
|
||||||
def self.run(port : Int32?)
|
def self.run(port : Int32?)
|
||||||
self.run port do
|
self.run(port) { }
|
||||||
log "[#{config.env}] Kemal is ready to lead at #{config.scheme}://#{config.host_binding}:#{config.port}"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Overload of `self.run` without port.
|
# Overload of `self.run` without port.
|
||||||
|
@ -68,7 +66,22 @@ module Kemal
|
||||||
config.running = true
|
config.running = true
|
||||||
|
|
||||||
yield config
|
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
|
end
|
||||||
|
|
||||||
def self.stop
|
def self.stop
|
||||||
|
|
Loading…
Reference in New Issue