A simple backend application using Eve, Pydantic, and MongoDB in a Docker container. The project is designed to demonstrate a scalable and modular structure for building APIs with Eve while utilizing Pydantic for data validation and environment variables for configuration.
This project is a simple demonstration of building a scalable REST API backend using Eve, a REST API framework built on top of Flask. The app utilizes Pydantic for input validation, MongoDB for data storage (hosted inside a Docker container), and environment variables loaded via .env
for configuration.
The goal is to show how to structure an Eve API application in a modular way, including good practices for project organization, data validation, and use of environment variables for configurations.
- Eve: REST API framework built on Flask.
- Pydantic: Data validation and settings management.
- MongoDB: Database for storing user data.
- Docker: Used for running MongoDB in a containerized environment.
- Python: The main programming language used.
- Flask: Web framework used by Eve.
- Python-dotenv: For managing environment variables via
.env
file.
- Python 3.9 or higher
- Docker (for MongoDB container)
- pip (Python package installer)
Clone the repository to your local machine:
git clone https://github.com/gustavovalle23/eve-playground.git
cd eve-playground
Create a virtual environment and install dependencies:
python3 -m venv venv
source venv/bin/activate # On Windows use `venv\Scripts\activate`
pip install -r requirements.txt
Ensure Docker is installed and running on your machine. Then, use Docker to run MongoDB:
docker-compose up -d
This will pull the official MongoDB image and run it in a container.
Create a .env
file in the project root and define the following variables:
MONGO_URI=mongodb://localhost:27017
MONGO_DBNAME=mydatabase
DEBUG=True
This ensures that your app is connected to the local MongoDB instance and the app runs in debug mode.
Now you can run the application using:
python -m app.main
This will start the Eve API on http://127.0.0.1:5000
.
Once the application is running, you can interact with the API via HTTP requests.
curl -X POST "http://127.0.0.1:5000/users" \
-H "Content-Type: application/json" \
-d '{"username": "john_doe", "email": "[email protected]", "age": 30}'
curl -X GET "http://127.0.0.1:5000/users"
POST /users
- Create a new user.GET /users
- Retrieve all users.GET /users/<user_id>
- Retrieve a single user by ID.PATCH /users/<user_id>
- Update a user.DELETE /users/<user_id>
- Delete a user.
The folder structure of the project is designed to be scalable and modular:
backend/
│── app/
│ ├── models/ # Database models
│ │ ├── user.py
│ │ ├── __init__.py
│ ├── schemas/ # Pydantic schemas for validation
│ │ ├── user.py
│ │ ├── __init__.py
│ ├── settings.py # Eve settings and MongoDB configuration
│ ├── main.py # Eve app instance
│ ├── __init__.py
│── .env # Environment variables
│── docker-compose.yml # MongoDB in Docker
│── requirements.txt # Project dependencies
│── README.md # Documentation
Contributions are welcome! Feel free to open an issue or submit a pull request. If you want to improve the project or have ideas for new features, let me know.
Steps to contribute:
- Fork the repository
- Create a new branch (
git checkout -b feature-name
) - Commit your changes (
git commit -am 'Add new feature'
) - Push to your branch (
git push origin feature-name
) - Create a new Pull Request