FAST-API Backend with MySQL Database using docker compose
This repository contains a simple FAST-API backend application connected to a MySQL database, both orchestrated using Docker Compose. The backend provides endpoints to perform basic CRUD operations on a 'todo' table in the database.
Prerequisites
- Docker installed on your machine
Installation
-
Clone this repository to your local machine:
git clone https://github.com/kafe-it/distributed-systems.git
Usage (Only start backend)
-
Navigate to the project directory:
cd fastapi-rest
-
Start the application by running Docker Compose:
docker compose up
The backend server will start on https://localhost:8000.
Usage (Start backend and frontend)
-
Navigate to the project directory:
cd sample/otel-in-action
-
Start the application by running Docker Compose:
docker compose --file .\docker-compose-fastapi.yaml up -d
The backend server will start on https://localhost:8000.
NOTE: Startup of backend depends on mysqldb. therefore, startup takes some time.
Endpoints
- GET /todos
- Description: Fetches all todo items from the database.
- Method: GET
- Response:
- Status Code: 200 (OK)
- Body: An array of todo texts
- POST /todos/:todo
- Description: Adds a new todo item to the database.
- Method: POST
- Parameters:
- todo: todo item to be added
- Response:
- Status Code: 201 (Created)
- Body: Confirmation message
- DELETE /todos/:todo
- Description: Deletes a todo item from the database.
- Method: DELETE
- Parameters:
- todo: todo item to be deleted
- Response:
- Status Code: 200 (OK)
- Body: Confirmation message
Configuration
The Docker Compose file (docker-compose-fastapi.yml
) contains the configuration for both the fast api backend and the MySQL database containers. Ensure that the environment variables in the database service match your MySQL configuration needs.
Error Handling
- If an error occurs during any operation, the server will respond with a 500 (Internal Server Error) status code along with an error message.
Kubernetes
Ensure kubectl and minikube/kind are installed.
Run:
minikube start
kubectl apply -f sample/otel-in-action/deployment.yaml
Forward Port for Codespaces:
kubectl port-forward service/todoui-flask 5000:5000
kubectl port-forward service/todoui-thymeleaf 8090:8090
Now you can observe the deployments:
kubectl get deployments
See Service Ports and IPs:
kubectl get services