This is a simple CRUD (Create, Read, Update, Delete) API built with Flask and MongoDB. It provides endpoints to interact with a MongoDB database to manage user records.
Before you can run this application, ensure you have the following prerequisites installed:
- Python 3.8 or higher
- Docker
- Docker Compose
-
Clone the repository to your local machine:
git clone https://github.com/ynb10/Flask-MongoDB-REST-API-CRUD
-
Change to the project directory:
cd internship
-
Create a virtual environment named
myenv
:python -m venv myenv
-
Activate the virtual environment:
-
On Windows:
myenv\Scripts\activate
-
On macOS and Linux:
source myenv/bin/activate
-
-
Install project dependencies within the virtual environment:
pip install -r requirements.txt
-
Build the Docker containers:
docker-compose build
-
Start the Docker containers:
docker-compose up
The Flask application will be accessible at http://localhost:5000
, and MongoDB will be running on http://localhost:27017
.
-
Endpoint:
/add
-
HTTP Method: POST
-
Request Body:
{ "name": "John Doe", "email": "johndoe@example.com", "pwd": "password" }
-
Description: Adds a new user to the database.
- Endpoint:
/users
- HTTP Method: GET
- Description: Retrieves a list of all users from the database.
- Endpoint:
/users/<id>
- HTTP Method: GET
- Description: Retrieves a user by their unique ID.
-
Endpoint:
/update/<id>
-
HTTP Method: PUT
-
Request Body:
{ "name": "Updated Name", "email": "updated@example.com", "pwd": "newpassword" }
-
Description: Updates an existing user's information.
- Endpoint:
/delete/<id>
- HTTP Method: DELETE
- Description: Deletes a user from the database by their unique ID.
MONGO_URI
: MongoDB connection URI. By default, it's set to"mongodb://localhost:27017/intern"
in thedocker-compose.yml
file.
You can use a tool like Postman to interact with the API endpoints mentioned above.
- Flask
- PyMongo
- Docker
- MongoDB