๐๐จ FastAPI Rocket Boilerplate to build an API based in Python with its most modern technologies!
Also sqlmodel, pydantic, alembic, poetry, ...
Clone the repo
Create a virtual environment:
python3.11 -m venv venv
make install
If you are using VS Code, I recommend you to install some plugins:
โน๏ธ You can test the pre-commit without committing running
trunk check
To integrate Sentry for error monitoring, add the Sentry DSN (Data Source Name) to the .env
file. Set up your environment by signing in to Sentry to create a project and obtain the Sentry DSN.
In the .env
file, include the following variable:
SENTRY_DSN=your_sentry_dsn_here
With this configuration, errors will be captured and reported to your Sentry project for effective monitoring.
Build and run the Docker services for using in Locaql.
make run
Congrats! the API is working at this point, you can check:
For admin, use:
ADMIN_USER=superuser
ADMIN_PASS=admin
For generating the SDK frontend client (the app should be running):
make generate_sdk
You will find the generated client in generate_client/openapi.json
Run pytest with coverage for unit testing.
make test
You do not need to run inside Docker container.
The DB is replaced by a SQLite db in memory ๐
Use Alembic for DB migrations.
If you create a new model, import it in: app/core/db/migrations/models.py
After this, or modified a previous model, create the migration document:
docker-compose run app alembic revision --autogenerate -m "your commit"
If you are trying to do something complicated, maybe you need to fix the file manually.
Migration file should be created inside the Docker container because the DB url is referencing the Docker network domain.
Migrations will run when docker compose up, but you can run them manually:
docker-compose run app alembic upgread head
Basically, you will want to create new services that contain endpoints and models. And of course, it is almost completely sure you need to add new extra dependencies.
You can use the service user
as reference.
If you want to create a new model to be stored in the DB, you should follow these steps:
table=True
from app.core.base.models import ModelCore
class NewModel(ModelCore, table=True):
unique_property: str
app.core.db.migrations.models
app.services.admin.models
:from app.core.admin.models import ModelViewCore
class NewModelAdmin(ModelViewCore, model=NewModel):
# You can add config settings here for the Admin panel.
pass
admin_models
into app.services.admin.config
If you want to create a new view protected by auth, you should include the get_current_user
dependency.
Here you have an example of a new service with a protected route:
from fastapi import APIRouter, Depends
from app.core.auth.functions import get_current_user
router = APIRouter(
prefix="/security",
tags=["security"]
)
@router.get("/protected")
def protected_route(current_user: str = Depends(get_current_user)):
""" Endpoint for auth test"""
return {"message": f"ยกHola, {current_user}! This is a protected url and you are inside!"}
And then append the router in routers
into app.main
For creating new users, they can register by themselves or be added by Admin panel.
Use Poetry like:
poetry add <new_dependency>
You should change the next env vars in .env
:
openssl rand -base64 32
to generate a new oneAlso, it is possible you want to modify the expiry time of access/refresh tokens.