This is an example FastAPI project that used the code from here: Example Repo I will try to develop this project as much as possible for the common use cases.
Also sqlmodel, pydantic, alembic, poetry, ...
- Infrastructure: the common services that every backend needs, served in local by Docker Compose.
- Easy: all the commands ready by Makefile.
- Fast: thanks to Fastapi and async programming.
- Async: Celery using RabbitMQ as broker and Redis as backend.
- ORM: custom sqlmodel orm as django orm and mongoengine.
- Authentication: OAuth2 with access/refresh tokens.
- Admin dashboard: custom admin dashboard as django by sqladmin.
- Frontend friendly: auto generation of SDK Typescript client.
- Role Management: User based and role based role management will be added.
- Logging: Request logging will be added. This can be basic request logging with current db or elastichsearch.
- Email Service: Mail service will be added for basic usage.
- Storage Service: File storage service like minio, S3, etc. This will be generic so you can easily adapt your own service. Just implementing your functions.
- Caching: Caching mechanism will be added.
-
Clone the repo
-
Create a virtual environment:
poetry shell
- Install the requirements with Poetry for developing, testing and debugging purposes.
make install
Build and run the Docker services for using in Local.
make run
Congrats! the API is working at this point, you can check:
- Docs: http://localhost:8000/docs
- Admin: http://localhost:8000/admin
- RabbitMQ: http://localhost:15672/
For admin, use:
ADMIN_USER=superuser
ADMIN_PASS=admin
For generating the SDK frontend client (the app should be running):
make generate_sdk
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:
- Create a new Class based in ModelCore with
table=True
from app.core.base.models import ModelCore
class NewModel(ModelCore, table=True):
unique_property: str
- Import the new class into the migration model file
app.core.db.migrations.models
- Create a new migration
- Create an AdminModel in
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
- Append it in
admin_models
intoapp.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
:
- Password hash:
- SECRET_KEY: run in the terminal
openssl rand -base64 32
to generate a new one
- SECRET_KEY: run in the terminal
- Admin superuser:
- ADMIN_USER
- ADMIN_PASS
Also, it is possible you want to modify the expiry time of access/refresh tokens.
Install poetry
pip install poetry
To initialize the poetry in project
poertry init
if it is already exist. Then type
poertry install
To see poetry env informations or just path
poetry env info
poetry env info -p
To create virtualenv isinde project directory use this otherwise the virtualenv file creates in a different folder in your system
poetry config virtualenvs.in-project true
To get inside virtualenv
poetry shell
To exit from virtualenv
deactivate
Or
exit
To add dependency
poetry add requests
To remove dependency
poetry remove requests
To use different version of python
poetry env use /full/path/to/python
if the python added to PATH then just type
poetry env use python3.7