The Flask API for Managing Orders Data is a project that provides a simple yet powerful API for managing orders stored in a PostgreSQL database on a Kubernetes Cluster. This documentation will guide you through the setup process and explain the structure and components of the project.
The Flask API for Managing Orders Data follows a microservice architecture. It consists of the following components:
-
Flask API: This component provides the API endpoints for managing orders. It is built using the Flask framework, a lightweight web framework for Python.
-
PostgreSQL Database: The PostgreSQL database stores the order data. It is used to persist and retrieve order information.
-
Mocker: The Mocker component generates mock order data and populates the database. It is optional and can be used to quickly populate the database with sample data for testing and demonstration purposes.
-
Kubernetes: Kubernetes is used for container orchestration. It manages the deployment and scaling of the Flask API application.
Before setting up and running the Flask API application, ensure that you have the following prerequisites installed:
- Docker: Containerization platform used to build and run the application.
- Kubernetes (kubectl): Command-line tool for interacting with Kubernetes clusters.
- Helm: Package manager for Kubernetes clusters.
The Flask API application has the following dependencies:
- Flask: A micro web framework for building web applications in Python.
- psycopg2: A PostgreSQL adapter for Python.
To install the dependencies locally, run the following command:
pip install -r app/requirements.txt -r mock-data/requirements.txt
Follow the steps below to set up and run the Flask API application:
First, clone the project repository to your local machine:
$ git clone https://github.com/hrittikhere/flask-app-pro.git
$ cd flask-app
The Flask API application and the Mocker component require Docker images to be built. Build the Docker images by running the following commands in respective directory:
$ docker build -t flask-api .
$ docker build -t mock-data .
To deploy the Flask API application, you need a Kubernetes cluster and a PostgreSQL database instance. Follow the steps below to set them up:
Helm is a package manager for Kubernetes. Install Helm by executing the following command:
$ curl https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | bash
Use Helm to install PostgreSQL:
$ helm repo add bitnami https://charts.bitnami.com/bitnami
$ helm install postgres bitnami/postgresql
Apply the Kubernetes manifests to deploy the Flask application and run the job to store order data in the database. Execute the following commands:
$ kubectl apply -f app/job.yml
$ kubectl apply -f app/app.yml
Ensure that the pods and services are running correctly. Use the following commands to check their status:
$ kubectl get pods
$ kubectl get svc
To access the Flask API, follow these steps:
-
Obtain the external IP address of the Flask application service:
$ kubectl get svc flask-service
-
You can also use the command to fetch the IP easily externally:
$ kubectl get svc flask-service -o jsonpath='{.status.loadBalancer.ingress[0].ip}' && echo
3. Use tools like cURL or Postman to send HTTP requests to the API endpoints using the obtained IP address. For example:
```shell
$ curl http://<flask-service-external-ip>/orders
The Flask API provides the following endpoints for managing orders:
POST /order
: Create a new order by providing the order details in the request body.GET /order/<order_id>
: Retrieve a specific order by its order ID.GET /
: Retrieve a list of all orders.DELETE /order/<order_id>
: Delete a specific order by its order ID.GET /order/search
: Search for orders based on specified product names or words or patterns.
Test the API by making requests to different endpoints. Here are some examples:
- Create a new order: Send a
POST
request to/order
with the following JSON payload:
{
"customer": {
"name": "John Doe",
"email": "[email protected]",
"address": "123 Main St, City, Country"
},
"order_id": 300,
"product_name": "Widget",
"quantity": 5,
"order_date": "2023-06-22",
"priority": "high"
}
-
Retrieve an order: Send a
GET
request to/order/<order_id>
, replacing<order_id>
with the actual order ID. -
List all orders: Send a
GET
request to/
to get all the orders in JSON. -
Delete an order: Send a
DELETE
request to/order/<order_id>
, replacing<order_id>
with the actual order ID. -
Search for orders: Send a
GET
request to/order/search?q=<search_query>
, replacing<search_query>
with the desired search term for your product.
For more information, if your Flask application is running on http://<flask-service-external-ip>/
, you can access the Swagger documentation at http://<flask-service-external-ip>/apidocs
and test the endpoints interactively.
This project is licensed under the MIT License. Your contributions are greatly appreciated and will be licensed under the same terms.