This FastAPI Boilerplate is designed to provide a robust starting point for building modern web applications. It comes pre-configured with Docker, Traefik for easy deployment, and GitHub Actions for continuous integration. The boilerplate supports both SQL and NoSQL databases through flexible ORM configurations.
- FastAPI: High-performance, easy to learn, fast to code, ready for production
- Docker and Docker Compose: Containerization of the application and its dependencies for easy deployment and scaling
- Traefik Integration: Simplifies networking and SSL termination
- GitHub Actions: Automated testing and deployment
- ORM Flexibility: Configurable for use with various ORMs to support both SQL and NoSQL databases
- Docker
- Docker Compose
To get started with this boilerplate, clone the repository and follow the steps below.
git clone https://github.com/mahdikiani/FastAPILaunchpad.git
cd FastAPILaunchpadCopy the sample environment file and customize it if needed:
cp sample.env .envUpdate DOMAIN in .env to localhost for local development. Other variables like PROJECT_NAME can be set as per your project.
docker-compose up --buildThis command will build the Docker image and run the container as specified in your docker-compose.yml file. Your FastAPI application will be available directly at http://localhost:8000.
Navigate to http://localhost:8000/docs in your web browser to view the automatic interactive API documentation provided by Swagger UI.
Database connection settings are managed in app/server/config.py. This boilerplate is designed to be flexible and can be adapted to various SQL and NoSQL databases by modifying the ORM setup and connection strings in this file. Refer to the comments and structure within app/server/config.py for more details on customization.
Traefik is optional for local development but recommended for production deployments as a reverse proxy and for managing SSL certificates.
For production, you can:
- Create a separate
docker-compose.traefik.ymlortraefik.ymlfile that includes Traefik service definitions and the necessary labels for theappservice. - Alternatively, re-integrate the Traefik labels into the main
docker-compose.yml. The originaldocker-compose.yml(before the simplification for local development) can serve as a reference for the required labels and network configurations. You can find previous versions in the Git history.
The app/Dockerfile is configured to run the application using CMD ["python", "app.py"] by default. For debugging:
- Modify the
app/Dockerfileto use thedebugpycommand. Comment out the currentCMDand uncomment the one for debugging:# CMD [ "python","app.py" ] CMD ["python", "-m" ,"debugpy", "--listen", "0.0.0.0:3000", "-m", "app"]
- Rebuild the Docker image:
Or, if you prefer to rebuild and restart:
docker-compose build app
docker-compose up --build
- Configure your IDE to attach to the debugger on port
3000.
While Docker is the recommended method for consistency and ease of deployment, you can also run the application locally without it:
- Create and activate a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
- Install dependencies:
pip install -r app/requirements.txt
- Set up environment variables:
The application may rely on environment variables defined in
.env. Ensure these are available in your shell. You can manually create a.envfile (if you haven't already from the Docker setup) and load it using a library likepython-dotenvif you modifyapp/app.pyor manage them through your shell. For example, your.envcould look like:PROJECT_NAME=my_project DOMAIN=localhost TESTING=false # Add other necessary variables - Run the application:
The application should be available at
python app/app.py
http://localhost:8000.
Note: Using Docker is highly recommended to ensure a consistent environment across development, testing, and production.
GitHub Actions workflows are set up for continuous integration, ensuring that your tests run automatically every time you push changes to your repository.
This project is licensed under the MIT License.