This project is a simple Flask API that interacts with a MongoDB database. It includes endpoints to perform CRUD (Create, Read, Update, Delete) operations on documents in a MongoDB collection.
-
Clone the repository:
git clone <repository-url> cd <repository-directory>
-
Install Docker and Docker Compose::
- Make sure Docker is installed on your machine. You can download it from Docker's official website.
- Install Docker Compose if not already installed. Instructions can be found here.
Create a .env file in the root of the project directory with the following content:
cp .env.example .env
AZURE_CONNECTION_URI=your_azure_connection_string
DB_NAME=your_database
COLLECTION_ID=your_collection
USERNAME_ENV=username
PASSWORD_ENV=password
...
AZURE_CONNECTION_URI
: The URI for connecting to the MongoDB container.
DB_NAME
: The name of your MongoDB database.
COLLECTION_ID
: The name of your MongoDB collection.
USERNAME_ENV
&PASSWORD_ENV
: Information to login to get access token
-
Ensure Docker is installed and running:
- Ensure that both Docker and Docker Compose are installed on your machine and the Docker service is running.
-
Build and run the containers: In the project root directory, run the following command:
docker-compose build --no-cache docker-compose up
This command will:
- Build the Docker image for the Flask API.
- Start the Flask API container.
-
Access the API: Using Postman to access API endpoint:
{ “username”: “phuc”, “password”: “phucnv” }
Log in to get tokens to access other endpoints in the system. Replace login with other endpoints as needed.
-
GET
: http://127.0.0.1:5000/api/v1/documentsAdd access token to Bearer token in Authorization (access token from Login API)
[!NOTE]: Add the following environment variables to the
.env
fileUSERNAME_ENV=phuc PASSWORD_ENV='$2b$12$z/OxfOfYiPC3tpepm0LpAO3IBDWRjPhmXp/1OBPIkniVlEtj5dnVe'
-
- POST
/api/v1/login
: Get access_token to call other endpoints - GET
/api/v1/documents
: Retrieve all documents from the collection.
- GET
/api/v1/documents/<id>
: Retrieve a specific document by its ObjectId.
- POST
/api/v1/documents
: Create a new document in the collection. Provide JSON data in the request body.
- PUT
/api/v1/documents/<id>
: Update an existing document by its ObjectId. Provide JSON data in the request body.
- DELETE
/api/v1/documents/<id>
: Delete a document by its ObjectId.
If the application fails to connect to MongoDB or if required environment variables are missing, it will raise an appropriate error message. Make sure to set all necessary environment variables and check your MongoDB connection.