Loading repository data…
Loading repository data…
ezeparziale / repository
:zap: Fastapi Api template
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
This template provides a robust starting point for building APIs with FastAPI. It includes user authentication, CRUD operations, JWT token-based authentication, and PostgreSQL integration. The setup is streamlined with Docker and includes comprehensive documentation and testing tools.
[!IMPORTANT] Min Python version: 3.14
Clone this repo:
git clone https://github.com/ezeparziale/fastapi-api-template
Create virtual environment:
python -m venv env
Activate environment:
. env/scripts/activate
. env/bin/activate
Upgrade pip:
python -m pip install --upgrade pip
Install requirements:
pip install -r requirements-dev.txt
Install pre-commit:
pre-commit install
Create .env file. Check the example .env.example
:globe_with_meridians: Google Auth credentials:
Create your app and obtain your client_id and secret:
https://developers.google.com/workspace/guides/create-credentials
:lock: How to create a secret key:
openssl rand -base64 64
:closed_lock_with_key: How to create an encryption key:
To create an encryption key for securing sensitive data, you can use the generate_key.py script provided in the repository. Run the following command:
python generate_key.py
This will generate a secure encryption key.
:construction: Before first run:
Run docker-compose :whale: to start the database server
docker compose -f "compose.yaml" up -d --build adminer db
and init the database with alembic:
alembic upgrade head
:key: Create a self-signed certificate with openssl:
openssl req -x509 -newkey rsa:4096 -nodes -out cert.pem -keyout key.pem -days 365
uvicorn app.main:app --reload --port 8000 --ssl-keyfile key.pem --ssl-certfile cert.pem
Run linter and formatter
scripts/lint.sh
scripts/format.sh
Run coverage
coverage run -m pytest
coverage report --show-missing
coverage html
Or run all in one with:
scripts/coverage.sh
Run pytest with coverage
coverage run -m pytest
or
scripts/test.sh
Alembic is used for database migrations. Below are some common commands to manage your database schema.
To autogenerate a new revision based on the changes detected in your models, run:
alembic revision --autogenerate -m "your message here"
To create a blank revision for custom migrations, run:
alembic revision -m "your message here"
To apply the latest migrations and upgrade the database schema, run:
alembic upgrade head
To revert the last migration and downgrade the database schema, run:
alembic downgrade -1
After creating a revision, you can edit the generated script to define your custom migrations.
This project includes a complete observability stack using Grafana, Loki, and Promtail.
Start the monitoring stack:
docker compose up -d grafana loki promtail
Access Grafana: Open http://localhost:3000 in your browser.
admin / adminAdd Loki Data Source:
http://loki:3100.To visualize logs efficiently, follow these steps to create a dashboard with a level filter:
levelCustomTRACE,DEBUG,INFO,SUCCESS,WARNING,ERROR,CRITICAL{container="app"} | json | record_level_name =~ "$level" | line_format "{{.record_message}}"
app container, parses the JSON format, filters by the selected $level variable, and cleans up the output message.