This FastAPI-Template is a public GitHub template designed to help developers quickly set up and develop APIs. It provides a solid foundation with various features and integrations to streamline the development process.
- FastAPI Application: A basic FastAPI application with example endpoints.
- Database Integration: Configuration for integrating with a MariaDB database using SQLAlchemy and Alembic for migrations.
- Docker Support: Dockerfile and docker-compose configuration for containerization.
- Devcontainer Support: Configuration for Visual Studio Code Dev Containers.
- GitHub Actions: Automated testing using GitHub Actions.
- mypy: Type checking with mypy.
- ruff: Linting with ruff.
- Click on the "Use this template" button on the GitHub repository page.
- Clone your new repository to your local machine.
- Follow the development instructions below to start building your API.
This template is optimized for use with Visual Studio Code.
- Ensure you have Docker and the Remote - Containers extension installed in VS Code.
- Open the project folder in VS Code.
- When prompted, click "Reopen in Container" or run the "Remote-Containers: Reopen in Container" command from the command palette.
- VS Code will build and start the development container, providing you with a fully configured environment.
- Ensure you have Docker and Docker Compose installed on your system.
- Run
docker-compose up -d
to start the development environment. - Use your preferred editor to make changes to the code.
- The FastAPI application will be available at
http://localhost:8000
.
- Ensure you have Docker and Docker Compose installed on your production server.
- Copy the
docker-compose.yml
file to your server. - Run
docker-compose up -d
to start the application in production mode.
fastapi-template
|-- LICENSE
|-- README.md
|-- alembic.ini
|-- database # Contains database-related files and migration scripts.
| |-- README
| |-- __init__.py
| |-- env.py
| |-- models.py # Defines SQLAlchemy ORM models.
| |-- script.py.mako
| `-- versions
| |-- cbc697a95795_init_database.py
| `-- fa9c8b7e809a_fix_type.py
|-- docker
| `-- api
| `-- dockerfile
|-- docker-compose-prod.yml
|-- docker-compose.yml
|-- example.env # Example environment variable file. Copy to .env and modify as needed
|-- log_config.yaml # Logging configuration files.
|-- log_config_debug.yaml # Logging configuration files. Used for debugging.
|-- pyproject.toml
|-- requirements-test.txt
|-- requirements.txt -> ./src/requirements.txt
|-- src
| |-- __init__.py
| |-- app.py # Main FastAPI application file.
| |-- endpoints # API endpoints.
| | |-- __init__.py
| | `-- v1 # Version 1 of the API endpoints.
| | |-- __init__.py
| | `-- version.py
| |-- requirements.txt
| |-- scheme # Pydantic schemas for request/response models.
| | |-- __init__.py
| | `-- version.py
| `-- utils
| |-- __init__.py
| `-- database.py # Contains Database connection and session management Class.
|-- start.sh
|-- tests # Contains test files.
| |-- __init__.py
| |-- test_database.py
| `-- test_version.py
`-- update_depends.sh
This repository follows the GitHub Flow workflow:
- Create a new branch for each feature or bugfix.
- Make changes and commit to the branch.
- Open a pull request for review.
- After approval, merge the branch into main.
- Delete the feature branch after merging.
GitHub Actions are configured to run tests automatically on each pull request:
- Runs pytest for unit and integration tests
- Performs type checking with mypy
- Lints the code with ruff
To modify ruff linting rules, edit the pyproject.toml
file in the root directory. Adjust the [tool.ruff]
section to customize ruff's behavior.
To configure mypy type checking, add or modify the [tool.mypy]
section in your pyproject.toml
file.
You can adjust these settings based on how strict you want the type checking to be. For more options, refer to the mypy configuration file documentation.
Remember to run mypy
and ruff
regularly during development to catch type errors and style issues early.
If you use Visual Studio Code, you can install the Python extension to get real-time feedback on type errors and linting issues.
This project is licensed under the MIT License. See the LICENSE file for details.