Skip to content

FastAPI template with JWT auth, PostgreSQL, and Docker Compose for rapid API development.

License

Notifications You must be signed in to change notification settings

aLmktr/fastapi-blueprint

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fastapi-blueprint-logo

FastAPI template with JWT auth, PostgreSQL, and Docker Compose for rapid API development.


Features

  • FastAPI: A high-performance web framework for building APIs with Python.
  • SQLAlchemy: An ORM tool. Following SQLAlchemy's version 2 approach for defining models
  • Alembic: Manages database migrations.
  • PostgreSQL: A robust and scalable database system.
  • Docker: Provides containerization for consistent development and deployment environments.
  • Unit Testing with Pytest: Ensures code quality and reliability through effective testing.
  • User Authentication with JWT: Implements secure user authentication and authorization.
  • Modular Structure: Organizes the project for maintainability and scalability.

Project Structure

The project is organized into a modular structure to promote scalability and maintainability.

📁 .
├── 📁 backend
│  ├── 📁 api
│  │  ├── 📁 auth
│  │  │  └── 📄 auth_routes.py
│  │  ├── 📁 core
│  │  │  ├── 📄 config.py
│  │  │  ├── 📄 database.py
│  │  │  └── 📄 security.py
│  │  └── 📁 user
│  │     ├── 📄 crud.py
│  │     ├── 📄 models.py
│  │     ├── 📄 schemas.py
│  │     └── 📄 user_routes.py
│  ├── ⚙️ alembic.ini
│  ├── 🐳 Dockerfile
│  ├── 📄 main.py
│  ├── 📁 migrations
│  ├── 📦 requirements.txt
│  └── 📁 tests
│     ├── 📄 conftest.py
│     ├── 📁 user
│     │  ├── 📄 test_crud.py
│     │  └── 📄 test_routes.py
│     └── 📁 utils
├── 🐳 compose.yaml
└── 📑 README.md

Installation

  1. Clone the repository:

    git clone https://github.com/aLmktr/fastapi-blueprint.git
  2. Step into the project:

    cd fastapi-blueprint 
  3. Update the .env.test file by setting the DB_PASS and SECRET_KEY (using hashes is recommended). Then, rename the file to .env

  4. Build and run the Docker containers:

    docker-compose up --build
  5. That's it! now you can find your API docs here localhost:8000/docs


Note

  • For security reasons, we have not included any method to create a superuser via the API. To create the first superuser, please follow these steps:
  1. Create a normal user using the create/ API endpoint.
  2. Access the database using Docker by running:
docker exec -it fastapi_blueprint_db psql -U root -d fastapi_blueprint_db
  1. Run the following SQL command to update the user role:
UPDATE users SET role='ADMIN' WHERE username='your_username_here';
  1. Exit the database by running \q.
  • Once these steps are completed, you are ready to proceed with managing your application.

Extending the App

Say, for example, you want to use this template for a shop and add functionality for managing items. You can do this by following these steps:

  1. Create a new folder for the items functionality inside api folder api/items organize the folder as follow:
└── 📁 items
     ├── 📄 __init__.py 
     ├── 📄 crud.py           
     ├── 📄 models.py         
     ├── 📄 schemas.py        
     └── 📄 item_routes.py  
  1. Write your database models: In the models.py file within the items folder, define your database models for the items functionality.

  2. Add model imports to Alembic for migrations: After writing your models, open the migrations/env.py file and add the following line under the user model import to ensure Alembic recognizes your items models:

"""
Import all models here to ensure they are included in the migration
"""
from api.user import models  # noqa
from api.items import models # noqa  <-------- this line 

target_metadata = Base.metadata
  1. Run the migration: Make sure your container is running, then apply the migrations by running:
alembic revision --autogenerate -m "Add items models"
alembic upgrade head 
  1. You're ready to go! Now you can start working on your new items functionality or any other feature for your app!

Contributing

If you have suggestions or improvements, please open an issue or submit a pull request. Contributions are welcome!


License

This project is licensed under the MIT License - see the LICENSE file for details.