This is a simple Flask application that provides CRUD operations (Create, Read, Update, Delete) on a User resource using a MongoDB database. The application uses a REST API that can be accessed via HTTP requests and tested using Postman.
Read Assignment : Click Here
- Create a new user
- Read all users or a specific user by ID
- Update user information
- Delete a user
To set up and run this application, you need the following installed:
- Docker
First, clone the repository to your local machine:
git clone areeburrub/flask-crud-mongodb
cd flask-crud-mongodb
To build and run the Flask application along with MongoDB inside Docker containers, run the following commands:
docker-compose build
docker-compose up
This will start both the Flask app and the MongoDB database inside their respective containers.
- The Flask API will be accessible at
http://localhost:5000
. - MongoDB will be accessible inside the container at
mongodb://mongo:27017
.
To stop the running containers, press Ctrl+C
or use:
docker-compose down
This will stop and remove the containers.
The following API endpoints are available to perform CRUD operations:
POST /users
- Request Body (JSON):
{
"id": "1",
"name": "Areeb ur Rub",
"email": "areeburrub@example.com",
"password": "securePassword123"
}
- Response (Success - 201 Created):
{
"message": "User created successfully",
"user": {
"id": "1",
"name": "Areeb ur Rub",
"email": "areeburrub@example.com"
}
}
GET /users
- Response (Success - 200 OK):
[
{
"id": "1",
"name": "Areeb ur Rub",
"email": "areeburrub@example.com"
}
]
GET /users/<id>
- Response (Success - 200 OK):
{
"id": "1",
"name": "Areeb ur Rub",
"email": "areeburrub@example.com"
}
PUT /users/<id>
- Request Body (JSON):
{
"name": "John Updated",
"email": "john.updated@example.com",
"password": "newSecurePassword"
}
- Response (Success - 200 OK):
{
"message": "User updated successfully",
"user": {
"id": "1",
"name": "John Updated",
"email": "john.updated@example.com"
}
}
DELETE /users/<id>
- Response (Success - 200 OK):
{
"message": "User deleted successfully"
}
- Open Postman and create a new request for each of the endpoints listed above.
- Set the appropriate request method (GET, POST, PUT, DELETE) and enter the URL (
http://localhost:5000
). - For POST and PUT requests, set the request body to raw JSON.
- Hit Send and verify the responses.
You can configure the MongoDB connection and other environment variables by editing the .env
file or updating the docker-compose.yml
.
MONGO_URI=mongodb://mongo:27017
FLASK_ENV=development
SECRET_KEY=your-secret-key