- Flask API
- MongoDB
- Docker
- Gunicorn
- Nginx
- JWT Authentication
This project is a Flask-based web application configured with MongoDB for data storage, Docker for containerization, Gunicorn as the WSGI server, Nginx as the reverse proxy and load balancer, and JWT (JSON Web Tokens) for authentication.
app/
: Directory containing the Flask application code.__init__.py
: Initializes the Flask app and sets up configurations.models.py
: Contains MongoDB data models.routes.py
: Defines API endpoints.auth.py
: Handles authentication-related functionality.config.py
: Contains configuration settings for the app.
docker
: Directory containing docker filesDockerfile
: Instructions for building the Docker image.gunicorn_config.py
: Gunicorn configuration file.nginx.conf
: Nginx configuration file for reverse proxy.
docker-compose.yml
: Configuration for Docker Compose to set up services..env
: Environment variables file.requirements.txt
: Python dependencies for the application.README.md
: Project details.manage.py
: App init file
- MongoDB: NoSQL database for storing user data.
- Docker: Containerization for consistent deployment.
- Gunicorn: WSGI server for serving the Flask application.
- Nginx: Reverse proxy and load balancer.
- JWT Authentication: Secure user authentication using JSON Web Tokens.
- Python >=3.10.7
- Docker
- Docker Compose
git clone https://github.com/dpralay07/python-flask-production-recipes.git
cd your-repository
- Environment Variables : Update the .env file with your configuration values. This will help to run the application in development server.
- Docker compose : The docker-compose.yml file is configured to build and run the Flask application along with MongoDB. It also sets up the Nginx reverse proxy. Update the environment variables with your configuration values to run the application in production server.
- Nginx Configuration : The nginx.conf file is used to configure Nginx to proxy requests to the Flask application.
Build the docker images
docker-compose build
Start the Containers
docker-compose up -d
This will start the MongoDB, Flask application, and Nginx services.
Access the Application
The Flask API will be available at http://<your_ip_address>:80
To stop and remove the containers, run:
docker-compose down
POST /register: Register a new user. Requires username and password in the request body.
POST /login: Authenticate a user and receive a JWT token. Requires username and password in the request body.
GET /protected: Access protected content. Requires a valid JWT token in the Authorization header.
We can test the API endpoints using tools like Postman or cURL. Example cURL Commands
Register a User:
curl -X POST http://<your_ip_address>:80/register -H "Content-Type: application/json" -d '{"username": "testuser", "password": "password123"}'
Login and Get Access Token:
curl -X POST http://<your_ip_address>:80/login -H "Content-Type: application/json" -d '{"username": "testuser", "password": "password123"}'
Access Protected Endpoint: Replace your_jwt_token with the token received from login.
curl -X GET http://<your_ip_address>:80/protected -H "Authorization: Bearer your_jwt_token"
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please open an issue or submit a pull request to improve this project.
For any questions or feedback, please contact [email protected].