An appliction for CRUD operations on MongoDB using flask+python backend and build REST API, The REST API endpoints should be accessible via HTTP requests and tested using Postman.
The application should connect to a MongoDB database. The application should provide the following REST API endpoints:
-1.GET /users - Returns a list of all users.
-2.GET /users/<id> - Returns the user with the specified ID.
-3.POST /users - Creates a new user with the specified data.
-4.PUT /users/<id> - Updates the user with the specified ID with the new data.
-5.DELETE /users/<id> - Deletes the user with the specified ID.
Setup and installation
-
Create a virtual environment
python3 -m venv venv
source venv/bin/activate
- create a new file in the directoty named app.py
-
To install Flask and pyMongo using pip install
pip install Flask pymongo
- import the flask and pymongo into the app.py
-
Install & Setup MongoDb Databsae using homebrew
- install MongoDb community in the local
brew install mongodb-community@6.0
- To Start mongoDb server
brew services start mongodb-community@6.0
- After starting the server open a new terminal
mongosh
use Collection
db.createCollection("users")
- To insert the data into the db
"name": "John Doe", "email": "john@example.com", "password": "secretpassword" })```
- install MongoDb community in the local
-
Run the Application.
- flask run
-
Testing the Application using CRUD
- Test in the POSTMAN
- install postman desktop agent
- The REST API endpoints:
- GET /users - Returns a list of all users.
- GET /users/ - Returns the user with the specified ID.
- POST /users - Creates a new user with the specified data.
- PUT /users/ - Updates the user with the specified ID with the new data.
- DELETE /users/ - Deletes the user with the specified ID.
- The REST API endpoints:
-
To Dockerize :
- Create a file named Dockerfile
- requirements.txt
Flask-PyMongo==2.3.0```
- create a yml file
- Build and run docker-compose build docker-compose up
- push the docker into hub
- list the docker images -
docker images
- login into docker -
docker login
enter username and password - create a image tag docker tag IMAGE NAME HUBNAME/IMAGE NAME -
docker tag corider_assignment-app lokeshbx/corider_assignment-app
- docker push IMAGE - docker push lokeshbx/corider_assignment-app
- list the docker images -