From e7f7ba8f2a0c6db0d5ad1097563980575f8335e8 Mon Sep 17 00:00:00 2001 From: Rommel Terrence Juanillo Date: Fri, 26 Jan 2024 03:55:19 +0800 Subject: [PATCH 1/7] Add docker compose for local development --- .env.sample | 6 ------ .gitignore | 2 +- app/api/endpoints.py | 2 +- app/api/routers.py | 4 ++-- app/config/base.py | 2 +- app/config/keycloak.py | 2 +- docker/Dockerfile | 32 ++++++++++++++++++++++++++++++++ docker/prestart.sh | 10 ++++++++++ docker/run.sh | 11 +++++++++++ local.yml | 26 ++++++++++++++++++++++++++ 10 files changed, 85 insertions(+), 12 deletions(-) delete mode 100644 .env.sample create mode 100644 docker/Dockerfile create mode 100644 docker/prestart.sh create mode 100644 docker/run.sh create mode 100644 local.yml diff --git a/.env.sample b/.env.sample deleted file mode 100644 index f7a57fe..0000000 --- a/.env.sample +++ /dev/null @@ -1,6 +0,0 @@ -KEYCLOAK_SERVER_URL=https://sample.com -KEYCLOAK_CLIENT_ID=sample -KEYCLOAK_REALM_NAME=sample -KEYCLOAK_CLIENT_SECRET=sample -KEYCLOAK_ADMIN_CLIENT_SECRET=sample -KEYCLOAK_CALLBACK_URI=http://sample.com/callback diff --git a/.gitignore b/.gitignore index 99cd9f4..6d112de 100644 --- a/.gitignore +++ b/.gitignore @@ -28,7 +28,7 @@ var/ *.egg .venv/ venv/ -.env +.envs/ # Python debug pdb/ diff --git a/app/api/endpoints.py b/app/api/endpoints.py index 3715212..352ffa2 100644 --- a/app/api/endpoints.py +++ b/app/api/endpoints.py @@ -1,7 +1,7 @@ from fastapi import APIRouter, Depends from fastapi_keycloak import OIDCUser -from app.keycloak import idp +from app.config.keycloak import idp router = APIRouter() diff --git a/app/api/routers.py b/app/api/routers.py index 12c716b..3e91456 100644 --- a/app/api/routers.py +++ b/app/api/routers.py @@ -1,6 +1,6 @@ from fastapi import APIRouter -from app import api +from app.api.endpoints import router api_router_v1 = APIRouter() -api_router_v1.include_router(api.router) +api_router_v1.include_router(router) diff --git a/app/config/base.py b/app/config/base.py index 197477c..da4349a 100644 --- a/app/config/base.py +++ b/app/config/base.py @@ -12,7 +12,7 @@ class KeycloakSettings(BaseModel): class Settings(BaseSettings): - model_config = SettingsConfigDict(env_file=".env", extra='ignore') + model_config = SettingsConfigDict(env_file=".env", extra="ignore") keycloak_server_url: str keycloak_client_id: str diff --git a/app/config/keycloak.py b/app/config/keycloak.py index 8967ccb..d5e8d9f 100644 --- a/app/config/keycloak.py +++ b/app/config/keycloak.py @@ -8,5 +8,5 @@ client_secret=settings.keycloak_client_secret, admin_client_secret=settings.keycloak_admin_client_secret, realm=settings.keycloak_realm_name, - callback_uri=settings.keycloak_callback_uri + callback_uri=settings.keycloak_callback_uri, ) diff --git a/docker/Dockerfile b/docker/Dockerfile new file mode 100644 index 0000000..214a726 --- /dev/null +++ b/docker/Dockerfile @@ -0,0 +1,32 @@ +FROM python:3.12-slim + +ENV PYTHONDONTWRITEBYTECODE 1 +ENV DOCKER_BUILDKIT 1 +ENV PYTHONUNBUFFERED 1 + +RUN addgroup --system app && adduser --system --group app + +WORKDIR /app + +RUN rm -rf /var/lib/apt/lists/* && \ + apt-get purge --auto-remove && \ + apt-get clean + +COPY ./requirements.txt /requirements.txt + +RUN --mount=type=cache,target=/root/.cache \ + pip install -r /requirements.txt --no-cache-dir + +COPY ./docker/backend/run.sh /run.sh +RUN chmod +x /run.sh + +COPY ./docker/backend/prestart.sh /prestart.sh +RUN chmod +x /prestart.sh + +COPY --chown=app . /app + +ENV PYTHONPATH=/app + +USER app + +ENTRYPOINT ["/prestart.sh"] diff --git a/docker/prestart.sh b/docker/prestart.sh new file mode 100644 index 0000000..5f679b9 --- /dev/null +++ b/docker/prestart.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +set -o errexit +set -o pipefail +set -o nounset + +# Run migrations +#alembic upgrade head + +exec "$@" diff --git a/docker/run.sh b/docker/run.sh new file mode 100644 index 0000000..a1ce6bb --- /dev/null +++ b/docker/run.sh @@ -0,0 +1,11 @@ +#!/bin/bash + +set -o errexit +set -o pipefail +set -o nounset + +export APP_MODULE=${APP_MODULE-run:app} +export HOST=${HOST:-0.0.0.0} +export PORT=${PORT:-8001} + +uvicorn --bind $HOST:$PORT "$APP_MODULE" diff --git a/local.yml b/local.yml new file mode 100644 index 0000000..b9c45f6 --- /dev/null +++ b/local.yml @@ -0,0 +1,26 @@ +volumes: + local_postgres_data: {} + local_redis_data: {} + +services: + backend: + image: backend_api + build: + context: . + dockerfile: docker/Dockerfile + command: /run.sh + depends_on: + - postgres + ports: + - "8000:8000" + env_file: + - .envs/.local + + postgres: + image: postgres:14 + volumes: + - local_postgres_data:/var/lib/postgresql/data + env_file: + - .envs/.local.postgres + ports: + - "5432:5432" From d96fe9490bb4246c187516ddee6d14f3cb932b31 Mon Sep 17 00:00:00 2001 From: Rommel Terrence Juanillo Date: Fri, 26 Jan 2024 04:35:25 +0800 Subject: [PATCH 2/7] Add keycloak docker compose setup for local development --- keycloak.yml | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 keycloak.yml diff --git a/keycloak.yml b/keycloak.yml new file mode 100644 index 0000000..353ca84 --- /dev/null +++ b/keycloak.yml @@ -0,0 +1,31 @@ +volumes: + postgres_data: {} + +services: + postgres: + image: postgres:12-bullseye + volumes: + - postgres_data:/var/lib/postgresql/data + environment: + POSTGRES_DB: keycloak + POSTGRES_USER: keycloak + POSTGRES_PASSWORD: password + + keycloak: + image: quay.io/keycloak/keycloak:latest + ports: + - "8080:8080" + environment: + DB_VENDOR: POSTGRES + DB_ADDR: postgres + DB_DATABASE: keycloak + DB_USER: keycloak + DB_SCHEMA: public + DB_PASSWORD: password + KEYCLOAK_ADMIN: admin + KEYCLOAK_ADMIN_PASSWORD: admin + # Uncomment the line below if you want to specify JDBC parameters. The parameter below is just an example, and it shouldn't be used in production without knowledge. It is highly recommended that you read the PostgreSQL JDBC driver documentation in order to use it. + #JDBC_PARAMS: "ssl=true" + depends_on: + - postgres + command: start-dev From e60088433e0a8b355414c4f71eb5dfdfff763911 Mon Sep 17 00:00:00 2001 From: Rommel Terrence Juanillo Date: Fri, 26 Jan 2024 04:36:15 +0800 Subject: [PATCH 3/7] Pre-commit fixes --- docker/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 214a726..c6344a3 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -16,7 +16,7 @@ COPY ./requirements.txt /requirements.txt RUN --mount=type=cache,target=/root/.cache \ pip install -r /requirements.txt --no-cache-dir - + COPY ./docker/backend/run.sh /run.sh RUN chmod +x /run.sh From 1373b1352788956fa29e356ef85e998f2198ed12 Mon Sep 17 00:00:00 2001 From: Rommel Terrence Juanillo Date: Fri, 26 Jan 2024 23:27:41 +0800 Subject: [PATCH 4/7] Update SettingsConfigDict and Docker file paths, modify default values, and add extra hosts configuration and port mapping --- app/config/base.py | 2 +- docker/Dockerfile | 4 ++-- docker/run.sh | 6 +++--- keycloak.yml | 2 ++ local.yml | 4 +++- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/config/base.py b/app/config/base.py index da4349a..0052cdc 100644 --- a/app/config/base.py +++ b/app/config/base.py @@ -12,7 +12,7 @@ class KeycloakSettings(BaseModel): class Settings(BaseSettings): - model_config = SettingsConfigDict(env_file=".env", extra="ignore") + model_config = SettingsConfigDict(env_file=".envs/.local", extra="ignore") keycloak_server_url: str keycloak_client_id: str diff --git a/docker/Dockerfile b/docker/Dockerfile index c6344a3..0f3b8f8 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -17,10 +17,10 @@ COPY ./requirements.txt /requirements.txt RUN --mount=type=cache,target=/root/.cache \ pip install -r /requirements.txt --no-cache-dir -COPY ./docker/backend/run.sh /run.sh +COPY ./docker/run.sh /run.sh RUN chmod +x /run.sh -COPY ./docker/backend/prestart.sh /prestart.sh +COPY ./docker/prestart.sh /prestart.sh RUN chmod +x /prestart.sh COPY --chown=app . /app diff --git a/docker/run.sh b/docker/run.sh index a1ce6bb..4c06e71 100644 --- a/docker/run.sh +++ b/docker/run.sh @@ -4,8 +4,8 @@ set -o errexit set -o pipefail set -o nounset -export APP_MODULE=${APP_MODULE-run:app} +export APP_MODULE=${APP_MODULE-app.main:app} export HOST=${HOST:-0.0.0.0} -export PORT=${PORT:-8001} +export PORT=${PORT:-8000} -uvicorn --bind $HOST:$PORT "$APP_MODULE" +uvicorn --host $HOST --port $PORT "$APP_MODULE" diff --git a/keycloak.yml b/keycloak.yml index 353ca84..b9dfa0f 100644 --- a/keycloak.yml +++ b/keycloak.yml @@ -1,6 +1,8 @@ volumes: postgres_data: {} +name: local-keycloak + services: postgres: image: postgres:12-bullseye diff --git a/local.yml b/local.yml index b9c45f6..269b7ca 100644 --- a/local.yml +++ b/local.yml @@ -15,6 +15,8 @@ services: - "8000:8000" env_file: - .envs/.local + extra_hosts: + - "host.docker.internal:host-gateway" postgres: image: postgres:14 @@ -23,4 +25,4 @@ services: env_file: - .envs/.local.postgres ports: - - "5432:5432" + - "5434:5432" From d1f7e4f650fd81db070f4e31616b55c032304e85 Mon Sep 17 00:00:00 2001 From: Rommel Terrence Juanillo Date: Fri, 26 Jan 2024 23:30:02 +0800 Subject: [PATCH 5/7] feat: Add initial README with project description --- README.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..336ced6 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# Edgecutters + +Simple FastAPI template with Keycloak integration in mind. + +_A work in progress_ From cf32c0c9c1505acf5333eb2bd75c0b95b1afe37b Mon Sep 17 00:00:00 2001 From: Rommel Terrence Juanillo Date: Fri, 26 Jan 2024 23:36:00 +0800 Subject: [PATCH 6/7] Remove keycloak settings --- app/config/base.py | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/app/config/base.py b/app/config/base.py index 0052cdc..e597ba9 100644 --- a/app/config/base.py +++ b/app/config/base.py @@ -1,16 +1,6 @@ -from pydantic import BaseModel from pydantic_settings import BaseSettings, SettingsConfigDict -class KeycloakSettings(BaseModel): - server_url: str - client_id: str - client_secret: str - realm_name: str - admin_client_secret: str - callback_uri: str - - class Settings(BaseSettings): model_config = SettingsConfigDict(env_file=".envs/.local", extra="ignore") From 04fe2365f58e95fbc3a8cee2880a2265b4703f57 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Fri, 12 Apr 2024 04:35:14 +0000 Subject: [PATCH 7/7] Bump idna from 3.6 to 3.7 Bumps [idna](https://github.com/kjd/idna) from 3.6 to 3.7. - [Release notes](https://github.com/kjd/idna/releases) - [Changelog](https://github.com/kjd/idna/blob/master/HISTORY.rst) - [Commits](https://github.com/kjd/idna/compare/v3.6...v3.7) --- updated-dependencies: - dependency-name: idna dependency-type: direct:production ... Signed-off-by: dependabot[bot] --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index b856c96..110f81a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,7 +14,7 @@ filelock==3.13.1 h11==0.14.0 httptools==0.6.1 identify==2.5.33 -idna==3.6 +idna==3.7 itsdangerous==2.1.2 nodeenv==1.8.0 packaging==23.2