soveran / toro

Tree oriented routing

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Good way to set the host?

lollar opened this issue · comments

Hello,

I ran into an issue when I was trying to use the crystal heroku buildpack and figured out there wasn't a way (at least via method calls) to set the host. I tried using the yield block in the .run method to set those host but the app didn't appear to ever listen when I did that. I forked the repo and was able to get something to work. Here is my solution: lollar@ecc21de which allowed me to get the app running on Heroku. Any thoughts on a "best-practice" way to do this? I would love to add in some documentation.

Thanks for all the work you put in!

commented

Hello @lollar,

I did not try it, but presumably the block passed to run when you use one yields a typical HTTP::Server so you can use this to set the host and port.

App.run(ENV['PORT']) do |server|
  server.bind_tcp "0.0.0.0", ENV['PORT']
end

The problem is that I had to specify the port twice. I don't think there is a way to set the host alone on HTTP::Server. Nevertheless — like you suggest — an overloaded version which accepts the host would be nice.

Hey @mig-hub thanks for the response. I did try using the block approach but unfortunately ran into an error because the port was already in use. I tried to pass in true for the reuse_port on bind_tcp but it still didn't work. I think you would also need to add the reuse_port argument in the HTTP::Server.listen callsite here: https://github.com/soveran/toro/blob/master/src/toro.cr#L51

Hi,

With this code

App.run 3000 do |server|
    server.bind_tcp "0.0.0.0", 3000
  end
end

I have Unhandled exception: bind: Address already in use (Errno), which is expected since the port is specified twice

@lollar This code

App.run 3000 do |server|
  server.bind_tcp("0.0.0.0", 3000, true)
  server.listen
end

works

could this be closed @lollar @soveran ?