From 6f85dbf91ce422a1b451287eb6324a2c7988e81d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateusz=20Gruszczy=C5=84ski?= Date: Thu, 28 Aug 2025 13:56:00 +0200 Subject: [PATCH] drobne --- .env.example | 3 +++ Dockerfile | 10 ++++++++-- app.py | 10 ++++++++++ config.py | 2 ++ docker-compose.yml | 7 +++++++ 5 files changed, 30 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index 0cacd24..5266799 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,9 @@ # === Podstawowe === APP_PORT=8080 +# healthcheck na potrzeby dockera +HEALTHCHECK_TOKEN=Bu22SW455TPe92 + # SQLAlchemy URI bazy (np. SQLite, Postgres, MySQL). # Przykłady: # - SQLite w katalogu instance: sqlite:///instance/baza.db diff --git a/Dockerfile b/Dockerfile index a659800..fa0d1b7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,9 +1,15 @@ FROM python:3.13-slim WORKDIR /app +RUN apt-get update && apt-get install -y --no-install-recommends \ + && apt-get install -y build-essential \ + && apt-get clean \ + && rm -rf /var/lib/apt/lists/* + COPY requirements.txt requirements.txt -RUN apt-get update && apt-get install -y build-essential && \ - pip install --upgrade pip && pip install -r requirements.txt + +RUN pip install --upgrade pip +RUN pip install --no-cache-dir -r requirements.txt COPY . . RUN mkdir -p /app/instance diff --git a/app.py b/app.py index 55a1a1b..9fb46e8 100644 --- a/app.py +++ b/app.py @@ -595,6 +595,16 @@ def favicon(): return "", 204 +@app.route("/healthcheck") +def healthcheck(): + header_token = request.headers.get("X-Internal-Check") + correct_token = app.config.get("HEALTHCHECK_TOKEN") + + if header_token != correct_token: + abort(404) + return "OK", 200 + + if __name__ == "__main__": with app.app_context(): db.create_all() diff --git a/config.py b/config.py index c1e7372..87bf745 100644 --- a/config.py +++ b/config.py @@ -47,3 +47,5 @@ class Config: # (opcjonalnie) wyłącz warningi track_modifications SQLALCHEMY_TRACK_MODIFICATIONS = False + + HEALTHCHECK_TOKEN = _get_str("HEALTHCHECK_TOKEN", "healthcheck") \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c443947..4223dee 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -3,10 +3,17 @@ version: '3.8' services: app: build: + container_name: zbiorka-app context: . dockerfile: Dockerfile ports: - "${APP_PORT:-8080}:${APP_PORT:-8080}" + healthcheck: + test: ["CMD", "python", "-c", "import urllib.request; import sys; req = urllib.request.Request('http://localhost:8000/healthcheck', headers={'X-Internal-Check': '${HEALTHCHECK_TOKEN}'}); sys.exit(0) if urllib.request.urlopen(req).read() == b'OK' else sys.exit(1)"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 10s volumes: - ./instance:/app/instance restart: unless-stopped