๐Ÿš€ Go REST API

Complete Enterprise-Grade REST API with Advanced Features

v1.0.0 Go 1.21 PostgreSQL Redis Docker

๐Ÿ“‹ Project Overview

Welcome to the Go REST API - a comprehensive, enterprise-grade REST API built with Go programming language. This project demonstrates modern API development practices with authentication, database integration, caching, file uploads, and comprehensive testing.

๐Ÿ” Security First

JWT Authentication, Role-Based Access Control, Password Hashing, Rate Limiting

โšก High Performance

Redis Caching, Optimized Queries, Concurrent Processing, Memory Efficient

๐Ÿงช Quality Assured

Unit Tests, Integration Tests, Code Coverage, Comprehensive Documentation

๐Ÿณ Production Ready

Docker Containerization, Environment Configuration, Monitoring, Scalable

๐Ÿ“ Project Structure

Well-organized modular architecture with clear separation of concerns:

๐Ÿ“‚ go-rest-api/
โ”œโ”€โ”€ ๐Ÿ“‚ cmd/
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ api/
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ main.go // Application entry point
โ”‚ โ””โ”€โ”€ ๐Ÿ“‚ seeder/
โ”‚ โ””โ”€โ”€ ๐Ÿ“„ main.go // Database seeding
โ”œโ”€โ”€ ๐Ÿ“‚ internal/
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ cache/
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ cache.go // Redis caching layer
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ config/
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ config.go // Configuration management
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ config_test.go // Config tests
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ database/
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ database.go // Database connection
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ handlers/
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ auth_handler.go // Authentication endpoints
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ product_handler.go // Product CRUD operations
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ category_handler.go // Category management
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ upload_handler.go // File upload handling
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ api_handler.go // General API endpoints
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ *_test.go // Unit tests
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ middleware/
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ auth_middleware.go // JWT authentication
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ role_middleware.go // Role-based access
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ ratelimit.go // Rate limiting
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ cors.go // CORS handling
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ logger.go // Request logging
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ models/
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ user.go // User data model
โ”‚ โ”‚ โ”œโ”€โ”€ ๐Ÿ“„ product.go // Product data model
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ category.go // Category data model
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ response/
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ response.go // API response utilities
โ”‚ โ”œโ”€โ”€ ๐Ÿ“‚ routes/
โ”‚ โ”‚ โ””โ”€โ”€ ๐Ÿ“„ routes.go // Route definitions
โ”‚ โ””โ”€โ”€ ๐Ÿ“‚ validation/
โ”‚ โ””โ”€โ”€ ๐Ÿ“„ validation.go // Input validation
โ”œโ”€โ”€ ๐Ÿ“‚ tests/
โ”‚ โ””โ”€โ”€ ๐Ÿ“„ integration_test.go // Integration tests
โ”œโ”€โ”€ ๐Ÿ“‚ docs/
โ”‚ โ””โ”€โ”€ ๐Ÿ“„ docs.go // Swagger documentation
โ”œโ”€โ”€ ๐Ÿ“„ go.mod // Go module definition
โ”œโ”€โ”€ ๐Ÿ“„ go.sum // Dependency checksums
โ”œโ”€โ”€ ๐Ÿ“„ docker-compose.yml // Docker orchestration
โ”œโ”€โ”€ ๐Ÿ“„ Dockerfile // Container definition
โ”œโ”€โ”€ ๐Ÿ“„ Makefile // Build automation
โ”œโ”€โ”€ ๐Ÿ“„ README.md // Project documentation
โ”œโ”€โ”€ ๐Ÿ“„ RELEASE_NOTES.md // Release documentation
โ”œโ”€โ”€ ๐Ÿ“„ LICENSE // MIT License
โ”œโ”€โ”€ ๐Ÿ“„ .env // Environment variables
โ””โ”€โ”€ ๐Ÿ“„ .gitignore // Git ignore rules

๐Ÿš€ Installation & Setup Guide

1 Clone the Repository
git clone https://github.com/rskworld/go-rest-api.git
cd go-rest-api
2 Using Docker (Recommended)
# Start all services (PostgreSQL, Redis, API)
docker-compose up --build

