This project is a backend API for managing tasks with CRUD operations. It is built using Flask and MySQL, and it is containerized using Docker and Docker Compose. The API includes user authentication using JWT (JSON Web Tokens) to secure task management operations.
For a visual walkthrough of the setup and usage of the Task Management API, you can watch the following video:
Before you begin, ensure you have the following installed on your machine:
-
Docker: Used to run the application in containers.
- Verify Installation: Run the following command in your terminal:
docker --version
- Verify Installation: Run the following command in your terminal:
-
Docker Compose: Used to define and run multi-container Docker applications.
- Verify Installation: Run the following command in your terminal:
docker-compose --version
- Verify Installation: Run the following command in your terminal:
-
Git (optional, for cloning the repository): Version control system to manage your code.
- Verify Installation: Run the following command in your terminal:
git --version
- Verify Installation: Run the following command in your terminal:
-
Python: Required for running the backend application.
- Verify Installation: Run the following command in your terminal:
or for Python 3 specifically:
python --version
python3 --version
- Verify Installation: Run the following command in your terminal:
-
pip: Python package installer, used to install dependencies.
- Verify Installation: Run the following command in your terminal:
or for pip3 specifically:
pip --version
pip3 --version
- Verify Installation: Run the following command in your terminal:
Make sure all the above tools are installed and accessible from your command line before proceeding with the setup instructions.
-
Clone the Repository: Go to the location where you would like this code folder to be and run the code below:
git clone https://github.com/<yourusername>/Task-Management-API.git cd Task-Management-API
-
Create a
.env
File: Create a.env
file in the root of the project directory with the following content:DB_USER=task_user DB_PASSWORD=task_password DB_NAME=task_db DB_HOST=db
-
Build and Start the Containers: Run the following command to build the Docker images and start the containers:
docker-compose up --build
This command will:
- Build the API service and the MySQL database service.
- Initialize the database with the provided SQL script.
-
Access the API: The API will be running at
http://localhost:5001
.
To run the API, ensure that the Docker containers are up and running. You can check the logs to confirm that the API is running without errors:
To check the logs to confirm that the API is running without errors, in a new terminal, run:
docker-compose logs -f
To stop the containers, press CTRL+C
in the terminal where the containers are running, or, in a new terminal, run:
docker-compose down
To register a new user, send a POST request to the /auth/register
endpoint with the following JSON body:
{
"username": "your_username",
"password": "your_password"
}
To log in, send a POST request to the /auth/login
endpoint with the following JSON body (You can see clearly how to do later using cURL or Postman):
{
"username": "your_username",
"password": "your_password"
}
On successful login, you will receive a JWT token in the response. Use this token for subsequent requests to protected endpoints.
Include the JWT token in the Authorization
header for requests to protected endpoints (e.g., task management endpoints):
Authorization: Bearer <your_jwt_token>
POST /auth/register
: Register a new user.POST /auth/login
: Log in and receive a JWT token.GET /tasks
: Retrieve all tasks (requires authentication).POST /tasks
: Create a new task (requires authentication).GET /tasks/{id}
: Retrieve a task by ID (requires authentication).PUT /tasks/{id}
: Update a task by ID (requires authentication).DELETE /tasks/{id}
: Delete a task by ID (requires authentication).
You can test the API using tools like Postman, cURL, or your web browser. Below are examples of how to test the API endpoints. Make sure the backend is running first.
-
Register a New User:
curl -X POST http://localhost:5001/auth/register -H "Content-Type: application/json" -d '{"username": "testuser", "password": "testpass"}'
-
Login:
curl -X POST http://localhost:5001/auth/login -H "Content-Type: application/json" -d '{"username": "testuser", "password": "testpass"}'
Example token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJmcmVzaCI6ZmFsc2UsImlhdCI6MTczODIxODMxNiwianRpIjoiZDA4OWEwMDMtZjVmOC00ODhkLTkwZDAtMTBiOTBjNjc4N2YwIiwidHlwZSI6ImFjY2VzcyIsInN1YiI6IjA5ZjdlMjI0LWM3NzctNDBkNy1iNjkyLTkyNzM2ZmVkMjdmNSIsIm5iZiI6MTczODIxODMxNiwiZXhwIjoxNzY5NzU0MzE2fQ.oNUkFVPwhcVgSFVJwqx4ELRXBwPNV9-86diHKq4g8OY"
-
Get All Tasks (after obtaining the JWT token):
curl -X GET http://localhost:5001/tasks -H "Authorization: Bearer <your_jwt_token>"
-
Create a New Task (after obtaining the JWT token):
curl -X POST http://localhost:5001/tasks -H "Content-Type: application/json" -H "Authorization: Bearer <your_jwt_token>" -d '{"title": "Test Task", "description": "This is a test task", "dueDate": "2023-12-31T00:00:00", "status": "pending"}'
-
Update a Task (after obtaining the JWT token):
curl -X PUT http://localhost:5001/tasks/<task_id> -H "Content-Type: application/json" -H "Authorization: Bearer <your_jwt_token>" -d '{"title": "Updated Task", "status": "completed"}'
-
Delete a Task (after obtaining the JWT token):
curl -X DELETE http://localhost:5001/tasks/<task_id> -H "Authorization: Bearer <your_jwt_token>"
-
Register a New User: Set the request type to POST and enter the URL
http://localhost:5001/auth/register
. In the "Body" tab, select "raw" and set the type to "JSON". Enter the JSON data for the user. -
Login: Set the request type to POST and enter the URL
http://localhost:5001/auth/login
. In the "Body" tab, select "raw" and set the type to "JSON". Enter the JSON data for the user. -
Get All Tasks: Set the request type to GET and enter the URL
http://localhost:5001/tasks
. In the "Headers" tab, add a new header with the keyAuthorization
and the valueBearer <your_jwt_token>
. -
Create a New Task: Set the request type to POST and enter the URL
http://localhost:5001/tasks
. In the "Body" tab, select "raw" and set the type to "JSON". Enter the JSON data for the task, and add theAuthorization
header as described above. -
Update a Task: Set the request type to PUT and enter the URL
http://localhost:5001/tasks/<task_id>
. In the "Body" tab, select "raw" and set the type to "JSON". Enter the JSON data for the updated task, and add theAuthorization
header as described above. -
Delete a Task: Set the request type to DELETE and enter the URL
http://localhost:5001/tasks/<task_id>
. In the "Headers" tab, add a new header with the keyAuthorization
and the valueBearer <your_jwt_token>
.
This README provides a comprehensive guide to setting up and using the Task Management API. Follow the instructions carefully to ensure a smooth experience. If you encounter any issues, please refer to the documentation for troubleshooting tips or reach out for support.