This repository provides a boilerplate for creating a Flask API. It includes a structured project layout, configuration management, and various utility functions to get you started quickly.
Follow these instructions to set up and run the project on your local machine.
- Python 3.7 or higher
- Virtual environment tool (optional but recommended)
-
Clone the repository:
git clone https://github.com/TechWithAbee/Flask-API-Boilerplate.git cd Flask-API-Boilerplate
-
Create a virtual environment:
python -m venv venv source venv/bin/activate # On Windows use `venv\Scripts\activate`
-
Install the dependencies:
pip install -r requirements.txt
-
Set up the environment variables:
Copy the
.env.example
file to.env
:cp .env.example .env
Edit the
.env
file to include your specific environment variables.
To run the Flask application, use the provided shell script:
./run.sh
Alternatively, you can run the application using gunicorn:
gunicorn --logger-class=config.logger.GunicornLogger service.wsgi:app --bind 0.0.0.0:8000 --workers=1
The project structure is organized as follows:
├── config
│ ├── logger.py
│ ├── monitoring.py
├── handler
│ ├── user
│ ├── user_routes.py
│ ├── user_service.py
├── helper
│ ├── api_types.py
├── service
│ ├── constants.py
│ ├── wsgi.py
├── .env.example
├── .gitignore
├── README.md
├── main.py
├── requirements.txt
├── run.sh
config/
: Contains configuration files.handler/
: Contains route handlers, organized by feature.helper/
: Contains utility functions and helpers.service/
: Contains business logic and service classes..env.example
: Example environment variables file..gitignore
: List of files and directories to ignore in Git.main.py
: Entry point for the Flask application.requirements.txt
: List of Python dependencies.run.sh
: Shell script to run the application.
The configuration is managed through environment variables. The .env.example
file provides a template. Copy this file to .env
and customize it according to your needs.
Example .env
file:
FLASK_ENV=development
DATABASE_URL=sqlite:///example.db
SECRET_KEY=your_secret_key
- Routes: Add your API routes in the appropriate handler modules (e.g.,
handler/user/user_routes.py
). - Services: Implement your business logic in service modules (e.g.,
handler/user/user_service.py
).
This project is licensed under the MIT License.