Skip to content

Instantly share code, notes, and snippets.

@telent
Created January 4, 2016 07:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telent/ef0bdee073d8f4e4dab5 to your computer and use it in GitHub Desktop.
Save telent/ef0bdee073d8f4e4dab5 to your computer and use it in GitHub Desktop.
puma on random port
require 'rack'
require 'puma'
def my_app
return ->(env) {
[200, {"content-type"=>"text/plain"},
["hello world"]]
}
end
_,_,_,my_address = UDPSocket.new.tap {|u| u.connect("8.8.8.8", 53) }.addr
puma = Rack::Handler.get("puma")
puma.run(my_app, {Host: "0.0.0.0", Port: 0}) do |s|
# oh Demeter ...
realport = s.binder.ios.first.local_address.ip_port
FileUtils.mkdir_p("run")
File.open("run/local_address", "w") do |f|
local_endpoint="#{my_address}:#{realport}"
f.write("#{local_endpoint}\n")
$stderr.write("Wrote #{local_endpoint.inspect} to run/local_address\n")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment