Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved URL display on MOTD #2817

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 25 additions & 9 deletions sanic/mixins/startup.py
Expand Up @@ -657,27 +657,43 @@ def serve_location(self) -> str:
def get_server_location(
server_settings: Optional[Dict[str, Any]] = None
) -> str:
serve_location = ""
proto = "http"
if not server_settings:
return serve_location
return ""

host = server_settings["host"]
port = server_settings["port"]

if server_settings.get("ssl") is not None:
proto = "https"
if server_settings.get("unix"):
serve_location = f'{server_settings["unix"]} {proto}://...'
elif server_settings.get("sock"):
return f'{server_settings["unix"]} {proto}://localhost'
if server_settings.get("sock"):
host, port, *_ = server_settings["sock"].getsockname()
if not host or not port:
return ""

if not serve_location and host and port:
# colon(:) is legal for a host only in an ipv6 address
display_host = f"[{host}]" if ":" in host else host
serve_location = f"{proto}://{display_host}:{port}"
# colon(:) is legal for a host only in an ipv6 address
url_host = f"[{host}]" if ":" in host else host
url_port = (
""
if (
(proto == "https" and port == 443)
or (proto == "http" and port == 80)
)
else f":{port}"
)

special = {
"127.0.0.1": "IPv4",
"0.0.0.0": "IPv4 wildcard",
"::1": "IPv6",
"::": "IPv6 wildcard",
}.get(host, "")
if special:
return f"({special}) {proto}://localhost{url_port}"

return serve_location
return f"{proto}://{url_host}{url_port}"
Comment on lines +676 to +696
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This whole bit feels to me like logic we should probably abstract to a utility function somewhere. Don't we do this elsewhere also?


@staticmethod
def get_address(
Expand Down