From 512ecea0c270781e7955651c0783997bbd0e5481 Mon Sep 17 00:00:00 2001 From: zwimer Date: Mon, 14 Oct 2024 00:37:06 -0400 Subject: [PATCH] Add favicon support --- rpipe/__init__.py | 2 +- rpipe/server/app.py | 15 ++++++++++++--- rpipe/server/main.py | 1 + 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/rpipe/__init__.py b/rpipe/__init__.py index bfb4f4f..5fcecbd 100644 --- a/rpipe/__init__.py +++ b/rpipe/__init__.py @@ -1 +1 @@ -__version__: str = "9.0.1" # Must be "..", all numbers +__version__: str = "9.0.2" # Must be "..", all numbers diff --git a/rpipe/server/app.py b/rpipe/server/app.py index cfc29b8..fe06076 100644 --- a/rpipe/server/app.py +++ b/rpipe/server/app.py @@ -6,7 +6,7 @@ from pathlib import Path import atexit -from flask import Response, Flask, request +from flask import Response, Flask, send_file, request import waitress from ..shared import restrict_umask, log, __version__ @@ -42,6 +42,7 @@ class ServerConfig: debug: bool state_file: Path | None key_files: list[Path] + favicon: Path | None # @@ -50,10 +51,10 @@ class ServerConfig: @app.errorhandler(404) -def page_not_found(_) -> Response: +def _page_not_found(_, *, quiet=False) -> Response: lg = getLogger(_LOG) lg.warning("404: Request from %s for %s", request.host_url, request.url) - lg.info("Headers: %s", request.headers) + (lg.debug if quiet else lg.info)("Headers: %s", request.headers) return Response("404: Not found", status=404) @@ -75,6 +76,12 @@ def _help() -> Response: return plaintext(msg) +def _mk_favicon(file: Path | None) -> None: + @app.route("/favicon.ico") + def _favicon() -> Response: + return _page_not_found(404, quiet=True) if file is None else send_file(file) + + @app.route("/version") def _show_version() -> Response: getLogger(_LOG).info("Request for /version") @@ -186,6 +193,8 @@ def _log_config(conf: LogConfig) -> Path: def serve(conf: ServerConfig, log_conf: LogConfig) -> None: log_file = _log_config(log_conf) lg = getLogger(_LOG) + lg.debug("Constructing favicon route given favicon.ico=%s", conf.favicon) + _mk_favicon(conf.favicon) lg.info("Setting max packet size: %s", log.LFS(MAX_SIZE_HARD)) app.config["MAX_CONTENT_LENGTH"] = MAX_SIZE_HARD app.url_map.strict_slashes = False diff --git a/rpipe/server/main.py b/rpipe/server/main.py index 295dfc0..7a2c252 100644 --- a/rpipe/server/main.py +++ b/rpipe/server/main.py @@ -28,6 +28,7 @@ def cli() -> None: default=[], help="SSH ed25519 public keys to accept for admin access", ) + parser.add_argument("-F", "--favicon", type=Path, help="The favicon file, if desired") log_group = parser.add_argument_group("Logging") log_group.add_argument( "-l", "--log-file", type=Path, default=None, help="The log file to append to, if desired"