A production-ready, scalable e-commerce backend built with Go microservices architecture, featuring gRPC inter-service communication, MongoDB persistence, JWT authentication, and Docker containerization.
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
- gRPC - High-performance inter-service communication
- Protocol Buffers - Efficient data serialization
- REST API - External client communication via Gateway
- β 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 CRUD operations
- β Inventory management
- β Product search and filtering
- β MongoDB product catalog
- β gRPC endpoints for internal communication
- β Add/Remove items from cart
- β Update cart quantities
- β Cart persistence
- β User-specific cart management
- β Integration with Product service via gRPC
- β Unified API endpoint
- β Request routing to microservices
- β Load balancing
- β Authentication middleware
- Language: Go 1.25.3
- Database: MongoDB
- Communication: gRPC, Protocol Buffers
- Authentication: JWT (golang-jwt/jwt/v5)
- Containerization: Docker, Docker Compose
- 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 managementgo-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
- Go 1.25.3 or higher
- Docker & Docker Compose
- MongoDB (or use Docker)
- Protocol Buffer Compiler (protoc)
git clone https://github.com/sachinggsingh/go-microservice.git
cd go-microserviceCreate .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=5product/.env
MONGO_URI=mongodb://localhost:27017
DB_NAME=ecommerce
PORT=8081cart/.env
MONGO_URI=mongodb://localhost:27017
DB_NAME=ecommerce
PORT=8082# 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# 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# 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.goPOST /api/auth/register
Content-Type: application/json
{
"email": "user@example.com",
"password": "securepassword",
"name": "John Doe"
}POST /api/auth/login
Content-Type: application/json
{
"email": "user@example.com",
"password": "securepassword"
}
Response:
{
"token": "eyJhbGciOiJIUzI1NiIs...",
"user": {...}
}GET /api/products
Authorization: Bearer <token>GET /api/products/:id
Authorization: Bearer <token>POST /api/products
Authorization: Bearer <token>
Content-Type: application/json
{
"name": "Product Name",
"description": "Product Description",
"price": 99.99,
"stock": 100
}GET /api/cart
Authorization: Bearer <token>POST /api/cart/add
Authorization: Bearer <token>
Content-Type: application/json
{
"product_id": "product_id_here",
"quantity": 2
}PUT /api/cart/update
Authorization: Bearer <token>
Content-Type: application/json
{
"product_id": "product_id_here",
"quantity": 5
}DELETE /api/cart/remove/:product_id
Authorization: Bearer <token>- 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
# Run tests for all services
go test ./...
# Run tests with coverage
go test -cover ./...
# Run tests for specific service
cd auth
go test ./internal/...Each microservice includes:
- Multi-stage builds for optimized image size
- Non-root user for security
- .dockerignore for efficient builds
- Health checks for container monitoring
- Structured logging with contextual information
- Error tracking and handling
- Service health endpoints
- Request/Response logging
| Service | Port | Protocol |
|---|---|---|
| Auth | 8080 | HTTP/gRPC |
| Product | 8081 | HTTP/gRPC |
| Cart | 8082 | HTTP/gRPC |
| Gateway | TBD | HTTP |
Services communicate via gRPC for:
- Token validation (Auth β Cart/Product)
- Product details (Product β Cart)
- High-performance data exchange
- 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)
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
Sachin Singh
- GitHub: @sachinggsingh
- 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