Skip to content

Latest commit

 

History

History
96 lines (74 loc) · 3.34 KB

README.md

File metadata and controls

96 lines (74 loc) · 3.34 KB

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.

Table of Contents

Installation

  1. Clone the repository:

    git clone <repository-url>
    cd <repository-directory>
  2. 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.

Environment Variables

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

Running the Application

  1. Ensure Docker is installed and running:

    • Ensure that both Docker and Docker Compose are installed on your machine and the Docker service is running.
  2. 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.
  3. Access the API: Using Postman to access API endpoint:

    Login API infomation:
    {
       “username”: “phuc”,
       “password”: “phucnv”
    }

    Log in to get tokens to access other endpoints in the system. Replace login with other endpoints as needed.

    Ex: API to get all documents in Azure Cosmos DB:

    [!NOTE]: Add the following environment variables to the .env file

    USERNAME_ENV=phuc
    PASSWORD_ENV='$2b$12$z/OxfOfYiPC3tpepm0LpAO3IBDWRjPhmXp/1OBPIkniVlEtj5dnVe'
    

API Endpoints

  • 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.

Error Handling

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.