This repository provides a base implementation for a Flask + MongoDB backend. It includes structured response handling, middleware, error handling, and a modular approach for adding routes.
- Flask-based API with structured JSON responses
- Middleware for request preprocessing and response formatting
- Global exception handling
- MongoDB integration with minimal setup
- Modular routes with Blueprints
- Asynchronous support for I/O operations
git clone https://github.com/dElCIoGio/flask-mongodb-template.git
cd flask-mongodb-templatepython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtThis will install all necessary dependencies, including MongoDB-related libraries.
uvicorn app:app --host 0.0.0.0 --port 5000 --reloadThe server will start at http://127.0.0.1:5000/
curl -X GET http://127.0.0.1:5000/{
"status": 200,
"success": true,
"message": "User created",
"data": {
"id": 1,
"name": "John Doe"
}
}/
│-- app.py # Main Flask application
│-- middleware.py # Middleware for request/response handling
│-- routes/
│ ├── user_routes.py # User-related routes
│ ├── product_routes.py # Product-related routes
│-- requirements.txt # Required dependencies
│-- README.md # Documentation
The only required configuration for MongoDB is setting the following environment variables in a .env file:
MONGO_URI=mongodb://localhost:27017/
MONGO_DATABASE_NAME=mydatabase
No additional setup is required beyond setting the environment variables. The system will automatically connect to MongoDB using the provided values.
Use Uvicorn for production deployment:
uvicorn app:app --host 0.0.0.0 --port 5000