Production ready template for FastAPI backend using Asyncio, SQLAlchemy, PostgreSQL and Docker
Report Bug
·
Request Feature
- Python 3.9
- FastAPI as a main web framework
- SQLAlchemy Core for working with databases
- Alembic for database migrations
- PostgreSQL as main database
- Docker-Compose for development and production
- Poetry for dependency management
- Pytest for tests
- Black, Flake8, Mypy for code formatting and linting
- Sentry for error tracking
- Uvicorn as ASGI server
- Traefik as reverse proxy and load balancer
- Redis as a message broker and for caching
- Ready to use file management, OTP authentication using JWT, scheduled tasks and background tasks
- Optional Celery for background tasks
- Optional Frontend container configuration using Nginx
First, install cookiecutter if you don't already have it:
pip3 install cookiecutter
Second, install docker and docker-compose if you don't already have it:
Official Docker and Docker compose installation guide
Finally, generate your project:
cookiecutter https://github.com/iyasha/fastapi-template.git
- Install Poetry
- Go to the project directory:
cd your_project_name
- Install dependencies:
poetry install
- Run database and celery (optional) containers:
docker-compose up -d db celeryworker
- Change database and redis host in
.env
file:
POSTGRES_HOST=localhost
REDIS_HOST=localhost
- Create database tables:
alembic revision --autogenerate -m "init"
alembic upgrade head
Next steps are optional and depends on your IDE. You can skip them if you don't need them.
- Install plugin .env files support
- Open project in PyCharm and create interpreter from Poetry virtual environment
- Add Python interpreter to Run configurations:
Serve
andCelery
(Optional) - Create file watcher for formatting and linting Guide Here:
- Run
Serve
andCelery
(Optional) configuration and go to http://localhost:8000/docs
- Create virtual environment using Poetry
- Run
uvicorn main:app --reload --port 8000
for development - Run
celery worker -A src.tasks -l INFO -Q main-queue -c 1
for celery (optional) - Go to http://localhost:8000/docs
pre-commit install
Variable | Description | Default value |
---|---|---|
project_name |
Project name | FastAPI Template |
enviroment |
Project environment | production |
version |
Project version | 0.1.0 |
author_name |
Author name | None |
author_email |
Author email | None |
full_domain |
Full domain name | http://localhost |
secret_key |
Secret key for JWT | Generate random string |
docs_suffix |
Swagger docs suffix | docs |
sentry_dsn |
Sentry DSN | None |
traefik_public_network_is_external |
Guide | False |
Variable | Description | Default value |
---|---|---|
db_user |
Database username | postgres |
db_password |
Database password | Generate random string |
db_name |
Database name | app |
db_host |
Database host | db |
db_port |
Database port | 5432 |
Variable | Description | Default value |
---|---|---|
redis_host |
Redis host | redis |
redis_port |
Redis port | 6379 |
redis_db |
Redis database | 0 |
Variable | Description | Default value |
---|---|---|
email_sender |
Email sender | None |
email_host |
Email smtp host | None |
email_port |
Email smtp port | None |
email_username |
Email smtp username | None |
email_password |
Email smtp password | None |
Variable | Description | Default value |
---|---|---|
celery_worker_concurrency |
Celery worker concurrency | 1 |
add_celery |
If 'y' then celery container and celery utils will be add to the project files | y |
Variable | Description | Default value |
---|---|---|
create_frontend_container_configuration |
If 'y' then nginx container will be add to docker-compose files | y |
create_watchtower_configuration |
If 'y' then watchtower container will be add to docker-compose files | y |
For scheduling tasks you need to set up cron job in your server. For example:
* * * * * docker exec backend python manage.py schedule
.
├─ alembic
├─ database
├─ docker
├─ media
├─ redis
├─ src
│ ├─ api
│ │ ├─ v1
│ │ │ └─ module_name
│ │ │ ├─ models.py
│ │ │ ├─ enums.py
│ │ │ ├─ repositories.py
│ │ │ ├─ schemas.py
│ │ │ └─ services.py
│ │ └─ router.py
│ ├─ commands
│ │ ├─ base.py
│ │ ├─ schedule.py
│ │ └─ your_command.py
│ ├─ sdk
│ │ ├─ exceptions
│ │ │ ├─ exception_handler_mapping.py
│ │ │ ├─ exceptions.py
│ │ │ ├─ handlers.py
│ │ │ └─ helpers.py
│ │ ├─ models.py
│ │ ├─ ordering.py
│ │ ├─ pagination.py
│ │ ├─ repositories.py
│ │ ├─ responses.py
│ │ ├─ schemas.py
│ │ └─ utils.py
│ ├─ background.py
│ ├─ cache.py
│ ├─ config.py
│ ├─ database.py
│ ├─ dependencies.py
│ ├─ main.py
│ ├─ manage.py
│ ├─ sentry.py
│ └─ tasks.py
└─ tests
├─ api
│ └─ v1
│ └─ module_name
│ ├─ test_views.py
│ ├─ test_services.py
│ ├─ test_repositories.py
│ └─ test_schemas.py
├─ common.py
├─ conftest.py
└─ utils.py
- Add project structure description to README.md
- Add tests
- Add README.md to project template
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
If you have any questions, feel free to contact me via email: ivan@simantiev.com