From b48a27b2a3c2a17c97cdc96e5affab3f5b8a59c3 Mon Sep 17 00:00:00 2001 From: "matthias@matsewe.de" Date: Tue, 2 Jul 2024 13:51:19 +0200 Subject: [PATCH] sample Docker/-compose file --- .gitignore | 3 ++- Dockerfile | 9 +++++++++ app/main.py | 2 +- app/security.py | 6 +++++- docker-compose.yml | 19 +++++++++++++++++++ 5 files changed, 36 insertions(+), 3 deletions(-) create mode 100644 Dockerfile create mode 100644 docker-compose.yml diff --git a/.gitignore b/.gitignore index 7a5cff2..9b0dcdd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ __pycache__ .venv -.vscode \ No newline at end of file +.vscode +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..63b0b13 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,9 @@ +FROM python:3.11 + +WORKDIR /code + +COPY ./requirements.txt /tmp/requirements.txt + +RUN pip install --no-cache-dir --upgrade -r /tmp/requirements.txt + +CMD ["fastapi", "run", "app/main.py", "--proxy-headers", "--port", "80", "--reload"] \ No newline at end of file diff --git a/app/main.py b/app/main.py index 9c0bf0d..822269d 100644 --- a/app/main.py +++ b/app/main.py @@ -19,7 +19,7 @@ from starlette.middleware import Middleware from starlette_context import context, plugins from starlette_context.middleware import RawContextMiddleware -if os.path.isfile("first_run") and (os.environ.get("RELOAD_ON_FIRST_RUN").lower() == "true"): +if os.path.isfile("first_run") and (os.environ.get("RELOAD_ON_FIRST_RUN", "").lower() == "true"): print("First run ... load data") with SessionLocal() as db: asyncio.run(admin.create_upload_file(include_non_singable=True, db=db)) diff --git a/app/security.py b/app/security.py index 6211a2e..8a5af6f 100644 --- a/app/security.py +++ b/app/security.py @@ -11,7 +11,7 @@ import os # openssl rand -hex 32 scopes_db = { - os.environ['ADMIN_EMAIL'] : ["admin"] + os.environ.get('ADMIN_EMAIL', "") : ["admin"] } credentials_exception = HTTPException( @@ -22,6 +22,10 @@ credentials_exception = HTTPException( async def get_current_user( security_scopes: SecurityScopes, request: Request ): + + if os.environ.get("NO_LOGIN", "").lower() == "true": + return {"sub": "test"} + try: username: str = request.headers.get("x-auth-request-user") # type: ignore if username is None: diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..354d9c8 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,19 @@ +version: "3.7" + +services: + liederwahl-dev: + build: . + container_name: liederwahl-dev + restart: unless-stopped + volumes: + - .:/code + - liederwahl-dev:/data + ports: + - 80:80 + environment: + - LIST_URL=${LIST_URL} + - NO_LOGIN=true + - RELOAD_ON_FIRST_RUN=false + +volumes: + liederwahl-dev: \ No newline at end of file