A production-ready RESTful API for task management built with Go, Gin, GORM, and PostgreSQL. Features JWT authentication, comprehensive testing, and interactive Swagger documentation.
- β Complete CRUD operations for tasks
- β JWT Authentication with secure password hashing (bcrypt)
- β User Management (register, login, profile)
- β Advanced Querying (filtering, pagination, search)
- β Task Priorities (low, medium, high) & due dates
- β Unit Tests with comprehensive coverage
- β Swagger Documentation (interactive API explorer)
- β CORS Enabled for frontend integration
- β Standardized JSON responses
- β Soft Deletes with GORM
| Component | Technology |
|---|---|
| Language | Go 1.23+ |
| Framework | Gin Web Framework |
| ORM | GORM |
| Database | PostgreSQL 12+ |
| Authentication | JWT (golang-jwt/jwt) |
| Password | bcrypt |
| Documentation | Swagger/OpenAPI |
| Testing | testify |
task-app/
βββ main.go # Entry point with Swagger annotations
βββ config/
β βββ database.go # GORM database configuration
βββ models/
β βββ task.go # Task model with GORM tags
β βββ user.go # User model with bcrypt
β βββ response.go # Standardized API responses
βββ handlers/
β βββ task_handler.go # Task CRUD + filtering/search
β βββ auth_handler.go # Register/Login/Profile
βββ middleware/
β βββ auth.go # JWT authentication middleware
βββ migrations/
β βββ init.sql # Initial database schema
βββ docs/ # Auto-generated Swagger docs
βββ go.mod # Go dependencies
βββ README.md
- Go 1.23+
- PostgreSQL 12+
- psql CLI tool
# 1. Clone/Navigate to project
cd /Users/shohel/goProjects/task-app
# 2. Install dependencies
go mod download
# 3. Create database
psql -U shohel -c "CREATE DATABASE taskdb;"
# 4. Run migrations (GORM auto-migrates on startup)
# Tables: tasks, users (created automatically)
# 5. Start server
go run main.goServer runs on: http://localhost:8086
Swagger UI: http://localhost:8086/api/v1/swagger/index.html
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/register |
Create new user account |
| POST | /api/v1/login |
Login and get JWT token |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/profile |
Get current user profile |
| Method | Endpoint | Description |
|---|---|---|
| POST | /api/v1/tasks |
Create new task |
| GET | /api/v1/tasks |
Get all tasks (with filters) |
| GET | /api/v1/tasks/:id |
Get task by ID |
| PUT | /api/v1/tasks/:id |
Update task |
| DELETE | /api/v1/tasks/:id |
Delete task (soft delete) |
GET /api/v1/tasks supports:
?status=pending- Filter by status (pending, in_progress, completed)?search=keyword- Search in title/description?page=1&limit=10- Pagination (default: page=1, limit=10)
curl -X POST http://localhost:8086/api/v1/register \
-H "Content-Type: application/json" \
-d '{
"username": "john",
"email": "john@example.com",
"password": "password123",
"full_name": "John Doe"
}'Response:
{
"status": "success",
"message": "User registered successfully",
"data": {
"token": "eyJhbGciOiJIUzI1NiIs...",
"user": {
"ID": 1,
"username": "john",
"email": "john@example.com",
"full_name": "John Doe"
}
}
}Add JWT token to Authorization header:
curl http://localhost:8086/api/v1/tasks \
-H "Authorization: Bearer YOUR_JWT_TOKEN"curl -X POST http://localhost:8086/api/v1/tasks \
-H "Content-Type: application/json" \
-H "Authorization: Bearer YOUR_TOKEN" \
-d '{
"title": "Complete API",
"description": "Finish task management API",
"status": "in_progress",
"priority": "high",
"due_date": "2025-12-31T23:59:59Z"
}'# Get pending tasks
curl "http://localhost:8086/api/v1/tasks?status=pending" \
-H "Authorization: Bearer YOUR_TOKEN"
# Search tasks
curl "http://localhost:8086/api/v1/tasks?search=API" \
-H "Authorization: Bearer YOUR_TOKEN"
# Paginate (page 2, 20 items)
curl "http://localhost:8086/api/v1/tasks?page=2&limit=20" \
-H "Authorization: Bearer YOUR_TOKEN"# Run all tests
go test ./handlers/... -v
# Run specific test
go test ./handlers/... -run TestRegister -v
# Test coverage
go test ./handlers/... -coverTest Database: taskdb_test (auto-created)
Interactive API Explorer: http://localhost:8086/api/v1/swagger/index.html
Features:
- Try all endpoints directly from browser
- Automatic request/response examples
- JWT authentication support
- Model schemas
# Install swag CLI
go install github.com/swaggo/swag/cmd/swag@latest
# Generate docs
~/go/bin/swag initEdit main.go:
dbConfig := config.DatabaseConfig{
Host: "localhost",
Port: 5432,
User: "shohel",
Password: "", // Your password
DBName: "taskdb",
}Edit middleware/auth.go:
var JWTSecret = []byte("your-secret-key-change-this-in-production")Edit main.go:
port := ":8086" // Change to desired portid SERIAL PRIMARY KEY
title VARCHAR(255) NOT NULL
description TEXT
status VARCHAR(50) DEFAULT 'pending'
priority VARCHAR(20) DEFAULT 'medium'
due_date TIMESTAMP
created_at TIMESTAMP
updated_at TIMESTAMP
deleted_at TIMESTAMP -- Soft deleteid SERIAL PRIMARY KEY
username VARCHAR(100) UNIQUE NOT NULL
email VARCHAR(255) UNIQUE NOT NULL
password VARCHAR(255) NOT NULL -- bcrypt hashed
full_name VARCHAR(255)
created_at TIMESTAMP
updated_at TIMESTAMP
deleted_at TIMESTAMPStatus: pending | in_progress | completed
Priority: low | medium | high
MIT License - Free for educational and commercial use.