Skip to content

Latest commit

 

History

History
184 lines (136 loc) · 5.61 KB

README.md

File metadata and controls

184 lines (136 loc) · 5.61 KB

FastApi template

================

Description

My project description

GCP Project

Template Stack

Project Setup

  • Install Poetry

  • Set config for venv in local

    poetry config virtualenvs.in-project true
    poetry env use 3.11
    poetry shell
    poetry install --without prod
  • (Postgres only) Create and run required databases

    docker compose up -d
  • (Postgres only) Apply migrations from VSCode launcher

    # For migrations on Cloud SQL instance, ensure creating unix socket & starting Cloud SQL Proxy first
    # sudo mkdir /var/cloudsql && sudo chmod 777 /var/cloudsql
    # cloud-sql-proxy -u /var/cloudsql sandbox-ahourlier:europe-west9:fastapi-template-instance

Run locally

# WITHOUT DOCKER (Guess ADC from env)
uvicorn app.main:app --reload          # Or from VSCode launcher

# WITH DOCKER
Run the service named 'app' in docker-compose.yml

Tests

poetry run pytest --cov=app --cov-report=term     # Uses SQLALCHEMY_DATABASE_URI in pyproject.toml

Application Structure

fastapi_template
│
├── .cloudbuild                    - Cloud Build configuration
│   └── cloudbuild.yaml
│
├── .github                        
│   └── workflows                  - Github Actions
│
├── .vscode
│   ├── launch.json                - Launch to execute the app
│   └── tasks.json                 - For dockerized launch
│
├── Dockerfile.prod                - Used to build and deploy on Cloud Run
│
├── alembic.ini                    - Local Database configuration
│
├── app                            - Web stuffs
│   ├── api                           - Global deps & routing
│   └── core                          
│      ├── google_apis                  - Google API's utils
│      ├── cloud_logging.py             - Logging wrapper
│      ├── config.py                    - Global app configuration
│      └── google_clients.py            - Google API's client builders
│   │
│   ├── firestore                     - CRUD, Endpoints, Models for Firestore
│   ├── models                        - Common models for Firestore or PostgreSQL
│   ├── sqlmodel                      - CRUD, Endpoints, Models for SQLAlchemy
│   ├── main.py                       - Entrypoint, app instanciation & middleware
│   └── middleware.py                 - Middleware definitions (Metric, Logs, Exceptions)
│
├── iac                            - Terraform resources
│
├── migrations                     - PostgreSQL migrations
│
├── tests                          - PostgresSQL unit tests
│
├── deploy.sh                      - Deployment script
│
├── format.sh                      - Linting and formatting script
│
├── docker-compose.yml             - Provide database local containers
│
├── main.tf                        - Terraform configuration for deployment
│
└── pyproject.toml

Deployment

⚠️ Everything under this section assumes you specified a repository to push to, a gcloud project name, and answered 'yes' to "as_container" question.

Initialisation

First, make sure ADC is configured correctly.

Start a first deployment

gcloud components update && gcloud components install beta
./deploy.sh -e <dev|staging|prod> # Will get the correct .env.x file & inject variables

# Creates required resources & IAM permissions
# - Secret in Secret Manager filled with .env.dev
# - Cloud Storage bucket to store terraform state
# - Artifact registry repository to store Cloud Run images
# - Required IAM permissions for Cloud Build default SA
#     - run.admin
#     - artifactregistry.admin
#     - datastore.owner
#     - cloudsql.admin
#     - secretmanager.secretAccessor
#     - storage.admin
#     - serviceusage.serviceUsageAdmin
# 
# - Cloud Build trigger to run deployment on push

# Then it starts the Cloud Build trigger

Cloud Build is now ready to auto deploy new Cloud Run revision after each push

...or re-deploy the app

./deploy.sh -e <dev|staging|prod> # Will get the correct .env.x file & inject variables

# - Replaces secret version content if it differs from .env.x
# - Runs the existing Cloud Build trigger

CI/CD

CI with Github Actions

Enable Github Actions API in your repository

Actions are configured to run linting for every Pull Request on develop, uat and main branches

CD with Cloud Build & Terraform

On push, .cloudbuild/cloudbuild.yaml will:

  • Build and push new image
  • re-apply the iac/main.tf infrastructure to ensure consistency
  • Deploy the new Cloud Run revision

Use iac/main.tf to deploy new GCP resources if possible to make terraform aware of it

Api docs

Maintainers

Digital Lab [email protected]