diff --git a/.env.ci b/.env.ci new file mode 100644 index 0000000..e511a0e --- /dev/null +++ b/.env.ci @@ -0,0 +1,20 @@ +APP_ENV=production + +# Required — fake value for CI only +SECRET_KEY=ci-only-not-a-real-secret-key-do-not-use-in-production + +# Postgres — matches docker-compose.ci.yml service credentials +DATABASE_URL=postgresql://user:password@postgres:5432/appdb + +# LocalStack — fake AWS credentials accepted by LocalStack +AWS_ACCESS_KEY_ID=test +AWS_SECRET_ACCESS_KEY=test +AWS_DEFAULT_REGION=us-east-1 +AWS_S3_BUCKET=media-bucket +AWS_S3_ENDPOINT_URL=http://localstack:4566 +AWS_S3_PUBLIC_ENDPOINT_URL=http://localhost:4566 +AWS_SQS_ENDPOINT_URL=http://localstack:4566 +SQS_QUEUE_URL=http://localstack:4566/000000000000/events-queue + +# Redis +REDIS_URL=redis://redis:6379/0 diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ba66e85..5bdd3bc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,7 +1,11 @@ name: CI on: - workflow_dispatch: + pull_request: + push: + branches: + - main + - develop jobs: lint: @@ -38,6 +42,8 @@ jobs: run: pip install -r requirements-test.txt - name: Run tests + env: + SECRET_KEY: test-only-not-a-real-secret run: pytest tests/app/unit tests/app/integration --cov=app --cov-report=term-missing e2e: @@ -58,6 +64,10 @@ jobs: run: docker compose -f docker-compose.ci.yml up -d --wait timeout-minutes: 5 + - name: Dump logs on failure + if: failure() + run: docker compose -f docker-compose.ci.yml logs + - name: Run E2E tests env: E2E_BASE_URL: http://localhost/api/v1 diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml index 6297f59..c8a3494 100644 --- a/.github/workflows/deploy-dev.yml +++ b/.github/workflows/deploy-dev.yml @@ -10,16 +10,14 @@ on: # Required GitHub configuration (Settings → Environments → dev): # vars.ECS_CLUSTER vars.ECS_SERVICE vars.APP_TASK_FAMILY # vars.VPC_SUBNETS vars.VPC_SECURITY_GROUPS vars.LAMBDA_FUNCTION_NAME +# vars.ECR_APP_REPO — from: terraform output ecr_app_repository_name +# vars.ECR_WORKER_REPO — from: terraform output ecr_worker_repository_name +# vars.AWS_REGION # (all values printed by: terraform output) # # Repository secret (Settings → Secrets → Actions): # secrets.AWS_ROLE_ARN — from: terraform output github_actions_role_arn -env: - AWS_REGION: us-east-1 - ECR_APP_REPO: fastapi-boilerplate/app - ECR_WORKER_REPO: fastapi-boilerplate/worker - jobs: # ── 1. Build all service images ──────────────────────────────────────────── @@ -27,6 +25,7 @@ jobs: build: name: Build & Push Images runs-on: ubuntu-latest + environment: dev outputs: image-tag: ${{ github.sha }} ecr-registry: ${{ steps.ecr-login.outputs.registry }} @@ -40,7 +39,7 @@ jobs: uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} - aws-region: ${{ env.AWS_REGION }} + aws-region: ${{ vars.AWS_REGION }} - name: Login to Amazon ECR id: ecr-login @@ -50,11 +49,11 @@ jobs: uses: docker/build-push-action@v6 with: context: . - file: Dockerfile + file: services/app/Dockerfile push: true tags: | - ${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_APP_REPO }}:${{ github.sha }} - ${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_APP_REPO }}:develop + ${{ steps.ecr-login.outputs.registry }}/${{ vars.ECR_APP_REPO }}:${{ github.sha }} + ${{ steps.ecr-login.outputs.registry }}/${{ vars.ECR_APP_REPO }}:develop - name: Build and push worker image uses: docker/build-push-action@v6 @@ -63,13 +62,13 @@ jobs: file: lambda/Dockerfile.lambda push: true tags: | - ${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_WORKER_REPO }}:${{ github.sha }} - ${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_WORKER_REPO }}:develop + ${{ steps.ecr-login.outputs.registry }}/${{ vars.ECR_WORKER_REPO }}:${{ github.sha }} + ${{ steps.ecr-login.outputs.registry }}/${{ vars.ECR_WORKER_REPO }}:develop # - name: Build and push image # uses: docker/build-push-action@v6 # with: - # context: services/ + # context: . # file: services//Dockerfile # push: true # tags: | @@ -93,7 +92,7 @@ jobs: uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} - aws-region: ${{ env.AWS_REGION }} + aws-region: ${{ vars.AWS_REGION }} - name: Run migrations env: @@ -139,7 +138,7 @@ jobs: uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} - aws-region: ${{ env.AWS_REGION }} + aws-region: ${{ vars.AWS_REGION }} # Tier 1 — services with no inter-service dependencies - name: Deploy app @@ -147,7 +146,7 @@ jobs: CURRENT=$(aws ecs describe-task-definition \ --task-definition "${{ vars.APP_TASK_FAMILY }}" --query taskDefinition) NEW_DEF=$(echo "$CURRENT" | jq \ - --arg img "${{ env.ECR_REGISTRY }}/${{ env.ECR_APP_REPO }}:${{ env.IMAGE_TAG }}" \ + --arg img "${{ env.ECR_REGISTRY }}/${{ vars.ECR_APP_REPO }}:${{ env.IMAGE_TAG }}" \ 'del(.taskDefinitionArn,.revision,.status,.requiresAttributes,.compatibilities,.registeredAt,.registeredBy) | .containerDefinitions[0].image = $img') ARN=$(aws ecs register-task-definition --cli-input-json "$NEW_DEF" \ @@ -183,13 +182,13 @@ jobs: uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} - aws-region: ${{ env.AWS_REGION }} + aws-region: ${{ vars.AWS_REGION }} - name: Update SQS worker run: | aws lambda update-function-code \ --function-name "${{ vars.LAMBDA_FUNCTION_NAME }}" \ - --image-uri "${{ env.ECR_REGISTRY }}/${{ env.ECR_WORKER_REPO }}:${{ env.IMAGE_TAG }}" + --image-uri "${{ env.ECR_REGISTRY }}/${{ vars.ECR_WORKER_REPO }}:${{ env.IMAGE_TAG }}" aws lambda wait function-updated \ --function-name "${{ vars.LAMBDA_FUNCTION_NAME }}" echo "✓ SQS worker updated" diff --git a/.github/workflows/deploy-prod.yml b/.github/workflows/deploy-prod.yml index 5c8f9eb..4fe765e 100644 --- a/.github/workflows/deploy-prod.yml +++ b/.github/workflows/deploy-prod.yml @@ -12,22 +12,21 @@ on: # Required GitHub configuration (Settings → Environments → production): # vars.ECS_CLUSTER vars.ECS_SERVICE vars.APP_TASK_FAMILY # vars.VPC_SUBNETS vars.VPC_SECURITY_GROUPS vars.LAMBDA_FUNCTION_NAME +# vars.ECR_APP_REPO — from: terraform output ecr_app_repository_name +# vars.ECR_WORKER_REPO — from: terraform output ecr_worker_repository_name +# vars.AWS_REGION # (all values printed by: terraform output) # # Repository secret (Settings → Secrets → Actions): # secrets.AWS_ROLE_ARN — from: terraform output github_actions_role_arn -env: - AWS_REGION: us-east-1 - ECR_APP_REPO: fastapi-boilerplate/app - ECR_WORKER_REPO: fastapi-boilerplate/worker - jobs: # ── 1. Build all service images ──────────────────────────────────────────── build: name: Build & Push Images runs-on: ubuntu-latest + environment: production outputs: image-tag: ${{ github.sha }} ecr-registry: ${{ steps.ecr-login.outputs.registry }} @@ -41,7 +40,7 @@ jobs: uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} - aws-region: ${{ env.AWS_REGION }} + aws-region: ${{ vars.AWS_REGION }} - name: Login to Amazon ECR id: ecr-login @@ -51,11 +50,11 @@ jobs: uses: docker/build-push-action@v6 with: context: . - file: Dockerfile + file: services/app/Dockerfile push: true tags: | - ${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_APP_REPO }}:${{ github.sha }} - ${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_APP_REPO }}:main + ${{ steps.ecr-login.outputs.registry }}/${{ vars.ECR_APP_REPO }}:${{ github.sha }} + ${{ steps.ecr-login.outputs.registry }}/${{ vars.ECR_APP_REPO }}:main - name: Build and push worker image uses: docker/build-push-action@v6 @@ -64,13 +63,13 @@ jobs: file: lambda/Dockerfile.lambda push: true tags: | - ${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_WORKER_REPO }}:${{ github.sha }} - ${{ steps.ecr-login.outputs.registry }}/${{ env.ECR_WORKER_REPO }}:main + ${{ steps.ecr-login.outputs.registry }}/${{ vars.ECR_WORKER_REPO }}:${{ github.sha }} + ${{ steps.ecr-login.outputs.registry }}/${{ vars.ECR_WORKER_REPO }}:main # - name: Build and push image # uses: docker/build-push-action@v6 # with: - # context: services/ + # context: . # file: services//Dockerfile # push: true # tags: | @@ -94,7 +93,7 @@ jobs: uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} - aws-region: ${{ env.AWS_REGION }} + aws-region: ${{ vars.AWS_REGION }} - name: Run migrations env: @@ -139,7 +138,7 @@ jobs: uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} - aws-region: ${{ env.AWS_REGION }} + aws-region: ${{ vars.AWS_REGION }} # Tier 1 — services with no inter-service dependencies - name: Deploy app @@ -147,7 +146,7 @@ jobs: CURRENT=$(aws ecs describe-task-definition \ --task-definition "${{ vars.APP_TASK_FAMILY }}" --query taskDefinition) NEW_DEF=$(echo "$CURRENT" | jq \ - --arg img "${{ env.ECR_REGISTRY }}/${{ env.ECR_APP_REPO }}:${{ env.IMAGE_TAG }}" \ + --arg img "${{ env.ECR_REGISTRY }}/${{ vars.ECR_APP_REPO }}:${{ env.IMAGE_TAG }}" \ 'del(.taskDefinitionArn,.revision,.status,.requiresAttributes,.compatibilities,.registeredAt,.registeredBy) | .containerDefinitions[0].image = $img') ARN=$(aws ecs register-task-definition --cli-input-json "$NEW_DEF" \ @@ -182,13 +181,13 @@ jobs: uses: aws-actions/configure-aws-credentials@v4 with: role-to-assume: ${{ secrets.AWS_ROLE_ARN }} - aws-region: ${{ env.AWS_REGION }} + aws-region: ${{ vars.AWS_REGION }} - name: Update SQS worker run: | aws lambda update-function-code \ --function-name "${{ vars.LAMBDA_FUNCTION_NAME }}" \ - --image-uri "${{ env.ECR_REGISTRY }}/${{ env.ECR_WORKER_REPO }}:${{ env.IMAGE_TAG }}" + --image-uri "${{ env.ECR_REGISTRY }}/${{ vars.ECR_WORKER_REPO }}:${{ env.IMAGE_TAG }}" aws lambda wait function-updated \ --function-name "${{ vars.LAMBDA_FUNCTION_NAME }}" echo "✓ SQS worker updated" diff --git a/.gitignore b/.gitignore index 0939c16..94d8f1b 100644 --- a/.gitignore +++ b/.gitignore @@ -81,3 +81,4 @@ terraform/*_override.tf.json terraform/bootstrap/.terraform/ terraform/bootstrap/terraform.tfstate terraform/bootstrap/terraform.tfstate.backup +terraform/environments/*.tfvars diff --git a/CLAUDE.md b/CLAUDE.md index 2552297..7bc16c1 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -10,19 +10,19 @@ The following security controls are intentionally in place. Do not remove or wea | Measure | Location | Rule | |---|---|---| -| `SECRET_KEY` fail-fast | `app/config.py` — `_require()` | Raises `RuntimeError` at startup if unset. Never add a default fallback value. | +| `SECRET_KEY` fail-fast | `services/app/app/config.py` — `_require()` | Raises `RuntimeError` at startup if unset. Never add a default fallback value. | | Upload size limit | `nginx/nginx.conf` — `client_max_body_size 50m` | Applied on both `/api/` and `/graphql` locations. FastAPI itself has no built-in size cap; Nginx is the enforcement point. | -| File upload allowlist | `app/api/v1/media.py` — `_ALLOWED_EXTENSIONS` | Allowlist of safe extensions only. Extend deliberately; never switch to a blocklist. | -| SQL masking in logs | `app/logging/data_filter.py` — `sanitize_traceback()` | Strips `[SQL: ...]`, `[parameters: ...]`, and connection strings from every traceback before it reaches any log backend. Lambda has an inline equivalent `_safe_exc()` in `lambda/handler.py`. | -| GraphQL introspection | `app/graphql_api/__init__.py` + `app/config.py` | `GRAPHQL_INTROSPECTION = True` in `DevelopmentConfig` only; `False` in `ProductionConfig`. Do not hardcode `True`. | -| Explicit JWT algorithm | `app/config.py` — `JWT_ALGORITHM = "HS256"` | Pinned to prevent silent algorithm changes on library upgrades. | +| File upload allowlist | `services/app/app/api/v1/media.py` — `_ALLOWED_EXTENSIONS` | Allowlist of safe extensions only. Extend deliberately; never switch to a blocklist. | +| SQL masking in logs | `services/app/app/logging/data_filter.py` — `sanitize_traceback()` | Strips `[SQL: ...]`, `[parameters: ...]`, and connection strings from every traceback before it reaches any log backend. Lambda has an inline equivalent `_safe_exc()` in `lambda/handler.py`. | +| GraphQL introspection | `services/app/app/graphql_api/__init__.py` + `services/app/app/config.py` | `GRAPHQL_INTROSPECTION = True` in `DevelopmentConfig` only; `False` in `ProductionConfig`. Do not hardcode `True`. | +| Explicit JWT algorithm | `services/app/app/config.py` — `JWT_ALGORITHM = "HS256"` | Pinned to prevent silent algorithm changes on library upgrades. | ### Infrastructure (Terraform) | Measure | Location | Rule | |---|---|---| | Redis TLS | `terraform/modules/elasticache/main.tf` | `transit_encryption_enabled = true`. Output URL is `rediss://` (double-s). Both must stay in sync. | -| Non-root containers | `Dockerfile`, `lambda/Dockerfile`, `lambda/Dockerfile.lambda` | App uses a `app` system user; Lambda uses `nobody`. The `USER` instruction must remain after all `COPY`/`RUN` steps. | +| Non-root containers | `services/app/Dockerfile`, `lambda/Dockerfile`, `lambda/Dockerfile.lambda` | App uses a `app` system user; Lambda uses `nobody`. The `USER` instruction must remain after all `COPY`/`RUN` steps. | | ECS task role SQS scope | `terraform/modules/iam/main.tf` | FastAPI app: `sqs:SendMessage` + `sqs:GetQueueAttributes` only. Lambda worker: `ReceiveMessage` + `DeleteMessage`. Never cross-assign. | | Secrets Manager recovery | `terraform/main.tf`, `terraform/modules/rds/main.tf` | `recovery_window_in_days = 7`. Never set to `0` in committed code (risk of unrecoverable accidental deletion). | | WAF logging | `terraform/modules/waf/main.tf` | CloudWatch log group `aws-waf-logs-{prefix}`, 90-day retention. Do not remove `aws_wafv2_web_acl_logging_configuration`. | @@ -72,22 +72,14 @@ docker compose logs -f app bash migrate.sh ``` -**`start_infra.sh`** — starts only the infrastructure services (Postgres, LocalStack, migrations) in detached mode. Used when running the FastAPI app on the host for local debugging. +**`launch_app_docker_image.sh`** — builds and launches a single service container standalone. Accepts an optional service name argument (default: `app`). ```bash -bash start_infra.sh -# To stop: docker compose stop postgres localstack +bash launch_app_docker_image.sh # builds services/app/Dockerfile +bash launch_app_docker_image.sh payments # builds services/payments/Dockerfile for future services +# To stop: docker stop fastapi-boilerplate- ``` -**`launch_app_docker_image.sh`** — builds the production Docker image standalone (no compose, no infrastructure) and starts a single detached container on port 5000. Useful for smoke-testing the image in isolation. After startup it hits `/api/v1/health` to verify the server is up and prints the GraphQL playground URL. - -```bash -bash launch_app_docker_image.sh -# To stop: docker stop fastapi-boilerplate -``` - -> Note: this script starts only the app container with no database or LocalStack, so any endpoint that touches Postgres or S3 will fail. Use it only to verify the image builds and the process starts cleanly. - ## Debugging Two VSCode debug configurations are defined in `.vscode/launch.json`: @@ -140,28 +132,74 @@ Other services reach the FastAPI app internally via `http://app:5000` (direct) o **Environment:** All config lives in `.env.local`, loaded via `env_file` in compose. Never committed — use `.env.local` as the single source of truth for local development. +## Multi-service Python path (`pyproject.toml`) + +`pyproject.toml` lives at the repo root and serves as the single pytest + ruff config. The `pythonpath` array controls which service directories pytest adds to `sys.path`: + +```toml +[tool.pytest.ini_options] +pythonpath = ["services/app"] # one entry per Python microservice +``` + +When you add a new Python service, append its directory: `pythonpath = ["services/app", "services/payments"]`. + +## Services Layout + +Microservices live under `services/`. Each service is self-contained with its own Dockerfiles. + +``` +services/ +└── app/ # FastAPI REST + GraphQL API + ├── app/ # Python package (app factory, models, routes, etc.) + ├── Dockerfile # Production image (uvicorn) + └── Dockerfile.dev # Dev image (uvicorn + debugpy, hot-reload via volume) +``` + +### Docker build pattern + +Build context stays at `.` (repo root) so Dockerfiles can access `requirements.txt`, `migrations/`, `alembic.ini`, and `wsgi.py`. Only the `dockerfile:` path points into `services/`: + +```yaml +build: + context: . + dockerfile: services/app/Dockerfile +``` + +### Dev volume mounts + +`app` and `migrate` services mount `./services/app:/app`. The `migrate` service also mounts `./migrations:/app/migrations` and `./alembic.ini:/app/alembic.ini` since Alembic files stay at the repo root. + +### Adding a new microservice + +1. Create `services//` with `Dockerfile` and `Dockerfile.dev`. +2. Add to `docker-compose.yml` with `context: .` and `dockerfile: services//Dockerfile.dev`. +3. Add Nginx upstream + location in `nginx/nginx.conf`. +4. Add build step in `deploy-dev.yml` and `deploy-prod.yml` with `context: .` and `file: services//Dockerfile`. +5. Add ECR repo in `terraform/modules/ecr/main.tf` and ECS service in `terraform/`. +6. Append `"services/"` to `pythonpath` in `pyproject.toml`. + ## Architecture ### App Factory -`app/__init__.py` uses a factory pattern (`create_app(config_name)`). The REST API version is selected dynamically via the `REST_API_V` env var (default `v1`) using `importlib`, enabling multi-version support without code changes. +`services/app/app/__init__.py` uses a factory pattern (`create_app(config_name)`). The REST API version is selected dynamically via the `REST_API_V` env var (default `v1`) using `importlib`, enabling multi-version support without code changes. ### Dual API Layer Every feature is exposed over both REST and GraphQL. Both share the same models and services — only the transport layer differs. -- **REST:** `app/api/v1/` — `APIRouter` mounted at `/api/v1/` -- **GraphQL:** `app/graphql_api/` — Strawberry `GraphQLRouter` at `/graphql`, with `multipart_uploads_enabled=True` for file uploads +- **REST:** `services/app/app/api/v1/` — `APIRouter` mounted at `/api/v1/` +- **GraphQL:** `services/app/app/graphql_api/` — Strawberry `GraphQLRouter` at `/graphql`, with `multipart_uploads_enabled=True` for file uploads -GraphQL resolvers do not use `Depends(require_access_token)` (a FastAPI dependency) — they call `get_token_from_bearer()` / `verify_access_token()` from `app/security.py` manually at the top of each resolver, since Strawberry handles the request context differently. The JWT identity and request are available via `info.context.request.headers` and `info.context.db`. +GraphQL resolvers do not use `Depends(require_access_token)` (a FastAPI dependency) — they call `get_token_from_bearer()` / `verify_access_token()` from `services/app/app/security.py` manually at the top of each resolver, since Strawberry handles the request context differently. The JWT identity and request are available via `info.context.request.headers` and `info.context.db`. ### Configuration -`app/config.py` has three config classes (`DevelopmentConfig`, `ProductionConfig`, `TestingConfig`) all inheriting from `Config`. The production image (`wsgi.py`) runs `create_app('production')`. All AWS/S3 settings must be on the base `Config` class, not only on `DevelopmentConfig`, or they will be absent in production mode. +`services/app/app/config.py` has three config classes (`DevelopmentConfig`, `ProductionConfig`, `TestingConfig`) all inheriting from `Config`. The production image (`wsgi.py`) runs `create_app('production')`. All AWS/S3 settings must be on the base `Config` class, not only on `DevelopmentConfig`, or they will be absent in production mode. ### S3 / LocalStack Split Endpoint -The S3 service (`app/services/aws_s3_service.py`) maintains two boto3 client modes: +The S3 service (`services/app/app/services/aws_s3_service.py`) maintains two boto3 client modes: - `_client()` — uses `AWS_S3_ENDPOINT_URL` (`http://localstack:4566`) for internal operations (upload). Resolvable only inside Docker. - `_client(public=True)` — uses `AWS_S3_PUBLIC_ENDPOINT_URL` (`http://localhost:4566`) for generating presigned URLs. Needed because presigned URLs are opened by the browser on the host machine, which cannot resolve the `localstack` hostname. @@ -170,7 +208,7 @@ In production both env vars are unset (`None`), so boto3 routes to real AWS auto ### Models -All models must be imported in `app/models/__init__.py` for Alembic to detect them via `Base.metadata`. Current models: +All models must be imported in `services/app/app/models/__init__.py` for Alembic to detect them via `Base.metadata`. Current models: - `User` — `id` (UUID PK), `email` (unique), `password_hash`, `created_at` - `Media` — `id` (UUID PK), `user_id` (FK → users), `content_key` (S3 object key, **not** a URL), `created_at` @@ -179,15 +217,15 @@ All models must be imported in `app/models/__init__.py` for Alembic to detect th ### Logging -The app uses an Object Adapter pattern. All loggers implement `LoggerProtocol` (`app/utils/logger.py`) and are injected into `AppLogger`, which fans out calls to all of them. +The app uses an Object Adapter pattern. All loggers implement `LoggerProtocol` (`services/app/app/utils/logger.py`) and are injected into `AppLogger`, which fans out calls to all of them. | Class | Location | Behaviour | |---|---|---| -| `AppLogger` | `app/logging/logger.py` | Fanout adapter; single public method `log(message, level, data, exc)`. `Level` enum exposed as `AppLogger.Level.{INFO,WARN,ERROR}` | -| `ConsoleLogger` | `app/logging/logger.py` | stdout via Python `logging`; DEBUG in dev, WARNING in prod | -| `SentryLogger` | `app/logging/sentry_logger.py` | `info`/`warn` → Sentry breadcrumbs; `error` → `capture_message` with extras | -| `CloudWatchLogger` | `app/logging/cloudwatch_logger.py` | Structured JSON events via `watchtower`; supports `endpoint_url` for LocalStack | -| `LokiLogger` | `app/logging/loki_logger.py` | POSTs structured JSON to Loki's `/loki/api/v1/push`; uses stdlib `urllib` only (no extra dependency); failures are silently swallowed | +| `AppLogger` | `services/app/app/logging/logger.py` | Fanout adapter; single public method `log(message, level, data, exc)`. `Level` enum exposed as `AppLogger.Level.{INFO,WARN,ERROR}` | +| `ConsoleLogger` | `services/app/app/logging/logger.py` | stdout via Python `logging`; DEBUG in dev, WARNING in prod | +| `SentryLogger` | `services/app/app/logging/sentry_logger.py` | `info`/`warn` → Sentry breadcrumbs; `error` → `capture_message` with extras | +| `CloudWatchLogger` | `services/app/app/logging/cloudwatch_logger.py` | Structured JSON events via `watchtower`; supports `endpoint_url` for LocalStack | +| `LokiLogger` | `services/app/app/logging/loki_logger.py` | POSTs structured JSON to Loki's `/loki/api/v1/push`; uses stdlib `urllib` only (no extra dependency); failures are silently swallowed | `AppLogger` is created in `create_app()` and attached as `app.logger_adapter`. Sentry, CloudWatch, and Loki are **opt-in** — only wired when their env vars are set. CloudWatch init failure is non-fatal (logs a warning, app continues with remaining loggers). Loki push failures are silently swallowed. @@ -209,10 +247,10 @@ logger = info.context.logger_adapter logger.log("upload failed", level=logger.Level.ERROR, data={"key": s3_key}, exc=e) ``` -**Data filtering:** `mask_sensitive()` in `app/logging/data_filter.py` recursively replaces values of sensitive keys (`password`, `token`, `secret`, `authorization`, etc.) with `***`. Applied automatically in `AppLogger.log()` before any logger sees the data. To add keys, extend `_SENSITIVE_KEYS` in `data_filter.py`. +**Data filtering:** `mask_sensitive()` in `services/app/app/logging/data_filter.py` recursively replaces values of sensitive keys (`password`, `token`, `secret`, `authorization`, etc.) with `***`. Applied automatically in `AppLogger.log()` before any logger sees the data. To add keys, extend `_SENSITIVE_KEYS` in `data_filter.py`. **Sentry notes:** -- JWT auth failures return 401 from `app/security.py` — Sentry captures these via `FastApiIntegration` automatically. +- JWT auth failures return 401 from `services/app/app/security.py` — Sentry captures these via `FastApiIntegration` automatically. - `info`/`warn` calls appear as breadcrumbs inside Sentry error events, not as standalone events. This is intentional — sending every log as an event burns Sentry quota. **CloudWatch / LocalStack notes:** @@ -272,9 +310,9 @@ Fargate is serverless — there is no accessible host OS, Docker socket, or cgro The only app-side requirement for the production setup is the `/metrics` endpoint — ADOT picks it up without any code changes. **Adding a new logger backend:** -1. Create a class in `app/logging/` implementing `LoggerProtocol` (`info`, `warning`, `error` methods). +1. Create a class in `services/app/app/logging/` implementing `LoggerProtocol` (`info`, `warning`, `error` methods). 2. Instantiate it conditionally in `create_app()` and append to `loggers`. -3. Add the required env var to `app/config.py` base `Config` class. +3. Add the required env var to `services/app/app/config.py` base `Config` class. ### Error Handling @@ -288,16 +326,16 @@ Each layer has a strict responsibility. Do not cross these boundaries: | Layer | Location | Responsibility | |---|---|---| -| **Models** | `app/models/` | SQLAlchemy schema only — no business logic, no imports from API or service layers | -| **Services** | `app/services/` | External integrations (S3, SQS, future: email, payments). Read config directly from `os.getenv()` — no framework context assumptions | -| **REST handlers** | `app/api/v1/` | Parse request, validate input, call services/models, return `rest_api_response()`. No raw `jsonify()` calls outside utils | -| **REST utils** | `app/api/utils/utils.py` | Shared REST helpers (`rest_api_response`). All reusable REST helper functions live here — never inline them in handlers | -| **GraphQL resolvers** | `app/graphql_api/resolvers/` | Mirror REST handlers but return `Response[T]` typed objects. No direct HTTP response logic | -| **GraphQL utils** | `app/graphql_api/utils.py` | Shared GraphQL helpers (e.g. model-to-type converters). All reusable GraphQL helper functions live here — never inline them in resolvers | -| **GraphQL types** | `app/graphql_api/types/` | Strawberry type definitions only — no resolver logic | -| **Extensions** | `app/extensions.py` | Defines SQLAlchemy `Base` (DeclarativeBase). Engine + session factory live in `app/database.py` and are initialized by `create_app()` | -| **Config** | `app/config.py` | All configuration via `os.getenv()` at class definition time. Env vars are never read directly in handlers or services | -| **Logging** | `app/logging/` | `AppLogger` + logger adapters (`ConsoleLogger`, `SentryLogger`, `CloudWatchLogger`, `LokiLogger`), `mask_sensitive` data filter. Logger attached to `app.state.logger_adapter`; accessed via `request.app.state.logger_adapter` in handlers | +| **Models** | `services/app/app/models/` | SQLAlchemy schema only — no business logic, no imports from API or service layers | +| **Services** | `services/app/app/services/` | External integrations (S3, SQS, future: email, payments). Read config directly from `os.getenv()` — no framework context assumptions | +| **REST handlers** | `services/app/app/api/v1/` | Parse request, validate input, call services/models, return `rest_api_response()`. No raw `jsonify()` calls outside utils | +| **REST utils** | `services/app/app/api/utils/utils.py` | Shared REST helpers (`rest_api_response`). All reusable REST helper functions live here — never inline them in handlers | +| **GraphQL resolvers** | `services/app/app/graphql_api/resolvers/` | Mirror REST handlers but return `Response[T]` typed objects. No direct HTTP response logic | +| **GraphQL utils** | `services/app/app/graphql_api/utils.py` | Shared GraphQL helpers (e.g. model-to-type converters). All reusable GraphQL helper functions live here — never inline them in resolvers | +| **GraphQL types** | `services/app/app/graphql_api/types/` | Strawberry type definitions only — no resolver logic | +| **Extensions** | `services/app/app/extensions.py` | Defines SQLAlchemy `Base` (DeclarativeBase). Engine + session factory live in `services/app/app/database.py` and are initialized by `create_app()` | +| **Config** | `services/app/app/config.py` | All configuration via `os.getenv()` at class definition time. Env vars are never read directly in handlers or services | +| **Logging** | `services/app/app/logging/` | `AppLogger` + logger adapters (`ConsoleLogger`, `SentryLogger`, `CloudWatchLogger`, `LokiLogger`), `mask_sensitive` data filter. Logger attached to `app.state.logger_adapter`; accessed via `request.app.state.logger_adapter` in handlers | ## Code Quality @@ -420,22 +458,22 @@ with patch("app.models.user.bcrypt.gensalt", - REST response fields use snake_case throughout (`access_token`, `refresh_token`, `media_id`). **Responses:** -- REST: always use `rest_api_response(success, message, data, status_code, request=request)` from `app/api/utils/utils.py`. Returns a `JSONResponse` — never return a raw dict. +- REST: always use `rest_api_response(success, message, data, status_code, request=request)` from `services/app/app/api/utils/utils.py`. Returns a `JSONResponse` — never return a raw dict. - GraphQL: always return `Response[T]` with `success`, `message`, and optionally `data`. Never raise exceptions from resolvers. **Authentication:** -- REST handlers: add `user_id: str = Depends(require_access_token)` (or `require_refresh_token`) from `app/security.py`. +- REST handlers: add `user_id: str = Depends(require_access_token)` (or `require_refresh_token`) from `services/app/app/security.py`. - GraphQL resolvers: call `get_token_from_bearer(auth_header)` + `verify_access_token(token)` / `verify_refresh_token(token)` inside a `try/except` at the top of the resolver — FastAPI's `Depends()` cannot be used inside Strawberry resolvers. **Postman collection:** `postman_collection.json` at the repo root must be kept in sync with API changes. Update it whenever you add, remove, or rename an endpoint or change a request/response shape. The collection uses collection-level variables (`base_url`, `access_token`, `refresh_token`, `media_id`) and test scripts on Login/Upload requests to auto-capture tokens and IDs for chained requests. **Adding a new feature:** -1. Add/update the SQLAlchemy model and register it in `app/models/__init__.py`. +1. Add/update the SQLAlchemy model and register it in `services/app/app/models/__init__.py`. 2. Generate and apply a migration: `alembic revision --autogenerate -m "description" && alembic upgrade head`. -3. Add any external service logic to `app/services/`. -4. Add a FastAPI router in `app/api/v1/` and include it in `app/api/v1/__init__.py`. -5. Add a GraphQL resolver class in `app/graphql_api/resolvers/` and merge it into the schema via `merge_types` in `app/graphql_api/schema.py`. -6. Add the corresponding type to `app/graphql_api/types/types.py` if needed. +3. Add any external service logic to `services/app/app/services/`. +4. Add a FastAPI router in `services/app/app/api/v1/` and include it in `services/app/app/api/v1/__init__.py`. +5. Add a GraphQL resolver class in `services/app/app/graphql_api/resolvers/` and merge it into the schema via `merge_types` in `services/app/app/graphql_api/schema.py`. +6. Add the corresponding type to `services/app/app/graphql_api/types/types.py` if needed. ## Lambda Worker Dockerfiles @@ -448,9 +486,9 @@ Two Dockerfiles exist for these two runtimes: | File | Used by | CMD | |---|---|---| | `lambda/Dockerfile` | `docker-compose.yml` worker service | `python handler.py` → runs `poll()` | -| `lambda/Dockerfile.lambda` | `deploy.yml` CI/CD worker image build | `handler.handler` → Lambda RIC calls `handler()` | +| `lambda/Dockerfile.lambda` | `deploy-dev.yml` / `deploy-prod.yml` CI/CD worker image build | `handler.handler` → Lambda RIC calls `handler()` | -Never use `Dockerfile` for the CI/CD image build — it produces a long-running process that is not a valid Lambda container. The deploy workflow already references `Dockerfile.lambda`. +Never use `Dockerfile` for the CI/CD image build — it produces a long-running process that is not a valid Lambda container. The deploy workflows already reference `Dockerfile.lambda`. ## CI/CD Pipeline @@ -498,12 +536,13 @@ Use a two-phase approach for breaking changes: first deploy adds the new column ### Adding a new microservice -1. Add its ECR repo to `terraform/modules/ecr/main.tf` -2. Add its ECS service + task definition to `terraform/` (or a new module) -3. Add a build step to the `build` job in `deploy.yml` -4. Add a `run_migration` call in `migrate-dev` and `migrate-prod` (if it has its own DB) -5. Add a deploy step in `deploy-dev` and `deploy-prod` at the correct tier -6. Add its GitHub environment vars (`*_TASK_FAMILY`, `*_SERVICE`) via `terraform output` +1. Create `services//` with its Dockerfile. +2. Add ECR repo to `terraform/modules/ecr/main.tf`. +3. Add ECS service + task definition to `terraform/`. +4. Add build step in `deploy-dev.yml` and `deploy-prod.yml` with `context: .` and `file: services//Dockerfile`. +5. Add `run_migration` call in migrate jobs if it has its own DB. +6. Add deploy step at the correct tier. +7. Add GitHub environment vars via `terraform output`. ### Required GitHub secrets and variables diff --git a/README.md b/README.md index 66db88e..da4bb26 100644 --- a/README.md +++ b/README.md @@ -79,65 +79,45 @@ Every feature is exposed over **both REST and GraphQL**. Both share the same dat ``` . -├── app/ -│ ├── __init__.py # App factory (create_app) -│ ├── config.py # Dev / Prod / Test config classes -│ ├── extensions.py # db, migrate, jwt singletons -│ ├── models/ # SQLAlchemy models (User, Media, Event) -│ ├── services/ # External integrations (S3, SQS) -│ ├── logging/ # AppLogger, SentryLogger, CloudWatchLogger, data_filter -│ ├── api/ -│ │ └── v1/ # REST blueprints (auth, media, events, cache, health) -│ └── graphql_api/ -│ ├── resolvers/ # GraphQL mutations and queries -│ ├── types/ # Strawberry type definitions -│ ├── utils.py # Shared GraphQL helpers (model → type converters) -│ └── schema.py # Merged GraphQL schema +├── services/ # One folder per microservice +│ └── app/ # FastAPI REST + GraphQL API +│ ├── app/ +│ │ ├── __init__.py # App factory (create_app) +│ │ ├── config.py # Dev / Prod / Test config classes +│ │ ├── database.py # SQLAlchemy engine + session factory +│ │ ├── extensions.py # SQLAlchemy Base (DeclarativeBase) +│ │ ├── security.py # JWT token helpers +│ │ ├── models/ # SQLAlchemy models (User, Media, Event) +│ │ ├── services/ # External integrations (S3, SQS) +│ │ ├── logging/ # AppLogger, SentryLogger, CloudWatchLogger, data_filter +│ │ ├── api/ +│ │ │ └── v1/ # FastAPI routers (auth, media, events, cache, health) +│ │ └── graphql_api/ +│ │ ├── resolvers/ # GraphQL mutations and queries +│ │ ├── types/ # Strawberry type definitions +│ │ ├── utils.py # Shared GraphQL helpers (model → type converters) +│ │ └── schema.py # Merged GraphQL schema +│ ├── Dockerfile # Production image (uvicorn) +│ └── Dockerfile.dev # Dev image (uvicorn + debugpy) ├── lambda/ │ ├── handler.py # Shared: handler(event, context) for Lambda + poll() for local dev │ ├── Dockerfile # Local dev — long-polling worker (used by docker-compose) │ ├── Dockerfile.lambda # AWS Lambda — uses Lambda Python base image │ └── requirements.txt # Lambda dependencies (boto3, sqlalchemy, psycopg2) -├── .github/ -│ └── workflows/ -│ ├── ci.yml # Lint + test on every push and pull request -│ └── deploy.yml # Build images → push to ECR → deploy to ECS + Lambda (manual trigger) -├── terraform/ -│ ├── bootstrap/ # Run once: creates S3 state bucket + DynamoDB lock table -│ ├── environments/ -│ │ ├── dev.tfvars # Dev-specific sizes and flags -│ │ └── prod.tfvars # Prod: Multi-AZ, deletion protection, HTTPS -│ ├── modules/ -│ │ ├── networking/ # VPC, NAT gateway, subnets, security groups, VPC flow logs -│ │ ├── ecr/ # ECR repositories for app and worker images -│ │ ├── iam/ # ECS roles, Lambda role, GitHub OIDC deploy role -│ │ ├── rds/ # PostgreSQL on RDS + DATABASE_URL stored in Secrets Manager -│ │ ├── elasticache/ # Redis (ElastiCache replication group) -│ │ ├── s3/ # Media bucket with encryption + HTTPS-only policy -│ │ ├── sqs/ # Events queue + dead-letter queue -│ │ ├── alb/ # Application Load Balancer with HTTP→HTTPS redirect -│ │ ├── waf/ # WAF: OWASP Top 10, SQLi rules, per-IP rate limit -│ │ ├── ecs/ # Fargate cluster, task definition, service -│ │ └── lambda/ # Container image Lambda function + SQS event source -│ ├── main.tf # Root module — wires all child modules -│ ├── variables.tf # All input variables with descriptions -│ ├── outputs.tf # Outputs map directly to GitHub environment vars -│ ├── versions.tf # Provider pins + S3 backend stub -│ └── backend.hcl # Fill-in template — gitignored, never committed -├── nginx/ -│ └── nginx.conf # Reverse proxy config with DDoS protection -├── migrations/ # Alembic migration files (auto-generated on up --build) -├── Dockerfile # Production image (gunicorn) -├── Dockerfile.dev # Dev image (uvicorn + debugpy) +├── migrations/ # Alembic migration files +├── alembic.ini # Alembic config (accessible to Docker build context) ├── docker-compose.yml # Full local stack -├── wsgi.py # Production entrypoint -├── migrate.sh # Interactive migration helper -├── start_infra.sh # Start only DB + S3 (for host debugging) -└── .vscode/ - ├── launch.json # VSCode debug configurations - └── tasks.json # Pre/post debug tasks +├── wsgi.py # Production entrypoint (accessible to Docker build context) +├── requirements.txt # App dependencies (accessible to Docker build context) +└── .github/ + └── workflows/ + ├── ci.yml # Lint + test on every push to main/develop and pull request + ├── deploy-dev.yml # Build → ECR → ECS + Lambda deploy (dev environment) + └── deploy-prod.yml # Build → ECR → ECS + Lambda deploy (production, manual approval) ``` +> **Why `wsgi.py`, `migrations/`, `alembic.ini`, and `requirements*.txt` stay at the repo root:** Docker build context is anchored at `.` so all Dockerfiles can reach them via COPY. The `migrate` service also bind-mounts `./alembic.ini` and `./migrations` so Alembic can run correctly. + --- ## Prerequisites @@ -548,8 +528,9 @@ All external traffic enters through **Nginx on port 80** (`nginx/nginx.conf`). T **Adding a new microservice:** -1. Add the service to `docker-compose.yml` — no port exposure needed (stays internal). -2. Add an upstream and a location block to `nginx/nginx.conf`: +1. Create `services//` with Dockerfile. +2. Add the service to `docker-compose.yml` — no port exposure needed (stays internal). +3. Add an upstream and a location block to `nginx/nginx.conf`: ```nginx upstream payments { @@ -613,7 +594,7 @@ The `worker` container (`lambda/handler.py`) polls SQS using long-polling (`Wait | File | Used by | Runtime | |---|---|---| | `lambda/Dockerfile` | `docker-compose.yml` | Long-polling loop (`poll()`) — blocks and polls SQS continuously | -| `lambda/Dockerfile.lambda` | `deploy.yml` CI/CD | AWS Lambda base image — Lambda runtime invokes `handler()` per SQS batch | +| `lambda/Dockerfile.lambda` | `deploy-dev.yml` / `deploy-prod.yml` CI/CD | AWS Lambda base image — Lambda runtime invokes `handler()` per SQS batch | The CI/CD pipeline builds `Dockerfile.lambda` and pushes it to ECR. On AWS, `DATABASE_URL` points to RDS, `SQS_QUEUE_URL` to the real queue, and `AWS_SQS_ENDPOINT_URL` is unset (boto3 routes to real AWS automatically). @@ -653,7 +634,7 @@ The boilerplate ships with a hardened baseline. The measures below are active ou **`SECRET_KEY` is required** — the app refuses to start if `SECRET_KEY` is not set. There is no default fallback. Set it in `.env.local` for local dev; Secrets Manager injects it in production via the ECS task definition. -**File uploads are restricted** — only `jpg`, `jpeg`, `png`, `gif`, `webp`, `pdf`, `mp4`, `mov` are accepted. Anything else returns `415`. Maximum upload size is 50 MB (`MAX_CONTENT_LENGTH` in config). To extend the allowlist, edit `_ALLOWED_EXTENSIONS` in [app/api/v1/media.py](app/api/v1/media.py). +**File uploads are restricted** — only `jpg`, `jpeg`, `png`, `gif`, `webp`, `pdf`, `mp4`, `mov` are accepted. Anything else returns `415`. Maximum upload size is 50 MB (`MAX_CONTENT_LENGTH` in config). To extend the allowlist, edit `ALLOWED_EXTENSIONS` in [app/api/v1/media.py](app/api/v1/media.py). **SQL queries are stripped from logs** — SQLAlchemy exceptions include the full query and bound parameters in their string representation. `sanitize_traceback()` in [app/logging/data_filter.py](app/logging/data_filter.py) redacts `[SQL: ...]` and `[parameters: ...]` blocks from every traceback before it reaches any log backend (Console, Sentry, CloudWatch, Loki). The Lambda worker has an equivalent `_safe_exc()` helper. @@ -857,7 +838,7 @@ build ── all images pushed to ECR before anything deploys **Why migrations are a separate job:** during a rolling ECS update, old and new task instances run simultaneously against the same database. Migrations must complete and be backward-compatible before any instance picks up the new code. -**Adding a new microservice:** add a build step to `build`, a `run_migration` call in both `migrate-*` jobs, and a deploy step at the correct tier in both `deploy-*` jobs. Workers go in `deploy-workers-*`. +**Adding a new microservice:** create `services//` with its Dockerfile, add a build step to `build` using `context: .` and `file: services//Dockerfile`, a `run_migration` call in both `migrate-*` jobs if it has its own DB, and a deploy step at the correct tier in both `deploy-*` jobs. Workers go in `deploy-workers-*`. The production approval gate is a native GitHub feature: create a `production` environment in **Settings → Environments**, add required reviewers, and the workflow pauses automatically. @@ -1199,7 +1180,7 @@ The only app-side requirement is the `/metrics` endpoint — ADOT picks it up au ## Production Image -The production Docker image uses `Dockerfile` (gunicorn, 4 workers, no debugpy). To smoke-test it in isolation: +The production Docker image uses `services/app/Dockerfile` (uvicorn, no debugpy). To smoke-test it in isolation: ```bash bash launch_app_docker_image.sh diff --git a/docker-compose.ci.yml b/docker-compose.ci.yml index 45d70be..b1f7d4b 100644 --- a/docker-compose.ci.yml +++ b/docker-compose.ci.yml @@ -35,7 +35,9 @@ services: retries: 10 migrate: - build: . + build: + context: . + dockerfile: services/app/Dockerfile command: sh -c "python -m alembic upgrade head" env_file: - .env.ci @@ -48,7 +50,7 @@ services: app: build: context: . - dockerfile: Dockerfile + dockerfile: services/app/Dockerfile ports: - "5000:5000" env_file: diff --git a/docker-compose.yml b/docker-compose.yml index f333895..471279d 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -44,14 +44,18 @@ services: retries: 5 migrate: - build: . + build: + context: . + dockerfile: services/app/Dockerfile command: sh -c "python -m alembic upgrade head" env_file: - .env.local - volumes: - - .:/app environment: PYTHONPATH: /app + volumes: + - ./services/app:/app + - ./migrations:/app/migrations + - ./alembic.ini:/app/alembic.ini depends_on: postgres: condition: service_healthy @@ -70,13 +74,13 @@ services: app: build: context: . - dockerfile: Dockerfile.dev + dockerfile: services/app/Dockerfile.dev restart: unless-stopped ports: - "5000:5000" - "5678:5678" volumes: - - .:/app + - ./services/app:/app env_file: - .env.local environment: diff --git a/lambda/Dockerfile.lambda b/lambda/Dockerfile.lambda index 4f93407..459eefb 100644 --- a/lambda/Dockerfile.lambda +++ b/lambda/Dockerfile.lambda @@ -2,7 +2,7 @@ # Uses the official Lambda Python base image which includes the # Runtime Interface Client (RIC) — no long-polling loop needed. # Lambda invokes handler.handler(event, context) directly per SQS batch. -FROM public.ecr.aws/lambda/python:3.12 +FROM --platform=linux/amd64 public.ecr.aws/lambda/python:3.12 COPY requirements.txt . RUN pip install -r requirements.txt -t . diff --git a/lambda/handler.py b/lambda/handler.py index 7f57b30..4dc2a9b 100644 --- a/lambda/handler.py +++ b/lambda/handler.py @@ -10,7 +10,19 @@ from sqlalchemy.orm import Session from sqlalchemy.pool import NullPool -DATABASE_URL = os.environ["DATABASE_URL"] + +def get_database_url() -> str: + secret_arn = os.environ.get("DATABASE_URL_SECRET_ARN") + if secret_arn: + sm = boto3.client( + "secretsmanager", + region_name=os.environ.get("AWS_DEFAULT_REGION", "us-east-1"), + ) + return sm.get_secret_value(SecretId=secret_arn)["SecretString"] + return os.environ["DATABASE_URL"] + + +DATABASE_URL = get_database_url() SQS_QUEUE_URL = os.environ["SQS_QUEUE_URL"] POLL_WAIT_SECONDS = int(os.environ.get("POLL_WAIT_SECONDS", "5")) diff --git a/launch_app_docker_image.sh b/launch_app_docker_image.sh index 51ef7d6..e95d30f 100644 --- a/launch_app_docker_image.sh +++ b/launch_app_docker_image.sh @@ -1,15 +1,17 @@ #!/bin/bash -# Only launches App container +# Builds and launches a single service container (no infrastructure). +# Usage: ./launch_app_docker_image.sh [service] (default: app) set -e -IMAGE_NAME="fastapi-boilerplate" +SERVICE="${1:-app}" +IMAGE_NAME="fastapi-boilerplate-${SERVICE}" PORT=5000 -echo "Building Docker image..." -docker build -t $IMAGE_NAME . +echo "Building Docker image for service: $SERVICE..." +docker build -f "services/${SERVICE}/Dockerfile" -t "$IMAGE_NAME" . echo "Starting container..." -docker run --rm -d -p $PORT:$PORT --name $IMAGE_NAME $IMAGE_NAME +docker run --rm -d -p $PORT:$PORT --name "$IMAGE_NAME" "$IMAGE_NAME" echo "Waiting for server to start..." sleep 2 diff --git a/migrations/env.py b/migrations/env.py index 72b6974..66994ac 100644 --- a/migrations/env.py +++ b/migrations/env.py @@ -14,8 +14,8 @@ logger = logging.getLogger("alembic.env") # Import Base and all models so autogenerate can detect changes -from app.extensions import Base # noqa: E402 import app.models # noqa: E402, F401 +from app.extensions import Base # noqa: E402 target_metadata = Base.metadata diff --git a/migrations/versions/0001_initial.py b/migrations/versions/0001_initial.py index 9797fae..5ad3733 100644 --- a/migrations/versions/0001_initial.py +++ b/migrations/versions/0001_initial.py @@ -47,7 +47,9 @@ def upgrade(): sa.Column("processed_at", sa.DateTime(timezone=True), nullable=True), sa.PrimaryKeyConstraint("id"), ) - op.create_index("ix_events_sqs_message_id", "events", ["sqs_message_id"], unique=True) + op.create_index( + "ix_events_sqs_message_id", "events", ["sqs_message_id"], unique=True + ) def downgrade(): diff --git a/pyproject.toml b/pyproject.toml index fcb0f42..e6c0f55 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.pytest.ini_options] testpaths = ["tests"] -pythonpath = ["."] +pythonpath = ["services/app"] filterwarnings = [ "ignore::DeprecationWarning:fastapi", "ignore::DeprecationWarning:starlette", @@ -22,6 +22,7 @@ select = [ ] ignore = [ "E501", # line too long — handled by the formatter + "B008", # Depends() in argument defaults is the standard FastAPI DI pattern ] [tool.ruff.lint.isort] diff --git a/requirements.txt b/requirements.txt index 1dc3af0..33a5771 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,4 +1,4 @@ -fastapi>=0.115.0,<0.116.0 +fastapi>=0.116.0 uvicorn[standard]>=0.30.0 SQLAlchemy>=2.0.0 alembic>=1.13.0 diff --git a/Dockerfile b/services/app/Dockerfile similarity index 58% rename from Dockerfile rename to services/app/Dockerfile index 45c7b71..b0ad88b 100644 --- a/Dockerfile +++ b/services/app/Dockerfile @@ -4,11 +4,17 @@ WORKDIR /app COPY requirements.txt . +RUN apt-get update && apt-get install -y --no-install-recommends curl && rm -rf /var/lib/apt/lists/* + RUN pip install --no-cache-dir -r requirements.txt -COPY . . +COPY services/app/app/ app/ +COPY migrations/ migrations/ +COPY alembic.ini . +COPY wsgi.py . RUN addgroup --system app && adduser --system --ingroup app app +ENV HOME=/tmp USER app EXPOSE 5000 diff --git a/Dockerfile.dev b/services/app/Dockerfile.dev similarity index 100% rename from Dockerfile.dev rename to services/app/Dockerfile.dev diff --git a/app/api/__init__.py b/services/app/alembic.ini similarity index 100% rename from app/api/__init__.py rename to services/app/alembic.ini diff --git a/app/__init__.py b/services/app/app/__init__.py similarity index 99% rename from app/__init__.py rename to services/app/app/__init__.py index e3317a8..850b589 100644 --- a/app/__init__.py +++ b/services/app/app/__init__.py @@ -14,7 +14,6 @@ from .logging.loki_logger import LokiLogger from .services.cache_service import CacheService - REST_API_V = os.environ.get("REST_API_V", "v1") api_module = importlib.import_module(f".api.{REST_API_V}", package=__name__) diff --git a/app/api/utils/__init__.py b/services/app/app/api/__init__.py similarity index 100% rename from app/api/utils/__init__.py rename to services/app/app/api/__init__.py diff --git a/app/graphql_api/types/__init__.py b/services/app/app/api/utils/__init__.py similarity index 100% rename from app/graphql_api/types/__init__.py rename to services/app/app/api/utils/__init__.py diff --git a/app/api/utils/utils.py b/services/app/app/api/utils/utils.py similarity index 100% rename from app/api/utils/utils.py rename to services/app/app/api/utils/utils.py diff --git a/app/api/v1/__init__.py b/services/app/app/api/v1/__init__.py similarity index 100% rename from app/api/v1/__init__.py rename to services/app/app/api/v1/__init__.py diff --git a/app/api/v1/auth.py b/services/app/app/api/v1/auth.py similarity index 100% rename from app/api/v1/auth.py rename to services/app/app/api/v1/auth.py diff --git a/app/api/v1/cache_test.py b/services/app/app/api/v1/cache_test.py similarity index 81% rename from app/api/v1/cache_test.py rename to services/app/app/api/v1/cache_test.py index af76c65..f8bc279 100644 --- a/app/api/v1/cache_test.py +++ b/services/app/app/api/v1/cache_test.py @@ -15,7 +15,10 @@ def cache_ping(request: Request): cache = getattr(request.app.state, "cache", None) if cache is None: return rest_api_response( - success=False, message="Redis not configured", status_code=503, request=request + success=False, + message="Redis not configured", + status_code=503, + request=request, ) return rest_api_response( data={"redis": "ok" if cache.ping() else "unavailable"}, request=request @@ -27,7 +30,10 @@ def cache_get(request: Request): cache = getattr(request.app.state, "cache", None) if cache is None: return rest_api_response( - success=False, message="Redis not configured", status_code=503, request=request + success=False, + message="Redis not configured", + status_code=503, + request=request, ) cached = cache.get(CACHE_KEY) @@ -55,7 +61,10 @@ def cache_invalidate(request: Request): cache = getattr(request.app.state, "cache", None) if cache is None: return rest_api_response( - success=False, message="Redis not configured", status_code=503, request=request + success=False, + message="Redis not configured", + status_code=503, + request=request, ) deleted = cache.delete(CACHE_KEY) diff --git a/app/api/v1/events.py b/services/app/app/api/v1/events.py similarity index 100% rename from app/api/v1/events.py rename to services/app/app/api/v1/events.py diff --git a/app/api/v1/health.py b/services/app/app/api/v1/health.py similarity index 100% rename from app/api/v1/health.py rename to services/app/app/api/v1/health.py diff --git a/app/api/v1/media.py b/services/app/app/api/v1/media.py similarity index 100% rename from app/api/v1/media.py rename to services/app/app/api/v1/media.py diff --git a/app/config.py b/services/app/app/config.py similarity index 100% rename from app/config.py rename to services/app/app/config.py diff --git a/app/database.py b/services/app/app/database.py similarity index 100% rename from app/database.py rename to services/app/app/database.py diff --git a/app/extensions.py b/services/app/app/extensions.py similarity index 100% rename from app/extensions.py rename to services/app/app/extensions.py diff --git a/app/graphql_api/__init__.py b/services/app/app/graphql_api/__init__.py similarity index 100% rename from app/graphql_api/__init__.py rename to services/app/app/graphql_api/__init__.py diff --git a/app/graphql_api/context.py b/services/app/app/graphql_api/context.py similarity index 94% rename from app/graphql_api/context.py rename to services/app/app/graphql_api/context.py index 4325a85..6aa9f14 100644 --- a/app/graphql_api/context.py +++ b/services/app/app/graphql_api/context.py @@ -1,9 +1,10 @@ -from typing import TYPE_CHECKING, Any +from typing import TYPE_CHECKING from strawberry.fastapi import BaseContext if TYPE_CHECKING: from sqlalchemy.orm import Session + from app.logging.logger import AppLogger from app.services.cache_service import CacheService diff --git a/app/graphql_api/resolvers/__init__.py b/services/app/app/graphql_api/resolvers/__init__.py similarity index 100% rename from app/graphql_api/resolvers/__init__.py rename to services/app/app/graphql_api/resolvers/__init__.py diff --git a/app/graphql_api/resolvers/auth.py b/services/app/app/graphql_api/resolvers/auth.py similarity index 100% rename from app/graphql_api/resolvers/auth.py rename to services/app/app/graphql_api/resolvers/auth.py diff --git a/app/graphql_api/resolvers/cache_test.py b/services/app/app/graphql_api/resolvers/cache_test.py similarity index 100% rename from app/graphql_api/resolvers/cache_test.py rename to services/app/app/graphql_api/resolvers/cache_test.py diff --git a/app/graphql_api/resolvers/events.py b/services/app/app/graphql_api/resolvers/events.py similarity index 100% rename from app/graphql_api/resolvers/events.py rename to services/app/app/graphql_api/resolvers/events.py diff --git a/app/graphql_api/resolvers/health.py b/services/app/app/graphql_api/resolvers/health.py similarity index 100% rename from app/graphql_api/resolvers/health.py rename to services/app/app/graphql_api/resolvers/health.py diff --git a/app/graphql_api/resolvers/media.py b/services/app/app/graphql_api/resolvers/media.py similarity index 82% rename from app/graphql_api/resolvers/media.py rename to services/app/app/graphql_api/resolvers/media.py index 0b6f420..50f180f 100644 --- a/app/graphql_api/resolvers/media.py +++ b/services/app/app/graphql_api/resolvers/media.py @@ -7,6 +7,12 @@ from app.security import get_token_from_bearer, verify_access_token from app.services.aws_s3_service import get_presigned_url, upload_file +_ALLOWED_EXTENSIONS = {"jpg", "jpeg", "png", "gif", "webp", "pdf", "mp4", "mov"} + + +def _allowed_file(filename: str) -> bool: + return "." in filename and filename.rsplit(".", 1)[1].lower() in _ALLOWED_EXTENSIONS + @strawberry.type class MediaQueries: @@ -51,8 +57,15 @@ def upload_file( except Exception as e: return Response(success=False, message="Unauthorized", exc=e) + if not file.filename or not _allowed_file(file.filename): + allowed = ", ".join(sorted(_ALLOWED_EXTENSIONS)) + return Response( + success=False, + message=f"File type not allowed. Permitted: {allowed}", + ) + try: - s3_key = upload_file(file, user_id, file.filename) + s3_key = upload_file(file.file, user_id, file.filename) except Exception as e: return Response(success=False, message="File upload failed", exc=e) diff --git a/app/graphql_api/schema.py b/services/app/app/graphql_api/schema.py similarity index 100% rename from app/graphql_api/schema.py rename to services/app/app/graphql_api/schema.py diff --git a/app/logging/__init__.py b/services/app/app/graphql_api/types/__init__.py similarity index 100% rename from app/logging/__init__.py rename to services/app/app/graphql_api/types/__init__.py diff --git a/app/graphql_api/types/types.py b/services/app/app/graphql_api/types/types.py similarity index 100% rename from app/graphql_api/types/types.py rename to services/app/app/graphql_api/types/types.py diff --git a/app/graphql_api/utils.py b/services/app/app/graphql_api/utils.py similarity index 100% rename from app/graphql_api/utils.py rename to services/app/app/graphql_api/utils.py diff --git a/app/utils/__init__.py b/services/app/app/logging/__init__.py similarity index 100% rename from app/utils/__init__.py rename to services/app/app/logging/__init__.py diff --git a/app/logging/cloudwatch_logger.py b/services/app/app/logging/cloudwatch_logger.py similarity index 100% rename from app/logging/cloudwatch_logger.py rename to services/app/app/logging/cloudwatch_logger.py diff --git a/app/logging/data_filter.py b/services/app/app/logging/data_filter.py similarity index 100% rename from app/logging/data_filter.py rename to services/app/app/logging/data_filter.py diff --git a/app/logging/logger.py b/services/app/app/logging/logger.py similarity index 100% rename from app/logging/logger.py rename to services/app/app/logging/logger.py diff --git a/app/logging/loki_logger.py b/services/app/app/logging/loki_logger.py similarity index 100% rename from app/logging/loki_logger.py rename to services/app/app/logging/loki_logger.py diff --git a/app/logging/sentry_logger.py b/services/app/app/logging/sentry_logger.py similarity index 100% rename from app/logging/sentry_logger.py rename to services/app/app/logging/sentry_logger.py diff --git a/app/models/__init__.py b/services/app/app/models/__init__.py similarity index 100% rename from app/models/__init__.py rename to services/app/app/models/__init__.py diff --git a/app/models/event.py b/services/app/app/models/event.py similarity index 100% rename from app/models/event.py rename to services/app/app/models/event.py diff --git a/app/models/media.py b/services/app/app/models/media.py similarity index 100% rename from app/models/media.py rename to services/app/app/models/media.py diff --git a/app/models/user.py b/services/app/app/models/user.py similarity index 100% rename from app/models/user.py rename to services/app/app/models/user.py diff --git a/app/security.py b/services/app/app/security.py similarity index 95% rename from app/security.py rename to services/app/app/security.py index bf9583a..f9b3bee 100644 --- a/app/security.py +++ b/services/app/app/security.py @@ -7,7 +7,9 @@ from app.config import config -bearer = HTTPBearer(auto_error=False) # auto_error=False lets us raise 401 instead of 403 +bearer = HTTPBearer( + auto_error=False +) # auto_error=False lets us raise 401 instead of 403 def settings(): @@ -68,7 +70,7 @@ def get_token_from_bearer(auth_header: str) -> str: """Extract raw JWT from 'Bearer ' string. Raises ValueError if malformed.""" if not auth_header.startswith("Bearer "): raise ValueError("Missing or invalid Authorization header") - return auth_header[len("Bearer "):] + return auth_header[len("Bearer ") :] # ── FastAPI dependency helpers ────────────────────────────────────────────── diff --git a/app/services/aws_s3_service.py b/services/app/app/services/aws_s3_service.py similarity index 100% rename from app/services/aws_s3_service.py rename to services/app/app/services/aws_s3_service.py diff --git a/app/services/aws_sqs_service.py b/services/app/app/services/aws_sqs_service.py similarity index 86% rename from app/services/aws_sqs_service.py rename to services/app/app/services/aws_sqs_service.py index b8c2381..42d69ea 100644 --- a/app/services/aws_sqs_service.py +++ b/services/app/app/services/aws_sqs_service.py @@ -26,7 +26,9 @@ def send_event(event_type: str, payload: dict) -> str: """Publish an event to the SQS queue. Returns the SQS MessageId.""" queue_url = os.environ["SQS_QUEUE_URL"] sqs = make_sqs_client() - ensure_queue(sqs, queue_url) + if os.getenv("AWS_SQS_ENDPOINT_URL"): + # LocalStack only — queue is pre-created by Terraform in production + ensure_queue(sqs, queue_url) body = json.dumps({"type": event_type, "payload": payload}) response = sqs.send_message(QueueUrl=queue_url, MessageBody=body) return response["MessageId"] diff --git a/app/services/cache_service.py b/services/app/app/services/cache_service.py similarity index 100% rename from app/services/cache_service.py rename to services/app/app/services/cache_service.py diff --git a/services/app/app/utils/__init__.py b/services/app/app/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/run.py b/services/app/run.py similarity index 100% rename from run.py rename to services/app/run.py diff --git a/terraform/environments/dev.tfvars.example b/terraform/environments/dev.tfvars.example new file mode 100644 index 0000000..f2a00a4 --- /dev/null +++ b/terraform/environments/dev.tfvars.example @@ -0,0 +1,31 @@ +environment = "dev" +github_org = "YOUR_GITHUB_ORG" +github_repo = "YOUR_GITHUB_REPO" +tf_state_bucket = "YOUR_ORG-fastapi-boilerplate-tfstate" +tf_state_lock_table = "YOUR_ORG-fastapi-boilerplate-tfstate-lock" + +# Bootstrap order: +# 1. terraform apply -target=module.ecr +# 2. Push images to ECR (or run the deploy workflow manually) +# 3. Set the two values below and run terraform apply +app_image = "ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/fastapi-boilerplate/app:latest" +worker_image = "ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/fastapi-boilerplate/worker:latest" + +# Smallest sizes — cost optimised for dev +rds_instance_class = "db.t3.micro" +rds_deletion_protection = false +rds_backup_retention = 1 +rds_multi_az = false +rds_skip_final_snapshot = true + +redis_node_type = "cache.t3.micro" + +ecs_cpu = 512 +ecs_memory = 1024 +ecs_desired_count = 1 + +# Leave empty for HTTP-only in dev (no certificate needed) +certificate_arn = "" + +# Higher limit to reduce noise during development +waf_rate_limit = 2000 diff --git a/terraform/environments/prod.tfvars.example b/terraform/environments/prod.tfvars.example new file mode 100644 index 0000000..d69bea6 --- /dev/null +++ b/terraform/environments/prod.tfvars.example @@ -0,0 +1,27 @@ +environment = "prod" +github_org = "YOUR_GITHUB_ORG" +github_repo = "YOUR_GITHUB_REPO" +tf_state_bucket = "YOUR_ORG-fastapi-boilerplate-tfstate" +tf_state_lock_table = "YOUR_ORG-fastapi-boilerplate-tfstate-lock" + +app_image = "ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/fastapi-boilerplate/app:latest" +worker_image = "ACCOUNT_ID.dkr.ecr.us-east-1.amazonaws.com/fastapi-boilerplate/worker:latest" + +# RDS: Multi-AZ, backups enabled, deletion protection on +rds_instance_class = "db.t3.small" +rds_deletion_protection = true +rds_backup_retention = 7 +rds_multi_az = true +rds_skip_final_snapshot = false + +redis_node_type = "cache.t3.micro" + +# Two tasks for availability +ecs_cpu = 1024 +ecs_memory = 2048 +ecs_desired_count = 2 + +# Required for prod — provision via ACM before applying +certificate_arn = "arn:aws:acm:us-east-1:ACCOUNT_ID:certificate/YOUR_CERT_ID" + +waf_rate_limit = 1000 diff --git a/terraform/main.tf b/terraform/main.tf index b6fd108..583153d 100644 --- a/terraform/main.tf +++ b/terraform/main.tf @@ -184,12 +184,12 @@ module "lambda" { app_name = var.app_name environment = var.environment - aws_region = var.aws_region worker_image = var.worker_image role_arn = module.iam.lambda_role_arn security_group_id = module.networking.lambda_security_group_id subnet_ids = module.networking.private_subnet_ids sqs_queue_arn = module.sqs.queue_arn + sqs_queue_url = module.sqs.queue_url database_url_secret_arn = module.rds.database_url_secret_arn redis_url = module.elasticache.redis_url s3_bucket = module.s3.bucket_name diff --git a/terraform/modules/ecr/main.tf b/terraform/modules/ecr/main.tf index b38d0b3..2f78dec 100644 --- a/terraform/modules/ecr/main.tf +++ b/terraform/modules/ecr/main.tf @@ -18,6 +18,7 @@ locals { resource "aws_ecr_repository" "app" { name = "${local.prefix}/app" image_tag_mutability = "MUTABLE" + force_delete = true image_scanning_configuration { scan_on_push = true diff --git a/terraform/modules/ecr/outputs.tf b/terraform/modules/ecr/outputs.tf index 6124763..64d9999 100644 --- a/terraform/modules/ecr/outputs.tf +++ b/terraform/modules/ecr/outputs.tf @@ -2,6 +2,10 @@ output "app_repository_url" { value = aws_ecr_repository.app.repository_url } +output "app_repository_name" { + value = aws_ecr_repository.app.name +} + output "app_repository_arn" { value = aws_ecr_repository.app.arn } @@ -10,6 +14,10 @@ output "worker_repository_url" { value = aws_ecr_repository.worker.repository_url } +output "worker_repository_name" { + value = aws_ecr_repository.worker.name +} + output "worker_repository_arn" { value = aws_ecr_repository.worker.arn } diff --git a/terraform/modules/iam/main.tf b/terraform/modules/iam/main.tf index a6489c4..f992ba6 100644 --- a/terraform/modules/iam/main.tf +++ b/terraform/modules/iam/main.tf @@ -150,10 +150,13 @@ resource "aws_iam_role" "github_actions" { "token.actions.githubusercontent.com:aud" = "sts.amazonaws.com" } StringLike = { - # Scoped to main and develop branches of the specific repo + # Branch triggers (workflow_dispatch, push) and environment-scoped jobs. + # Jobs with `environment:` set use the environment: sub form, not ref:. "token.actions.githubusercontent.com:sub" = [ "repo:${var.github_org}/${var.github_repo}:ref:refs/heads/main", "repo:${var.github_org}/${var.github_repo}:ref:refs/heads/develop", + "repo:${var.github_org}/${var.github_repo}:environment:dev", + "repo:${var.github_org}/${var.github_repo}:environment:production", ] } } @@ -227,6 +230,7 @@ resource "aws_iam_role_policy" "github_actions" { Action = [ "lambda:UpdateFunctionCode", "lambda:GetFunction", + "lambda:GetFunctionConfiguration", ] Resource = "arn:aws:lambda:${var.aws_region}:${var.aws_account_id}:function:${local.prefix}-worker" }, diff --git a/terraform/modules/lambda/main.tf b/terraform/modules/lambda/main.tf index e2c0ab7..b433d85 100644 --- a/terraform/modules/lambda/main.tf +++ b/terraform/modules/lambda/main.tf @@ -22,9 +22,9 @@ resource "aws_lambda_function" "worker" { environment { variables = { - AWS_DEFAULT_REGION = var.aws_region - AWS_S3_BUCKET = var.s3_bucket - REDIS_URL = var.redis_url + AWS_S3_BUCKET = var.s3_bucket + REDIS_URL = var.redis_url + SQS_QUEUE_URL = var.sqs_queue_url # DATABASE_URL is fetched from Secrets Manager at runtime via boto3 DATABASE_URL_SECRET_ARN = var.database_url_secret_arn } diff --git a/terraform/modules/lambda/variables.tf b/terraform/modules/lambda/variables.tf index f9e38ba..3e98642 100644 --- a/terraform/modules/lambda/variables.tf +++ b/terraform/modules/lambda/variables.tf @@ -1,11 +1,11 @@ variable "app_name" { type = string } variable "environment" { type = string } -variable "aws_region" { type = string } variable "worker_image" { type = string } variable "role_arn" { type = string } variable "security_group_id" { type = string } variable "subnet_ids" { type = list(string) } variable "sqs_queue_arn" { type = string } +variable "sqs_queue_url" { type = string } variable "database_url_secret_arn" { type = string } variable "redis_url" { type = string } variable "s3_bucket" { type = string } diff --git a/terraform/modules/waf/main.tf b/terraform/modules/waf/main.tf index d1b4d93..595644d 100644 --- a/terraform/modules/waf/main.tf +++ b/terraform/modules/waf/main.tf @@ -46,6 +46,15 @@ resource "aws_wafv2_web_acl" "main" { managed_rule_group_statement { name = "AWSManagedRulesCommonRuleSet" vendor_name = "AWS" + + # SizeRestrictions_BODY blocks any body > 8 KB, which breaks file uploads. + # Nginx enforces client_max_body_size 50m, so WAF's check is redundant here. + rule_action_override { + name = "SizeRestrictions_BODY" + action_to_use { + count {} + } + } } } diff --git a/terraform/outputs.tf b/terraform/outputs.tf index 75a4708..297094b 100644 --- a/terraform/outputs.tf +++ b/terraform/outputs.tf @@ -46,6 +46,16 @@ output "github_actions_role_arn" { # ── ECR ────────────────────────────────────────────────────────────────────── +output "ecr_app_repository_name" { + description = "→ GitHub var: ECR_APP_REPO" + value = module.ecr.app_repository_name +} + +output "ecr_worker_repository_name" { + description = "→ GitHub var: ECR_WORKER_REPO" + value = module.ecr.worker_repository_name +} + output "ecr_app_repository_url" { description = "ECR URL for the FastAPI app — use in app_image tfvar after first push." value = module.ecr.app_repository_url diff --git a/tests/app/e2e/test_e2e.py b/tests/app/e2e/test_e2e.py index c216906..5d9915f 100644 --- a/tests/app/e2e/test_e2e.py +++ b/tests/app/e2e/test_e2e.py @@ -10,6 +10,7 @@ """ import io +import uuid def test_health_check(http, base_url): @@ -21,7 +22,10 @@ def test_health_check(http, base_url): def test_auth_register_and_login(http, base_url): - creds = {"email": "smoke-auth@ci-test.internal", "password": "SmokeAuth1!"} + creds = { + "email": f"smoke-{uuid.uuid4().hex[:8]}@ci-test.internal", + "password": "SmokeAuth1!", + } assert http.post(f"{base_url}/auth/register", json=creds).status_code == 201 res = http.post(f"{base_url}/auth/login", json=creds) @@ -40,7 +44,7 @@ def test_media_upload_and_get_presigned_url(http, base_url, auth_headers): res = http.post( f"{base_url}/media/upload", headers=auth_headers, - files={"file": ("e2e.txt", io.BytesIO(b"e2e smoke content"), "text/plain")}, + files={"file": ("e2e.png", io.BytesIO(b"e2e smoke content"), "image/png")}, ) assert res.status_code == 201 data = res.json()["data"] diff --git a/tests/app/integration/conftest.py b/tests/app/integration/conftest.py index 48473a2..59f903b 100644 --- a/tests/app/integration/conftest.py +++ b/tests/app/integration/conftest.py @@ -65,8 +65,8 @@ def override_get_db(): @pytest.fixture(autouse=True) def clean_tables(test_engine): yield + from app.extensions import Base - from sqlalchemy import text with test_engine.connect() as conn: for table in reversed(Base.metadata.sorted_tables): diff --git a/tests/app/integration/test_events.py b/tests/app/integration/test_events.py index 094e564..14860fc 100644 --- a/tests/app/integration/test_events.py +++ b/tests/app/integration/test_events.py @@ -58,9 +58,7 @@ def test_returns_stored_events(self, client, auth_headers, db_session): def test_response_contains_required_fields(self, client, auth_headers, db_session): from app.models.event import Event - db_session.add( - Event(sqs_message_id="msg-002", type="ping", status="processed") - ) + db_session.add(Event(sqs_message_id="msg-002", type="ping", status="processed")) db_session.commit() data = client.get("/api/v1/events", headers=auth_headers).json()["data"][0] diff --git a/tests/app/integration/test_graphql_auth.py b/tests/app/integration/test_graphql_auth.py index 8d35d75..f19573f 100644 --- a/tests/app/integration/test_graphql_auth.py +++ b/tests/app/integration/test_graphql_auth.py @@ -43,9 +43,9 @@ def test_duplicate_email(self, gql): assert "already registered" in payload["message"] def test_missing_password(self, gql): - payload = gql(_REGISTER, {"email": "a@b.com", "password": ""}).json()[ - "data" - ]["register"] + payload = gql(_REGISTER, {"email": "a@b.com", "password": ""}).json()["data"][ + "register" + ] assert payload["success"] is False def test_email_normalized_to_lowercase(self, gql, client): @@ -64,9 +64,9 @@ def test_success_returns_both_tokens(self, gql, registered_user): assert payload["data"]["refreshToken"] def test_wrong_password(self, gql, registered_user): - payload = gql(_LOGIN, {**registered_user, "password": "wrong"}).json()[ - "data" - ]["login"] + payload = gql(_LOGIN, {**registered_user, "password": "wrong"}).json()["data"][ + "login" + ] assert payload["success"] is False assert "Invalid credentials" in payload["message"] diff --git a/tests/app/integration/test_graphql_media.py b/tests/app/integration/test_graphql_media.py index b653699..8f8a1f9 100644 --- a/tests/app/integration/test_graphql_media.py +++ b/tests/app/integration/test_graphql_media.py @@ -165,9 +165,9 @@ def test_success_returns_media_payload(self, client, gql_auth_headers): return_value=_FAKE_URL, ), ): - payload = _upload_file(client, gql_auth_headers["access"]).json()[ - "data" - ]["uploadFile"] + payload = _upload_file(client, gql_auth_headers["access"]).json()["data"][ + "uploadFile" + ] assert payload["success"] is True assert payload["data"]["mediaId"] @@ -178,8 +178,8 @@ def test_s3_failure_returns_error(self, client, gql_auth_headers): "app.graphql_api.resolvers.media.upload_file", side_effect=Exception("S3 down"), ): - payload = _upload_file(client, gql_auth_headers["access"]).json()[ - "data" - ]["uploadFile"] + payload = _upload_file(client, gql_auth_headers["access"]).json()["data"][ + "uploadFile" + ] assert payload["success"] is False assert "upload failed" in payload["message"] diff --git a/tests/app/integration/test_media.py b/tests/app/integration/test_media.py index 50ef4c3..07c38c1 100644 --- a/tests/app/integration/test_media.py +++ b/tests/app/integration/test_media.py @@ -51,7 +51,9 @@ def test_s3_failure_returns_500(self, client, auth_headers): res = client.post( "/api/v1/media/upload", headers=auth_headers, - files={"file": ("f.jpg", io.BytesIO(b"data"), "application/octet-stream")}, + files={ + "file": ("f.jpg", io.BytesIO(b"data"), "application/octet-stream") + }, ) assert res.status_code == 500 diff --git a/tests/app/unit/api/test_utils.py b/tests/app/unit/api/test_utils.py index 28b41a7..3c0cbe1 100644 --- a/tests/app/unit/api/test_utils.py +++ b/tests/app/unit/api/test_utils.py @@ -2,7 +2,6 @@ import os from unittest.mock import MagicMock -import pytest from starlette.requests import Request os.environ.setdefault("SECRET_KEY", "test-secret-key-minimum-32-bytes!!!!") @@ -45,7 +44,9 @@ def test_default_returns_200_success(self): assert body["data"] == {} def test_failure_with_custom_status(self): - response = rest_api_response(success=False, message="Not found", status_code=404) + response = rest_api_response( + success=False, message="Not found", status_code=404 + ) assert response.status_code == 404 body = _parse(response) assert body["success"] is False @@ -69,7 +70,9 @@ def test_response_always_has_three_keys(self): def test_logger_adapter_called_when_present(self): mock_adapter = MagicMock() - rest_api_response(success=True, message="ok", request=_make_request(mock_adapter)) + rest_api_response( + success=True, message="ok", request=_make_request(mock_adapter) + ) mock_adapter.log.assert_called_once() _, kwargs = mock_adapter.log.call_args assert kwargs["level"] == AppLogger.Level.INFO diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000..8c3cfef --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,5 @@ +import os + +# Config._require() runs at class definition time, so SECRET_KEY must be in the +# environment before any `import app.*` happens during test collection. +os.environ.setdefault("SECRET_KEY", "test-secret-key-not-for-production")