What the idiomatic way to determine a webapp’s base URL on fly.io (i.e. https://hello.fly.dev/
)? I have a technique outlined below. Any suggestions to simplify my base url detection flow is appreciated.
- Get the URL scheme from the
Fly-Forwarded-Proto
header. It will likely be eitherhttp
orhttps
. - Get the public facing hostname via the
Host
header. If you’re app in on thefly.dev
domain, it’ll look likeapp.fly.dev
. - Then check if the
Fly-Forwarded-Port
is the default port for the protocol scheme (i.e. 80 for http, 443 for https). - Finally put together the base url like the following pseudocode:
fly-forwarded-proto + '://' + host + (if scheme_default_port != fly-forwarded-port then ':' + fly-forwarded-port else '') + '/'