Skip to content

thenic-maker/task-manager-api

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jwtmini — Task Manager API

A simple Go backend demonstrating REST API (Gin), JWT Auth, Postgres, and a Worker Queue.

Architecture

Client
  │
  ├─ POST /register        → Create account
  ├─ POST /login            → Get JWT token
  │
  └─ [JWT Protected]
     ├─ POST /api/tasks     → Create task → pushed to Worker Queue
     ├─ GET  /api/tasks     → List my tasks
     └─ GET  /api/tasks/:id → Get task status/result
                                    │
                              ┌─────┴─────┐
                              │ Worker Pool │  (3 goroutines)
                              │  picks up   │
                              │  tasks from  │
                              │  channel     │
                              └─────────────┘
                                    │
                              Updates DB with result

Project Structure

jwtmini/
├── main.go              # Entry point: wires everything together
├── config/config.go     # Env-based configuration
├── db/db.go             # Postgres connection + auto-migration
├── model/model.go       # User & Task models (GORM)
├── middleware/jwt.go     # JWT generation + auth middleware
├── handler/auth.go      # Register & Login handlers
├── handler/task.go      # Task CRUD handlers
├── queue/queue.go       # In-memory worker queue (channels)
└── docker-compose.yml   # Postgres container

Quick Start

# 1. Start Postgres
docker compose up -d

# 2. Install dependencies
go mod tidy

# 3. Run the server
go run main.go

API Usage

Register

curl -X POST http://localhost:8080/register \
  -H "Content-Type: application/json" \
  -d '{"email": "nitin@example.com", "password": "secret123"}'

Login (get JWT)

curl -X POST http://localhost:8080/login \
  -H "Content-Type: application/json" \
  -d '{"email": "nitin@example.com", "password": "secret123"}'

# Response: {"token": "eyJhbGciOi..."}

Create Task (queued for background processing)

curl -X POST http://localhost:8080/api/tasks \
  -H "Authorization: Bearer <YOUR_TOKEN>" \
  -H "Content-Type: application/json" \
  -d '{"title": "Send welcome email", "payload": "user@example.com"}'

List Tasks

curl http://localhost:8080/api/tasks \
  -H "Authorization: Bearer <YOUR_TOKEN>"

Get Task (check if worker processed it)

curl http://localhost:8080/api/tasks/1 \
  -H "Authorization: Bearer <YOUR_TOKEN>"

Key Concepts Demonstrated

Concept Where
REST API Gin router in main.go, handlers in handler/
JWT Auth Token generation & middleware in middleware/jwt.go
Postgres + ORM GORM auto-migration in db/db.go, models in model/
Worker Queue Channel-based queue in queue/queue.go, 3 goroutines process tasks
Password Hashing bcrypt in handler/auth.go
Graceful Shutdown Signal handling in main.go stops workers cleanly

Environment Variables

Variable Default Description
DB_HOST localhost Postgres host
DB_PORT 5432 Postgres port
DB_USER postgres Postgres user
DB_PASSWORD postgres Postgres password
DB_NAME jwtmini Database name
JWT_SECRET (dev default) Change in production!
PORT 8080 Server port

About

Jwt, postgres docker

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages