Skip to content

sachinggsingh/Go-microservices

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

20 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Go Microservices E-Commerce Platform

Microservices Architecture Diagram

A production-ready, scalable e-commerce backend built with Go microservices architecture, featuring gRPC inter-service communication, MongoDB persistence, JWT authentication, and Docker containerization.

πŸ—οΈ Architecture Overview

This project implements a microservices-based architecture with the following services:

  • Auth Service - User authentication and authorization with JWT tokens
  • Product Service - Product catalog management and inventory
  • Cart Service - Shopping cart operations and management
  • Gateway Service - API Gateway for routing and service orchestration

Communication Patterns

  • gRPC - High-performance inter-service communication
  • Protocol Buffers - Efficient data serialization
  • REST API - External client communication via Gateway

πŸš€ Features

Authentication Service (Port: 8080)

  • βœ… User registration with password hashing (bcrypt)
  • βœ… JWT-based authentication
  • βœ… Token validation and refresh
  • βœ… MongoDB user persistence
  • βœ… gRPC service for token validation
  • βœ… Redis-backed token caching and blacklist
  • βœ… Rate limiting (IP-based) using Redis

Product Service (Port: 8081)

  • βœ… Product CRUD operations
  • βœ… Inventory management
  • βœ… Product search and filtering
  • βœ… MongoDB product catalog
  • βœ… gRPC endpoints for internal communication

Cart Service (Port: 8082)

  • βœ… Add/Remove items from cart
  • βœ… Update cart quantities
  • βœ… Cart persistence
  • βœ… User-specific cart management
  • βœ… Integration with Product service via gRPC

Gateway Service

  • βœ… Unified API endpoint
  • βœ… Request routing to microservices
  • βœ… Load balancing
  • βœ… Authentication middleware

πŸ› οΈ Tech Stack

Core Technologies

  • Language: Go 1.25.3
  • Database: MongoDB
  • Communication: gRPC, Protocol Buffers
  • Authentication: JWT (golang-jwt/jwt/v5)
  • Containerization: Docker, Docker Compose

Key Dependencies

- google.golang.org/grpc v1.76.0          // gRPC framework
- go.mongodb.org/mongo-driver v1.17.6     // MongoDB driver
- github.com/golang-jwt/jwt/v5 v5.3.0     // JWT authentication
- golang.org/x/crypto v0.40.0             // Password hashing
- github.com/joho/godotenv v1.5.1         // Environment management

πŸ“ Project Structure

go-microservice/
β”œβ”€β”€ auth/                    # Authentication microservice
β”‚   β”œβ”€β”€ cmd/                 # Application entry point
β”‚   β”œβ”€β”€ internal/
β”‚   β”‚   β”œβ”€β”€ api/            # HTTP/gRPC handlers
β”‚   β”‚   β”œβ”€β”€ config/         # Configuration management
β”‚   β”‚   β”œβ”€β”€ helper/         # Utility functions
β”‚   β”‚   β”œβ”€β”€ intra/          # Inter-service communication
β”‚   β”‚   β”œβ”€β”€ model/          # Data models
β”‚   β”‚   β”œβ”€β”€ repository/     # Database layer
β”‚   β”‚   └── service/        # Business logic
β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”œβ”€β”€ go.mod
β”‚   └── .env
β”‚
β”œβ”€β”€ product/                 # Product microservice
β”‚   β”œβ”€β”€ cmd/
β”‚   β”œβ”€β”€ internal/
β”‚   β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”œβ”€β”€ intra/
β”‚   β”‚   β”œβ”€β”€ model/
β”‚   β”‚   β”œβ”€β”€ repository/
β”‚   β”‚   └── service/
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── go.mod
β”‚
β”œβ”€β”€ cart/                    # Cart microservice
β”‚   β”œβ”€β”€ cmd/
β”‚   β”œβ”€β”€ internal/
β”‚   β”‚   β”œβ”€β”€ api/
β”‚   β”‚   β”œβ”€β”€ config/
β”‚   β”‚   β”œβ”€β”€ errors/
β”‚   β”‚   β”œβ”€β”€ intra/
β”‚   β”‚   β”œβ”€β”€ middleware/
β”‚   β”‚   β”œβ”€β”€ model/
β”‚   β”‚   β”œβ”€β”€ pkg/
β”‚   β”‚   β”œβ”€β”€ repository/
β”‚   β”‚   └── service/
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── go.mod
β”‚
β”œβ”€β”€ gateway/                 # API Gateway
β”‚   β”œβ”€β”€ cmd/
β”‚   β”œβ”€β”€ internal/
β”‚   β”œβ”€β”€ Dockerfile
β”‚   └── go.mod
β”‚
β”œβ”€β”€ proto/                   # Protocol Buffer definitions
β”‚   β”œβ”€β”€ product.proto
β”‚   β”œβ”€β”€ showProduct.proto
β”‚   └── validateToken.proto
β”‚
β”œβ”€β”€ pb/                      # Generated protobuf code
β”œβ”€β”€ docker-compose.yaml      # Multi-container orchestration
└── README.md

