A production-ready RESTful API built with Go, Gin framework, PostgreSQL, Redis, and clean architecture principles.
- ποΈ Clean Architecture - Layered architecture with dependency injection
- π₯ Gin Framework - High-performance HTTP web framework
- π PostgreSQL - Robust relational database
- π΄ Redis - In-memory caching and session management
- π JWT Authentication - Secure token-based authentication with Redis sessions
- π GORM - Feature-rich ORM for database operations
- π³ Docker Support - Full containerization with Docker Compose
- β Input Validation - Comprehensive request validation
- π Structured Logging - Production-ready logging
- β‘ Caching - Redis-based user and session caching
- Go 1.21+
- Docker & Docker Compose
- Make (optional but recommended)
./run.sh
# 1. Install dependencies
make setup
# 2. Start databases
make db-up
# 3. Run the application
make run
make dev # Starts databases and runs app
./test_api.sh
Method | Endpoint | Description | Auth Required |
---|---|---|---|
GET | /health |
Service health status | No |
POST | /api/v1/auth/register |
Register new user | No |
POST | /api/v1/auth/login |
Login user | No |
GET | /api/v1/auth/me |
Get current user profile | Yes |
POST | /api/v1/auth/logout |
Logout user | Yes |
POST | /api/v1/users |
Create user | No |
GET | /api/v1/users |
Get all users (paginated) | No |
GET | /api/v1/users/:id |
Get user by ID (cached) | No |
PUT | /api/v1/users/:id |
Update user | Yes |
DELETE | /api/v1/users/:id |
Delete user | Yes |
curl -X POST http://localhost:8080/api/v1/auth/register \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "[email protected]",
"password": "password123"
}'
curl -X POST http://localhost:8080/api/v1/auth/login \
-H "Content-Type: application/json" \
-d '{
"email": "[email protected]",
"password": "password123"
}'
curl -X GET http://localhost:8080/api/v1/users/1
go-boilerplate/
βββ cmd/ # Application initialization
βββ handlers/ # HTTP request handlers
βββ routes/ # Route definitions
βββ models/ # Data models & DTOs
β βββ request/ # Request models
β βββ response/ # Response models
βββ services/ # Business logic layer
β βββ interfaces/ # Service interfaces
βββ repository/ # Data access layer
β βββ interfaces/ # Repository interfaces
βββ database/ # Database connections
βββ middleware/ # Custom middleware
βββ utilities/ # Helper functions & Redis utils
βββ config/ # Configuration management
βββ scripts/ # Utility scripts
# Database
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=postgres
DB_NAME=app_db
# Redis
REDIS_HOST=localhost
REDIS_PORT=6379
REDIS_DB=0
# Application
PORT=8080
JWT_SECRET=your-super-secret-jwt-key
make db-up # Start PostgreSQL & Redis
make db-down # Stop databases
make psql # Connect to PostgreSQL
make redis-cli # Connect to Redis CLI
make setup # Install dependencies
make run # Run application
make dev # Start databases and run app
make test # Test API endpoints
make build # Build binary
make docker-up # Start all services
make docker-down # Stop all services
- Auto-migrations with GORM
- Connection pooling
- Transaction support
- User profile caching (30 min TTL)
- JWT session management (24 hour TTL)
- Cache invalidation on updates
- JWT Authentication with Redis session storage
- Password Hashing using bcrypt
- Input Validation with comprehensive error handling
- CORS middleware configuration
- SQL Injection protection via GORM
docker-compose up -d --build
make build
./bin/app
Command | Description |
---|---|
make setup |
Install dependencies |
make db-up |
Start databases |
make run |
Run the application |
make dev |
Start databases and run app |
make test |
Test API endpoints |
make clean |
Clean up containers |
Your Go API with PostgreSQL and Redis is ready for production!
Happy coding! π