This is a simple Todo REST API built using Go with Fiber, MongoDB, and godotenv for environment variable management.
- Go installed (version 1.16 or above)
- MongoDB running
- Docker (optional for running the app in a container)
git clone <repository-url>
cd <project-directory>go mod tidyCreate a .env file in the root directory of your project with the following variables:
DB_URL=mongodb://localhost:27017
PORT=5000Make sure DB_URL points to your running MongoDB instance.
Start the application using Air for live reloading, or just use Go directly.
Using Air:
airUsing Go:
go run main.goBy default, the server will run on port 5000. You can change this in the .env file.
The API documentation is available in the Swagger format.
- You can explore the API endpoints and test the API interactively using Swagger.
- To view the Swagger documentation locally, install Swagger UI or use ReDoc.
- The API documentation is defined in the
swagger.yamlfile in the project. You can use the following link to view it in Swagger UI:
- URL:
/api/todos - Method:
GET - Response: JSON array of all todos in the database
curl -X GET http://localhost:5000/api/todos[
{
"id": "63419d9a0f9e7f9a9f7e1234",
"completed": false,
"body": "Write documentation"
},
{
"id": "63419d9a0f9e7f9a9f7e1235",
"completed": true,
"body": "Setup project"
}
]-
URL:
/api/todos -
Method:
POST -
Request Body:
{ "body": "Todo content here", "completed": false } -
Response: Created Todo object with its MongoDB ID
curl -X POST http://localhost:5000/api/todos -H "Content-Type: application/json" -d '{"body": "New Todo", "completed": false}'{
"id": "63419d9a0f9e7f9a9f7e1236",
"completed": false,
"body": "New Todo"
}- URL:
/api/todos/:id - Method:
PATCH - Request Params:
id- ID of the todo to update (as URL parameter)
- Request Body:
- This endpoint is designed to mark a todo as completed, so no request body is needed.
- Response: Success message upon successful update
curl -X PATCH http://localhost:5000/api/todos/63419d9a0f9e7f9a9f7e1234{
"update success": true
}- URL:
/api/todos/:id - Method:
DELETE - Request Params:
id- ID of the todo to delete (as URL parameter)
- Response: Success message upon successful deletion
curl -X DELETE http://localhost:5000/api/todos/63419d9a0f9e7f9a9f7e1234{
"delete success": true
}The API handles errors by returning a JSON object with an appropriate HTTP status code and a descriptive error message.
Example error responses:
-
Invalid ID:
{ "error": "Invalid ID" } -
Empty Todo Body:
{ "error": "Todo body cannot be empty" }