From: Adam Dullage Date: Sun, 18 Feb 2024 10:31:54 +0000 (+0000) Subject: Added /health endpoint, Docker HEALTHCHECK and improved access log formatting. X-Git-Url: http://git.99rst.org/?a=commitdiff_plain;h=697b3d35b476065478c0a6c1dd95585975297acf;p=flatnotes.git Added /health endpoint, Docker HEALTHCHECK and improved access log formatting. --- diff --git a/Dockerfile b/Dockerfile index 0ef0d20..df9c15f 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,7 +30,9 @@ ENV FLATNOTES_PATH=/data RUN mkdir -p ${APP_PATH} RUN mkdir -p ${FLATNOTES_PATH} -RUN apt update && apt install -y gosu \ +RUN apt update && apt install -y \ + curl \ + gosu \ && rm -rf /var/lib/apt/lists/* RUN pip install pipenv @@ -45,6 +47,7 @@ COPY --from=build ${BUILD_DIR}/client/dist ./client/dist VOLUME /data EXPOSE 8080/tcp +HEALTHCHECK --interval=60s --timeout=10s CMD curl -f http://localhost:8080/health || exit 1 COPY entrypoint.sh / RUN chmod +x /entrypoint.sh diff --git a/server/logger.py b/server/logger.py index 7b89d95..a7bf7fa 100644 --- a/server/logger.py +++ b/server/logger.py @@ -1,9 +1,31 @@ import logging import os -logging.basicConfig( - format="%(asctime)s [%(levelname)s]: %(message)s", - datefmt="%Y-%m-%d %H:%M:%S", +formatter = logging.Formatter( + "%(asctime)s [%(levelname)s]: %(message)s", "%Y-%m-%d %H:%M:%S" ) +log_level = os.environ.get("LOGLEVEL", "INFO").upper() + +# Internal logger = logging.getLogger() -logger.setLevel(os.environ.get("LOGLEVEL", "INFO").upper()) +handler = logging.StreamHandler() +handler.setFormatter(formatter) +logger.addHandler(handler) +logger.setLevel(log_level) + + +# Uvicorn +class HealthEndpointFilter(logging.Filter): + def filter(self, record: logging.LogRecord) -> bool: + return ( + record.args + and len(record.args) >= 3 + and record.args[2] != "/health" + ) + + +uvicorn_logger = logging.getLogger("uvicorn.access") +uvicorn_logger.addFilter(HealthEndpointFilter()) +for handler in uvicorn_logger.handlers: + handler.setFormatter(formatter) +uvicorn_logger.setLevel(log_level) diff --git a/server/main.py b/server/main.py index 6c52ebb..ce72fb2 100644 --- a/server/main.py +++ b/server/main.py @@ -225,6 +225,17 @@ if global_config.auth_type != AuthType.READ_ONLY: raise HTTPException(409, api_messages.attachment_exists) +# endregion + + +# region Healthcheck +@app.get("/health") +def healthcheck() -> str: + """A lightweight endpoint that simply returns 'OK' to indicate the server + is running.""" + return "OK" + + # endregion app.mount("/", StaticFiles(directory="client/dist"), name="dist")