- FastAPI: High-performance web framework for building the Python backend API.
- Pydantic: Used by FastAPI for data validation and settings management, providing clear, error-checked data handling.
- SQLAlchemy: Python ORM for database interactions, making it easier to work with PostgreSQL.
- PostgreSQL: Reliable SQL database for structured data storage.
- pgcrypto: PostgreSQL extension for column-specific encryption.
- Docker Compose: Simplifies both development and production setups with container orchestration.
- bcrypt: For secure password hashing using the BCrypt SHA256 algorithm.
- JWT (JSON Web Token): Provides secure, stateless user authentication.
- uv: Python package and environment management.
- setuptools and Cython: Tools used to compile and optimize Python code, generating platform-specific binary files for better performance.
-
Clone the repository:
git clone https://github.com/kishorninawe/fastapi-template.git cd fastapi-template
-
Configure environment variables:
Update configurations in the.env
file to customize settings such as database connection, secret keys, and other environment-specific configurations.
-
Build and run the application with Docker:
docker-compose up --build
-
Set up environment and install dependencies:
-
Create virtual environment & install dependencies:
-
Using
pip
:python -m venv .venv pip install -e .
-
Using
uv
:pip install uv uv sync
-
-
Activate virtual environment:
-
On Linux/macOS:
source .venv/bin/activate
-
On Windows:
.venv\Scripts\activate
-
-
Check database accessibility and create
pgcrypto
if not exists:-
On Linux/macOS:
PYTHONPATH=$(pwd):$PYTHONPATH python app/backend_pre_start.py
-
On Windows:
set PYTHONPATH=%cd%;%PYTHONPATH% && python app\backend_pre_start.py
-
-
Run migrations:
alembic upgrade head
-
Add initial data to the database:
-
On Linux/macOS:
PYTHONPATH=$(pwd):$PYTHONPATH python app/initial_data.py
-
On Windows:
set PYTHONPATH=%cd%;%PYTHONPATH% && python app\initial_data.py
-
-
Start the FastAPI server:
uvicorn app.main:app --port 8000 --reload
-
-
Access the application:
Once the server is running, access the API documentation at:
http://localhost:8000/docs
To run tests, use:
pytest
Tests are managed with pytest. You can modify or add new tests in the ./app/tests/
directory.
To check code style and quality, use:
ruff check .
Code style is managed with Ruff. You can configure or extend linting rules in the pyproject.toml
file.
To check for type errors, use:
mypy .
Type checking is managed with mypy. You can configure or customize type-checking rules in the pyproject.toml
file.
Alembic is used for managing database migrations. If you haven't set up Alembic yet, initialize it with:
-
Initialize Alembic:
alembic init app/alembic
Once initialized, to add or modify models, edit the ./app/models.py
file and then use the following commands:
-
Create a new migration (after modifying models or tables):
alembic revision --autogenerate -m "Description of migration"
-
Apply the latest migrations:
alembic upgrade head
-
Rollback the last migration:
alembic downgrade -1
Migration configurations can be customized in the alembic.ini
file.
The FastAPI Template is licensed under the terms of the MIT license.
This project is based on the Full Stack FastAPI Template and the Uvicorn-Gunicorn Docker by Sebastián Ramírez, both of which are licensed under the MIT License.
The original code and project structure were adapted to fit the needs of this project.