⚙️ You can Generate Project Interactively Based on this template with the FastAPI Gen8 CLI Tool
This repository provides a clean and scalable template for building FastAPI applications. It is designed to help you start new projects quickly with best practices in mind.
-
📘 Organized project structure
-
🗒️ Predefined Environment Configuration with Pydantic-Settings
-
🛜 Dependency management Setup for Common Dependencies
get_db: Async Database Session Dependencyget_current_user: Async User dependency, Extract user Database with Access Token in request, raises 401 Http Exception if token is not valid
-
👤 Initial User Model and User Authentication Endpoints with Unit Tests
-
📝 Predefined Logging Configuration
-
⚙️ Unit Test Configuration with Pytest (With Async Support)
-
⏺️ Alembic Data Migration Configuration and alembic.ini
In order to get started with the FastAPI Project, follow the following steps
-
Activate Project Python Virtual Environment
source venv/bin/activate # this is for Unix systems
-
Create an .env file from the .env.example file and provide values for missing environment variables - 1. Update the DATABASE_URL to point at an accessible DATABASE server - 2. Update the MAIL_CONFIG section to include mail server credentials
-
Install
makeif you do not already have it and run the commandmake run-localto start you local server -
Apply Initial Database Migration for Ensure Database Connection string is valid
bash alembic upgrade head -
Ensure the Setup Is Complete and Sucessful by Running the following command
bash make test-localIf all the tests pass successfully you're good to start working on your project. -
Start Local Server with the following command
bash make run-local
- After making Changes to your Model(s) in the models/ directory, ensure the Model class is Imported in the init module of the models directory, this way, the configured alembic for your project can pick up models changes for Migrations
fastapi-project-structure/
.
├── Makefile
├── app
│ ├── __init__.py
│ ├── api_router.py
│ ├── database.py
│ ├── dependencies.py
│ ├── logger.py
│ ├── main.py
│ ├── middlewares.py
│ ├── models
│ │ ├── __init__.py
│ │ └── auth.py
│ ├── routers
│ │ ├── __init__.py
│ │ └── auth.py
│ ├── schemas
│ │ ├── __init__.py
│ │ └── auth.py
│ ├── services
│ │ ├── __init__.py
│ │ └── auth.py
│ └── settings.py
├── logs
└── requirements.txt
-
Clone the repository:
git clone https://github.com/brianobot/fastAPI_project_structure.git cd fastAPI_project_structure -
Create & Activate Virtual Environment to Manage Project Dependency In Isolation
python3 -m venv venv && source venv/bin/activate #for unix computers
-
Install dependencies:
pip install -r requirements.txt
-
Run the application:
make run-local # or uvicorn app.main:app --reload -
Access the API docs:
- Open http://localhost:8000/docs in your browser.
Run initial tests using pytest:
make test-local # or pytestor
Run Specific tests
pytest -s app/routers/tests/test_auth.pyCopy .env.example to .env and update the values as needed.
Contributions are welcome! Please open issues or submit pull requests.