Skip to content

shafikshaon/go-boilerplate

Repository files navigation

go-boilerplate

A production-ready RESTful API built with Go, Gin framework, PostgreSQL, Redis, and clean architecture principles.

πŸš€ Features

  • πŸ—οΈ 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

πŸ“‹ Prerequisites

  • Go 1.21+
  • Docker & Docker Compose
  • Make (optional but recommended)

πŸ› οΈ Quick Start

🎯 One-Command Setup

./run.sh

Step-by-Step Setup

# 1. Install dependencies
make setup

# 2. Start databases
make db-up

# 3. Run the application
make run

Development Workflow

make dev  # Starts databases and runs app

πŸ§ͺ Test the API

./test_api.sh

πŸ“š API Endpoints

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

πŸ’» Example Requests

Register User

curl -X POST http://localhost:8080/api/v1/auth/register \
  -H "Content-Type: application/json" \
  -d '{
    "name": "John Doe",
    "email": "[email protected]",
    "password": "password123"
  }'

Login

curl -X POST http://localhost:8080/api/v1/auth/login \
  -H "Content-Type: application/json" \
  -d '{
    "email": "[email protected]",
    "password": "password123"
  }'

Get User (Cached)

curl -X GET http://localhost:8080/api/v1/users/1

πŸ—οΈ Project Structure

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

βš™οΈ Configuration

Environment Variables (.env)

# 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

πŸ› οΈ Development Commands

Database Management

make db-up         # Start PostgreSQL & Redis
make db-down       # Stop databases
make psql          # Connect to PostgreSQL
make redis-cli     # Connect to Redis CLI

Development

make setup         # Install dependencies
make run           # Run application
make dev           # Start databases and run app
make test          # Test API endpoints
make build         # Build binary

Docker Operations

make docker-up     # Start all services
make docker-down   # Stop all services

πŸ—„οΈ Database Features

PostgreSQL

  • Auto-migrations with GORM
  • Connection pooling
  • Transaction support

Redis Caching

  • User profile caching (30 min TTL)
  • JWT session management (24 hour TTL)
  • Cache invalidation on updates

πŸ”’ Security Features

  • JWT Authentication with Redis session storage
  • Password Hashing using bcrypt
  • Input Validation with comprehensive error handling
  • CORS middleware configuration
  • SQL Injection protection via GORM

πŸ“¦ Deployment

Production with Docker

docker-compose up -d --build

Manual Deployment

make build
./bin/app

πŸ“‹ Available Make Commands

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

πŸŽ‰ You're All Set!

Your Go API with PostgreSQL and Redis is ready for production!

Happy coding! πŸš€

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published