Skip to content

Commit 99e2f61

Browse files
committed
simple todo api for testing keploy idempotency new feature
Signed-off-by: AHMED-D007A <ahmed.mamdouh8840@gmail.com>
1 parent aabe0a9 commit 99e2f61

File tree

8 files changed

+640
-0
lines changed

8 files changed

+640
-0
lines changed

todo-mux/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vscode
2+
test.txt
3+
todo-go

todo-mux/README.md

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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.

todo-mux/docker-compose.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: "3.8"
2+
3+
services:
4+
db:
5+
image: postgres:13
6+
environment:
7+
POSTGRES_USER: user
8+
POSTGRES_PASSWORD: password
9+
POSTGRES_DB: todo_db
10+
ports:
11+
- "5432:5432"
12+
volumes:
13+
- postgres_data:/var/lib/postgresql/data
14+
15+
volumes:
16+
postgres_data:

todo-mux/go.mod

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module todo-go
2+
3+
go 1.23.4
4+
5+
require (
6+
github.com/gorilla/mux v1.8.1
7+
github.com/lib/pq v1.10.9
8+
)
9+
10+
require github.com/google/uuid v1.6.0
11+
12+
require github.com/golang-jwt/jwt/v5 v5.2.1

todo-mux/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
2+
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
3+
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
4+
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
5+
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
6+
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
7+
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
8+
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=

0 commit comments

Comments
 (0)