# API will be available at: http://localhost:8080
# Swagger docs at: http://localhost:8080/swagger/index.html
3 Manual Setup (Alternative)
# Install Go dependencies
go mod download

# Configure environment
cp .env.example .env
# Edit .env with your database settings

# Run database migrations
make seed

# Start the server
make run
๐Ÿ’ก Prerequisites: Docker, Docker Compose, or Go 1.21+ installed on your system.

๐Ÿ”— API Endpoints

Authentication

POST /api/v1/register

User registration with name, email, and password

POST /api/v1/login

User login with JWT token generation

GET /api/v1/profile

Get authenticated user profile (requires Bearer token)

Products Management

GET /api/v1/products

List products with search, filtering, and pagination

GET /api/v1/products/:id

Get specific product details

POST /api/v1/products

Create new product (Admin only)

PUT /api/v1/products/:id

Update product (Admin only)

DELETE /api/v1/products/:id

Delete product (Admin only)

Categories Management

GET /api/v1/categories

List all categories

GET /api/v1/categories/:id

Get category details with products

POST /api/v1/categories

Create category (Admin only)

File Management

POST /api/v1/products/:id/upload

Upload product image (Admin only)

DELETE /api/v1/products/:id/image

Delete product image (Admin only)

System

GET /api/v1/health

Health check endpoint

GET /swagger/*

API documentation (Swagger UI)

๐Ÿ’ก Usage Examples

1. User Registration

curl -X POST http://localhost:8080/api/v1/register \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com",
"password": "password123"
}'

2. User Login

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

3. Get Products with Search

curl "http://localhost:8080/api/v1/products?search=laptop&page=1&limit=10"

4. Create Product (Admin)

curl -X POST http://localhost:8080/api/v1/products \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-H "Content-Type: application/json" \
-d '{
"name": "Gaming Laptop",
"description": "High-performance gaming laptop",
"price": 1299.99,
"stock": 50,
"category_id": 1
}'

5. Upload Product Image

curl -X POST http://localhost:8080/api/v1/products/1/upload \
-H "Authorization: Bearer YOUR_JWT_TOKEN" \
-F "image=@laptop.jpg"

๐Ÿงช Testing & Quality Assurance

Running Tests

# Run all tests
make test

# Run tests with coverage
make test-coverage

# Run integration tests
go test ./tests/...

# View coverage report
open coverage.html

Test Coverage Includes:

โš™๏ธ Configuration

Environment Variables (.env)

# Database Configuration
DB_HOST=localhost
DB_USER=postgres
DB_PASSWORD=your_password
DB_NAME=gorestapi
DB_PORT=5432

# Authentication
JWT_SECRET=your_jwt_secret_key

# Server
PORT=8080

# File Upload
UPLOAD_PATH=./uploads

# Redis Cache
REDIS_HOST=localhost
REDIS_PORT=6379
CACHE_ENABLED=true

Makefile Commands

make run # Start the server
make build # Build the application
make test # Run all tests
make seed # Run database seeder
make docker-up # Start Docker services
make docker-down # Stop Docker services
make clean # Clean build artifacts

๐Ÿ—๏ธ Architecture & Features

๐Ÿ” Security

JWT Authentication
Role-Based Access Control
Password Hashing
Rate Limiting
CORS Protection

๐Ÿ“Š Database

PostgreSQL Integration
GORM ORM
Automatic Migrations
Relationship Management
Query Optimization

โšก Performance

Redis Caching
Efficient Pagination
Concurrent Processing
Memory Optimization
Database Indexing

๐Ÿงช Quality

Unit Testing
Integration Testing
Code Coverage
Input Validation
Error Handling

๐Ÿณ DevOps

Docker Containerization
Environment Config
Automated Testing
CI/CD Ready
Production Deployment

๐Ÿ“š Documentation

Swagger API Docs
Comprehensive README
Code Comments
Usage Examples
Architecture Guide

๐Ÿ”— Links & Resources

๐Ÿ“– Documentation

Complete README
Release Notes
API Documentation

๐Ÿ“ฆ Repository

GitHub Repository
Version Tags
Releases

๐Ÿ› ๏ธ Technologies

Go Programming
Gin Framework
GORM ORM

๐Ÿ“ž Support

Email Support
Website
Contact Form