|
| 1 | +# Todo-Go Application |
| 2 | + |
| 3 | +This is a RESTful Todo application written in Go, using PostgreSQL as the database and Gorilla Mux for routing. The application includes JWT authentication, request logging, and idempotency handling. It is written for testing keploy idempotency feature. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- Go 1.16 or later |
| 8 | +- Docker and Docker Compose |
| 9 | +- PostgreSQL |
| 10 | + |
| 11 | +## Getting Started |
| 12 | + |
| 13 | +### 1. Start the PostgreSQL database |
| 14 | + |
| 15 | +Start the PostgreSQL database using Docker Compose: |
| 16 | + |
| 17 | +```sh |
| 18 | +docker-compose up -d |
| 19 | +``` |
| 20 | + |
| 21 | +To stop and remove the volume and its data, use: |
| 22 | + |
| 23 | +```sh |
| 24 | +docker-compose down -v |
| 25 | +``` |
| 26 | + |
| 27 | +### 2. Run the application |
| 28 | + |
| 29 | +Run the Go application: |
| 30 | + |
| 31 | +```sh |
| 32 | +go build . |
| 33 | +./todo-go |
| 34 | +``` |
| 35 | + |
| 36 | +The server will start running on `http://localhost:3040`. |
| 37 | + |
| 38 | +## Authentication |
| 39 | + |
| 40 | +This application uses JWT for authentication. Before accessing protected endpoints, you need to obtain a token: |
| 41 | + |
| 42 | +```sh |
| 43 | +curl -X POST -H "Content-Type: application/json" -d '{"username": "admin", "password": "password"}' http://localhost:3040/login |
| 44 | +``` |
| 45 | + |
| 46 | +The response will contain a token that should be used in subsequent requests: |
| 47 | + |
| 48 | +```json |
| 49 | +{"token":"your_jwt_token"} |
| 50 | +``` |
| 51 | + |
| 52 | +## API Endpoints |
| 53 | + |
| 54 | +### Login (Public) |
| 55 | + |
| 56 | +```sh |
| 57 | +curl -X POST -H "Content-Type: application/json" -d '{"username": "admin", "password": "password"}' http://localhost:3040/login |
| 58 | +``` |
| 59 | + |
| 60 | +### Create a To-Do |
| 61 | + |
| 62 | +```sh |
| 63 | +curl -X POST \ |
| 64 | + -H "Content-Type: application/json" \ |
| 65 | + -H "Authorization: Bearer your_jwt_token" \ |
| 66 | + -H "Idempotency-Key: unique_key" \ |
| 67 | + -d '{"task": "Learn Go", "progress": "Todo"}' \ |
| 68 | + http://localhost:3040/api/todos |
| 69 | +``` |
| 70 | + |
| 71 | +Note: The Idempotency-Key header is required to prevent duplicate creation of todos. |
| 72 | + |
| 73 | +### Get All To-Dos |
| 74 | + |
| 75 | +```sh |
| 76 | +curl -H "Authorization: Bearer your_jwt_token" http://localhost:3040/api/todos |
| 77 | +``` |
| 78 | + |
| 79 | +### Get a Specific To-Do |
| 80 | + |
| 81 | +```sh |
| 82 | +curl -H "Authorization: Bearer your_jwt_token" http://localhost:3040/api/todos/1 |
| 83 | +``` |
| 84 | + |
| 85 | +### Update a To-Do |
| 86 | + |
| 87 | +```sh |
| 88 | +curl -X PUT \ |
| 89 | + -H "Content-Type: application/json" \ |
| 90 | + -H "Authorization: Bearer your_jwt_token" \ |
| 91 | + -d '{"task": "Learn Keploy", "progress": "Done"}' \ |
| 92 | + http://localhost:3040/api/todos/1 |
| 93 | +``` |
| 94 | + |
| 95 | +### Delete a To-Do |
| 96 | + |
| 97 | +```sh |
| 98 | +curl -X DELETE -H "Authorization: Bearer your_jwt_token" http://localhost:3040/api/todos/1 |
| 99 | +``` |
| 100 | + |
| 101 | +## Features |
| 102 | + |
| 103 | +- **JWT Authentication**: Secure API endpoints with JWT tokens |
| 104 | +- **Request Logging**: Each request is logged with a unique request ID |
| 105 | +- **Idempotency Keys**: Prevent duplicate creation of resources |
| 106 | +- **Error Handling**: Proper error responses with appropriate HTTP status codes |
| 107 | +- **RESTful API Design**: Follow REST principles for API design |
| 108 | + |
| 109 | +## Response Format |
| 110 | + |
| 111 | +Most endpoints return responses in the following format: |
| 112 | + |
| 113 | +```json |
| 114 | +{ |
| 115 | + "request_id": "unique-request-id", |
| 116 | + "timestamp": "2025-03-12T12:00:00Z", |
| 117 | + "todo": { |
| 118 | + "id": 1, |
| 119 | + "task": "Learn Go", |
| 120 | + "progress": "Todo", |
| 121 | + "last_checked": "2025-03-12T12:00:00Z" |
| 122 | + } |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +## License |
| 127 | + |
| 128 | +This project is licensed under the MIT License. |
0 commit comments