πŸ”§ Installation & Setup

Prerequisites

  • Go 1.25.3 or higher
  • Docker & Docker Compose
  • MongoDB (or use Docker)
  • Protocol Buffer Compiler (protoc)

1. Clone the Repository

git clone https://github.com/sachinggsingh/go-microservice.git
cd go-microservice

2. Environment Configuration

Create .env files for each service:

auth/.env

MONGO_URI=mongodb://localhost:27017
DB_NAME=ecommerce
JWT_SECRET=your-secret-key-here
PORT=8080

# Redis configuration (used for token cache, blacklist and rate limiter)
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=
REDIS_DB=0

# Rate limiting (defaults shown)
# Maximum attempts before blocking
RATE_LIMIT_MAX_ATTEMPTS=5
# Window in minutes for counting attempts
RATE_LIMIT_WINDOW_MINUTES=5

product/.env

MONGO_URI=mongodb://localhost:27017
DB_NAME=ecommerce
PORT=8081

cart/.env

MONGO_URI=mongodb://localhost:27017
DB_NAME=ecommerce
PORT=8082

3. Generate Protocol Buffers

# Install protoc-gen-go and protoc-gen-go-grpc
go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest

# Generate Go code from proto files
protoc --go_out=. --go-grpc_out=. proto/*.proto

4. Run with Docker Compose

# Build and start all services
docker-compose up --build

# Run in detached mode
docker-compose up -d

# View logs
docker-compose logs -f

# Stop all services
docker-compose down

5. Run Locally (Development)

# Terminal 1 - Auth Service
cd auth
go mod download
go run cmd/main.go

# Terminal 2 - Product Service
cd product
go mod download
go run cmd/main.go

# Terminal 3 - Cart Service
cd cart
go mod download
go run cmd/main.go

# Terminal 4 - Gateway Service
cd gateway
go mod download
go run cmd/main.go

πŸ“‘ API Documentation

Authentication Endpoints

Register User

POST /api/auth/register
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "securepassword",
  "name": "John Doe"
}

Login

POST /api/auth/login
Content-Type: application/json

{
  "email": "user@example.com",
  "password": "securepassword"
}

Response:
{
  "token": "eyJhbGciOiJIUzI1NiIs...",
  "user": {...}
}

Product Endpoints

Get All Products

GET /api/products
Authorization: Bearer <token>

Get Product by ID

GET /api/products/:id
Authorization: Bearer <token>

Create Product

POST /api/products
Authorization: Bearer <token>
Content-Type: application/json

{
  "name": "Product Name",
  "description": "Product Description",
  "price": 99.99,
  "stock": 100
}

Cart Endpoints

Get Cart

GET /api/cart
Authorization: Bearer <token>

Add to Cart

POST /api/cart/add
Authorization: Bearer <token>
Content-Type: application/json

{
  "product_id": "product_id_here",
  "quantity": 2
}

Update Cart Item

PUT /api/cart/update
Authorization: Bearer <token>
Content-Type: application/json

{
  "product_id": "product_id_here",
  "quantity": 5
}

Remove from Cart

DELETE /api/cart/remove/:product_id
Authorization: Bearer <token>

πŸ”’ Security Features

  • Password Hashing: bcrypt with salt rounds
  • JWT Authentication: Secure token-based auth
  • Environment Variables: Sensitive data protection
  • Input Validation: Request payload validation
  • CORS: Cross-origin resource sharing configuration

πŸ§ͺ Testing

# Run tests for all services
go test ./...

# Run tests with coverage
go test -cover ./...

# Run tests for specific service
cd auth
go test ./internal/...

🐳 Docker Configuration

Each microservice includes:

  • Multi-stage builds for optimized image size
  • Non-root user for security
  • .dockerignore for efficient builds
  • Health checks for container monitoring

πŸ“Š Monitoring & Logging

  • Structured logging with contextual information
  • Error tracking and handling
  • Service health endpoints
  • Request/Response logging

🚦 Service Ports

Service Port Protocol
Auth 8080 HTTP/gRPC
Product 8081 HTTP/gRPC
Cart 8082 HTTP/gRPC
Gateway TBD HTTP

πŸ”„ Inter-Service Communication

Services communicate via gRPC for:

  • Token validation (Auth β†’ Cart/Product)
  • Product details (Product β†’ Cart)
  • High-performance data exchange

πŸ›£οΈ Roadmap

  • Redis caching layer
  • Message queue (RabbitMQ/Kafka)
  • Service mesh (Istio)
  • Kubernetes deployment
  • CI/CD pipeline
  • API rate limiting
  • Distributed tracing (Jaeger)
  • Metrics collection (Prometheus/Grafana)

🀝 Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

πŸ“ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ‘¨β€πŸ’» Author

Sachin Singh

πŸ™ Acknowledgments

  • Go community for excellent libraries
  • gRPC team for the framework
  • MongoDB team for the driver
  • Docker for containerization

Built with ❀️ using Go and Microservices Architecture

About

It's an E-commerce Backend system following the microservices architecture.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published