An open API service indexing awesome lists of open source software.

https://github.com/nnavales/fastapi-template

Backend modern template. Using FastAPI, SQLModel, PostgreSQL, Docker, GitHub Actions, and more.
https://github.com/nnavales/fastapi-template

api backend backend-api development fastapi production

Last synced: 21 days ago
JSON representation

Backend modern template. Using FastAPI, SQLModel, PostgreSQL, Docker, GitHub Actions, and more.

Awesome Lists containing this project

README

          

# FastAPI Project Template
A modern, production-ready template for building APIs with FastAPI, featuring OAuth2 authentication, SQLModel ORM, and comprehensive testing.

## Features

- ๐Ÿš€ FastAPI โ€“ High-performance, modern web framework for building APIs
- ๐Ÿ” OAuth2 Authentication โ€“ Secure login flow with Password and JWT token support
- ๐Ÿ“š SQLModel โ€“ Type-safe SQL database integration for Python
- ๐Ÿ˜ PostgreSQL โ€“ Robust, open-source relational database management system
- ๐Ÿงช Pytest โ€“ Powerful testing framework for test-driven development
- ๐Ÿ” Ruff โ€“ Lightning-fast Python linter for maintaining code quality
- ๐Ÿ‹ Docker โ€“ Simplified deployment with containerization
- โš™๏ธ GitHub Actions โ€“ Automated CI/CD pipeline for continuous integration and delivery
- โšก Async Endpoints โ€“ High-performance, non-blocking API endpoints
- ๐Ÿ”’ JWT Authentication โ€“ Secure, token-based authentication using OAuth2
- ๐Ÿ“Š SQLModel Integration โ€“ Type-safe database queries with SQLModel
- ๐Ÿ”„ Alembic โ€“ Database schema migrations for version control
- ๐Ÿงช Comprehensive Testing โ€“ Full test suite with Pytest for ensuring app stability
- ๐Ÿ“ Custom Logging โ€“ Tailored logging configuration for effective debugging and monitoring
- ๐Ÿ” Sentry โ€“ Real-time error tracking and alerting integration
- ๐Ÿ›ก๏ธ Secure Docker Configuration โ€“ Best practices for secure and efficient containerized deployment
- ๐Ÿ”ง Code Quality โ€“ Code enforcement with Ruff for cleaner and maintainable code
- ๐Ÿ“ฆ Fast Dependency Management โ€“ Streamlined project setup with uv for rapid development

---

## Prerequisites

- **Python 3.11+**
- `uv` package manager
- **Docker and Docker Compose**
- just (to run scripts faster)

---

## Project Structure

This project follows a modular approach, with separate directories for different components:

- **`app/`**: Contains the core application code
- **`auth/`**: Handles user authentication, JWT tokens, and security-related services
- **`config.py`**: Centralized configuration settings
- **`database.py`**: Database setup and connections
- **`models.py`**: SQLAlchemy and SQLModel ORM models
- **`main.py`**: Entry point for the FastAPI application
- **`routes.py`**: All endpoint route definitions
- **`tests/`**: Test suite using pytest for unit and integration testing
- **`alembic/`**: Database migration scripts managed by Alembic

### Key Files:
- **`main.py`**: FastAPI app initialization and middleware configuration
- **`config.py`**: Configuration file for environment variables
- **`database.py`**: Database connection and session management
- **`models.py`**: All ORM models (with SQLModel)
- **`routes.py`**: API routes and endpoints
- **`scripts/`**: Custom scripts for deployment and maintenance tasks

---

## Setup Guide

### 1. Clone the Repository

```bash
git clone https://github.com/navalesnahuel/fastapi-template.git
cd fastapi-template
```

### 2. Set Up the `.env` File

| Variable | Description | Dev Required | Deploy Required |
|---------------------------|-------------------------------------------|--------------|-----------------|
| `APP_VERSION` | Version of the application | Yes | Yes |
| `ENVIRONMENT` | Environment the application is running in | Yes | No |
| `DATABASE_URL` | Database connection string | Yes | Yes |
| `CORS_HEADERS` | Allowed headers for CORS requests | No | No |
| `CORS_ORIGINS` | Allowed origins for CORS requests | No | No |
| `FIRST_SUPERUSER` | Email for the first superuser | Yes | Yes |
| `FIRST_SUPERUSER_PASSWORD`| Password for the first superuser | Yes | Yes |
| `DOCKERHUB_USERNAME` | Docker Hub username (for deployment) | No | Yes |
| `DOCKERHUB_TOKEN` | Docker Hub authentication token | No | Yes |
| `VIRTUAL_HOST` | Primary domain for the application | No | Yes |
| `LETSENCRYPT_EMAIL` | Email for SSL certificates | No | Yes |

### 3. Running the Application
This project uses separate documentation files for development, deployment, and CI/CD.

- **Development**: See [Development Instructions](./docs/development.md).
- **Deployment**: See [Deployment Instructions](./docs/deployment.md).

---

#### CI/CD Pipeline Setup
For CI/CD setup instructions, see [CI/CD Instructions](./docs/cicd.md)

---

## License
The Full Stack FastAPI Template is licensed under the terms of the MIT license.

## Acknowledgements
This repository incorporates code and ideas from the following open-source projects:

- [FastAPI Production Template](https://github.com/zhanymkanov/fastapi_production_template)
- [Full Stack FastAPI Template](https://github.com/fastapi/full-stack-fastapi-template)

A big thanks to the contributors of these repositories for providing foundational code that made this project possible.