Skip to content

cheeyeo/FastAPI-application-example

Repository files navigation

FastAPI project

This repo hosts a FastAPI application that generates random numbers between a min and max value.

It is also an experiment in using OAuth2 for API authentication and authorization via AWS Cognito.

Run on localhost

Create a AWS Cognito User Pool with an optional PreToken Generation Lambda trigger.

Get the app client ID and secret; user pool ID and region.

Create .env file with following values:

export RDS_USERNAME="xxx"
export RDS_DB_NAME="xxx"
export RDS_PASSWORD="xxx"
export RDS_HOSTNAME="localhost"
export RDS_PORT=5432
export ENV_TYPE="dev"
export SECRET_KEY="XXXXXX"
export AWS_REGION="eu-west-2"
export AWS_COGNITO_APP_CLIENT_ID="XXXXX"
export AWS_COGNITO_APP_CLIENT_SECRET="XXXXX"
export AWS_USER_POOL_ID="XXXX"

Run docker-compose with watch enabled:

docker compose -f compose.yml up

Go to http://localhost:4000/docs to view the Swagger UI

Installing ruff

Ref: https://docs.astral.sh/ruff/tutorial/#rule-selection

uv add --dev ruff

uv run ruff check --fix app/

uv run ruff format app/

https://github.com/astral-sh/uv-docker-example/blob/main/Dockerfile

Ref

https://realpython.com/fastapi-python-web-apis/

Full app with database: https://github.com/fastapi/full-stack-fastapi-template/tree/master/backend

TUTORIAL

OAUTH2

ON using alembic to generate migrations

  • Run alembic init alembic

  • To setup autogeneration for models:

    • Comment out sqlalchemy.url option in alembic.ini
    • In alembic/env.py, update target_metadata to point to SQLModel.metadata
    • Import the postgresql url and use it in run_migrations_offline and run_migrations_online
  • Run `alembic revision --autogenerate -m "MESSAGE" to detect and create the migrations based on existing model fields....

  • Run alembic upgrade head to run migrations

About

Example of creating a FastAPI application using uv, docker

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published