Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions todo-mux/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode
test.txt
todo-go
128 changes: 128 additions & 0 deletions todo-mux/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Todo-Go Application

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.

## Prerequisites

- Go 1.16 or later
- Docker and Docker Compose
- PostgreSQL

## Getting Started

### 1. Start the PostgreSQL database

Start the PostgreSQL database using Docker Compose:

```sh
docker-compose up -d
```

To stop and remove the volume and its data, use:

```sh
docker-compose down -v
```

### 2. Run the application

Run the Go application:

```sh
go build .
./todo-go
```

The server will start running on `http://localhost:3040`.

## Authentication

This application uses JWT for authentication. Before accessing protected endpoints, you need to obtain a token:

```sh
curl -X POST -H "Content-Type: application/json" -d '{"username": "admin", "password": "password"}' http://localhost:3040/login
```

The response will contain a token that should be used in subsequent requests:

```json
{"token":"your_jwt_token"}
```

## API Endpoints

### Login (Public)

```sh
curl -X POST -H "Content-Type: application/json" -d '{"username": "admin", "password": "password"}' http://localhost:3040/login
```

### Create a To-Do

```sh
curl -X POST \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_jwt_token" \
-H "Idempotency-Key: unique_key" \
-d '{"task": "Learn Go", "progress": "Todo"}' \
http://localhost:3040/api/todos
```

Note: The Idempotency-Key header is required to prevent duplicate creation of todos.

### Get All To-Dos

```sh
curl -H "Authorization: Bearer your_jwt_token" http://localhost:3040/api/todos
```

### Get a Specific To-Do

```sh
curl -H "Authorization: Bearer your_jwt_token" http://localhost:3040/api/todos/1
```

### Update a To-Do

```sh
curl -X PUT \
-H "Content-Type: application/json" \
-H "Authorization: Bearer your_jwt_token" \
-d '{"task": "Learn Keploy", "progress": "Done"}' \
http://localhost:3040/api/todos/1
```

### Delete a To-Do

```sh
curl -X DELETE -H "Authorization: Bearer your_jwt_token" http://localhost:3040/api/todos/1
```

## Features

- **JWT Authentication**: Secure API endpoints with JWT tokens
- **Request Logging**: Each request is logged with a unique request ID
- **Idempotency Keys**: Prevent duplicate creation of resources
- **Error Handling**: Proper error responses with appropriate HTTP status codes
- **RESTful API Design**: Follow REST principles for API design

## Response Format

Most endpoints return responses in the following format:

```json
{
"request_id": "unique-request-id",
"timestamp": "2025-03-12T12:00:00Z",
"todo": {
"id": 1,
"task": "Learn Go",
"progress": "Todo",
"last_checked": "2025-03-12T12:00:00Z"
}
}
```

## License

This project is licensed under the MIT License.
16 changes: 16 additions & 0 deletions todo-mux/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
version: "3.8"

services:
db:
image: postgres:13
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: todo_db
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data

volumes:
postgres_data:
12 changes: 12 additions & 0 deletions todo-mux/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module todo-go

go 1.23.4

require (
github.com/gorilla/mux v1.8.1
github.com/lib/pq v1.10.9
)

require github.com/google/uuid v1.6.0

require github.com/golang-jwt/jwt/v5 v5.2.1
8 changes: 8 additions & 0 deletions todo-mux/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/mux v1.8.1 h1:TuBL49tXwgrFYWhqrNgrUNEY92u81SPhu7sTdzQEiWY=
github.com/gorilla/mux v1.8.1/go.mod h1:AKf9I4AEqPTmMytcMc0KkNouC66V3BtZ4qD5fmWSiMQ=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
Loading