A complete microservices architecture built with Go, featuring user management, product catalog, order processing, and API gateway with PostgreSQL databases and Docker containerization.
- Architecture Overview
- Tech Stack
- Prerequisites
- Quick Start
- Database Migration with dbmate
- API Documentation
- Testing with cURL
- Project Structure
- Services
- Troubleshooting
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β β β β β β
β NGINX ββββββ API Gateway ββββββ User Service β
β (Port 80) β β (Port 8000) β β (Port 8001) β
β β β β β β
βββββββββββββββββββ ββββββββββββββββββββ βββββββββββββββββββ
β
ββββββββββββββββββββββββββββββββββββββ
β β
βββββββββββββββββββ βββββββββββββββββββ
β β β β
β Product Service β β Order Service β
β (Port 8002) β β (Port 8003) β
β β β β
βββββββββββββββββββ βββββββββββββββββββ
β β
ββββββββββββββββββ¬ββββββββββββββββββββ
β
βββββββββββββββββββββββ
β β
β PostgreSQL β
β (Port 5432) β
β βββββββββββββββββββ β
β β users_db β β
β β products_db β β
β β orders_db β β
β βββββββββββββββββββ β
βββββββββββββββββββββββ
- Backend: Go 1.23
- Database: PostgreSQL 15
- Migration Tool: dbmate
- Containerization: Docker & Docker Compose
- Reverse Proxy: NGINX
- Libraries:
- Gorilla Mux (HTTP routing)
- PostgreSQL Driver (lib/pq)
- JWT Authentication (golang-jwt/jwt)
- Bcrypt (password hashing)
Before running this project, make sure you have:
- Docker installed
- Docker Compose installed
- Go 1.23+ (for local development)
- dbmate (for database migrations)
# Using go install
go install github.com/amacneil/dbmate/v2@latest
# Or using curl (Linux/macOS)
sudo curl -fsSL -o /usr/local/bin/dbmate https://github.com/amacneil/dbmate/releases/latest/download/dbmate-linux-amd64
sudo chmod +x /usr/local/bin/dbmategit clone https://github.com/hariomop12/MicroService.git
cd MicroService# Build and start all containers
docker-compose up --build -d
# Check service status
docker-compose psSet up all databases:
# Users Database
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/users_db?sslmode=disable"
dbmate up
# Products Database
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/products_db?sslmode=disable"
dbmate up
# Orders Database
export DATABASE_URL="postgresql://postgres:postgres@localhost:5432/orders_db?sslmode=disable"
dbmate upAlternative - Manual Database Setup:
# Create tables manually if dbmate migration fails
docker exec -it postgres_main psql -U postgres -d users_db -c "
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL,
username VARCHAR(100) UNIQUE NOT NULL,
password_hash VARCHAR(255) NOT NULL,
full_name VARCHAR(255),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);"
docker exec -it postgres_main psql -U postgres -d products_db -c "
CREATE TABLE IF NOT EXISTS products (
id SERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
description TEXT,
price DECIMAL(10, 2) NOT NULL,
stock_quantity INTEGER NOT NULL DEFAULT 0,
category VARCHAR(100),
tags TEXT[],
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);"
docker exec -it postgres_main psql -U postgres -d orders_db -c "
CREATE TABLE IF NOT EXISTS orders (
id SERIAL PRIMARY KEY,
user_id INTEGER NOT NULL,
status VARCHAR(50) NOT NULL DEFAULT 'pending',
total_amount DECIMAL(10, 2) NOT NULL,
metadata JSONB DEFAULT '{}',
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS order_items (
id SERIAL PRIMARY KEY,
order_id INTEGER REFERENCES orders(id) ON DELETE CASCADE,
product_id INTEGER NOT NULL,
quantity INTEGER NOT NULL,
price DECIMAL(10, 2) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);"# Add sample products
docker exec -it postgres_main psql -U postgres -d products_db -c "
INSERT INTO products (name, description, price, stock_quantity, category, tags) VALUES
('Laptop Pro 15', 'High-performance laptop with 16GB RAM', 1299.99, 50, 'Electronics', ARRAY['laptop', 'computer', 'electronics']),
('Wireless Mouse', 'Ergonomic wireless mouse with USB receiver', 29.99, 200, 'Accessories', ARRAY['mouse', 'wireless', 'accessories']),
('Mechanical Keyboard', 'RGB mechanical keyboard with blue switches', 89.99, 100, 'Accessories', ARRAY['keyboard', 'mechanical', 'rgb']),
('USB-C Hub', '7-in-1 USB-C hub with HDMI and ethernet', 49.99, 150, 'Accessories', ARRAY['hub', 'usb-c', 'adapter']),
('Monitor 27', '4K IPS monitor with HDR support', 399.99, 75, 'Electronics', ARRAY['monitor', 'display', '4k'])
ON CONFLICT DO NOTHING;"| Service | Port | Endpoint |
|---|---|---|
| API Gateway | 8000 | http://localhost:8000 |
| User Service | 8001 | http://localhost:8001 |
| Product Service | 8002 | http://localhost:8002 |
| Order Service | 8003 | http://localhost:8003 |
| NGINX | 80 | http://localhost |
| PostgreSQL | 5432 | localhost:5432 |
curl http://localhost:8001/health # User Service
curl http://localhost:8002/health # Product Service
curl http://localhost:8003/health # Order Servicecurl -X POST http://localhost:8001/register \
-H "Content-Type: application/json" \
-d '{
"email": "john.doe@example.com",
"username": "johndoe",
"password": "securepassword123",
"full_name": "John Doe"
}'curl -X POST http://localhost:8001/login \
-H "Content-Type: application/json" \
-d '{
"email": "john.doe@example.com",
"password": "securepassword123"
}'curl -X GET http://localhost:8001/users/1curl -X GET "http://localhost:8001/users/search?q=john"curl -X POST http://localhost:8002/api/products \
-H "Content-Type: application/json" \
-d '{
"name": "Gaming Chair",
"description": "Ergonomic gaming chair with RGB lighting",
"price": 299.99,
"stock_quantity": 25,
"category": "Furniture",
"tags": ["gaming", "chair", "rgb", "ergonomic"]
}'curl -X GET http://localhost:8002/api/products/1curl -X GET "http://localhost:8002/api/products/search?q=laptop"curl -X GET "http://localhost:8002/api/products/tags?tags=laptop,computer"curl -X PATCH http://localhost:8002/api/products/1/stock \
-H "Content-Type: application/json" \
-d '{
"quantity": 45
}'curl -X POST http://localhost:8003/api/orders \
-H "Content-Type: application/json" \
-d '{
"user_id": 1,
"items": [
{
"product_id": 1,
"quantity": 2
},
{
"product_id": 2,
"quantity": 1
}
]
}'curl -X GET http://localhost:8003/api/orders/1curl -X GET http://localhost:8003/api/orders/user/1curl -X PATCH http://localhost:8003/api/orders/1/status \
-H "Content-Type: application/json" \
-d '{
"status": "shipped"
}'curl -X POST http://localhost:8000/api/users/register \
-H "Content-Type: application/json" \
-d '{
"email": "jane.smith@example.com",
"username": "janesmith",
"password": "mypassword456",
"full_name": "Jane Smith"
}'curl -X POST http://localhost:8000/api/products \
-H "Content-Type: application/json" \
-d '{
"name": "Wireless Headphones",
"description": "Noise-cancelling wireless headphones",
"price": 199.99,
"stock_quantity": 80,
"category": "Electronics",
"tags": ["headphones", "wireless", "audio"]
}'curl -X POST http://localhost:8000/api/orders \
-H "Content-Type: application/json" \
-d '{
"user_id": 1,
"items": [
{
"product_id": 3,
"quantity": 1
}
]
}'Run this sequence to test the entire system:
# 1. Register a user
curl -X POST http://localhost:8001/register \
-H "Content-Type: application/json" \
-d '{"email": "test@example.com", "username": "testuser", "password": "password123", "full_name": "Test User"}'
# 2. Create a product
curl -X POST http://localhost:8002/api/products \
-H "Content-Type: application/json" \
-d '{"name": "Test Product", "description": "A test product", "price": 99.99, "stock_quantity": 10, "category": "Test", "tags": ["test"]}'
# 3. Create an order
curl -X POST http://localhost:8003/api/orders \
-H "Content-Type: application/json" \
-d '{"user_id": 1, "items": [{"product_id": 1, "quantity": 2}]}'
# 4. Check the order
curl -X GET http://localhost:8003/api/orders/1
# 5. Update order status
curl -X PATCH http://localhost:8003/api/orders/1/status \
-H "Content-Type: application/json" \
-d '{"status": "processing"}'MicroService/
βββ api-getway/ # API Gateway service
β βββ api_getway.go
β βββ Dockerfile
β βββ go.mod
β βββ go.sum
βββ db/
β βββ migrations/ # Database migrations
β βββ 20251005121033_m1.sql
β βββ 20251005121034_users.sql
βββ nginx/ # NGINX configuration
β βββ nginx.conf
β βββ logs/
βββ order-service/ # Order management service
β βββ order_service.go
β βββ Dockerfile
β βββ go.mod
β βββ go.sum
βββ product-service/ # Product catalog service
β βββ product_service.go
β βββ Dockerfile
β βββ go.mod
β βββ go.sum
βββ user-service/ # User management service
β βββ main.go
β βββ Dockerfile
β βββ go.mod
β βββ go.sum
βββ docker-compose.yml # Docker composition
βββ init-databases.sh # Database initialization script
βββ README.md # This file
- User registration and authentication
- JWT token generation
- User profile management
- Password hashing with bcrypt
- Product catalog management
- Full-text search capabilities
- Tag-based filtering
- Stock management
- Order creation and management
- Order status tracking
- Integration with Product Service for pricing
- Order history by user
- Request routing and load balancing
- Centralized logging
- Rate limiting capabilities
- Service discovery
# Install dependencies for each service
cd user-service && go mod tidy
cd ../product-service && go mod tidy
cd ../order-service && go mod tidy
cd ../api-getway && go mod tidy
# Run services locally (requires PostgreSQL running)
cd user-service && go run main.go # Port 8001
cd product-service && go run *.go # Port 8002
cd order-service && go run *.go # Port 8003
cd api-getway && go run *.go # Port 8000# Connect to PostgreSQL
docker exec -it postgres_main psql -U postgres
# View databases
\l
# Connect to specific database
\c users_db
# View tables
\dt
# View table structure
\d users# Check container logs
docker-compose logs -f
# Rebuild containers
docker-compose down
docker-compose up --build -d# Check PostgreSQL health
docker exec postgres_main pg_isready -U postgres
# Restart database
docker-compose restart postgres# Check what's using ports
sudo netstat -tulpn | grep :8001
sudo netstat -tulpn | grep :5432
# Stop conflicting services
sudo systemctl stop postgresql # If local PostgreSQL is running# Reset migrations
dbmate rollback
dbmate up
# Check migration status
dbmate status# Check all service health
curl http://localhost:8001/health && echo ""
curl http://localhost:8002/health && echo ""
curl http://localhost:8003/health && echo ""Create a .env file for custom configuration:
# Database
POSTGRES_USER=postgres
POSTGRES_PASSWORD=postgres
POSTGRES_MULTIPLE_DATABASES=users_db,products_db,orders_db
# Services
USER_SERVICE_PORT=8001
PRODUCT_SERVICE_PORT=8002
ORDER_SERVICE_PORT=8003
API_GATEWAY_PORT=8000
# JWT Secret
JWT_SECRET=your-super-secret-jwt-key-change-in-production- Update environment variables
- Configure proper secrets management
- Set up SSL certificates
- Configure production database
- Set up monitoring and logging
# Build and tag images
docker build -t yourusername/user-service ./user-service
docker build -t yourusername/product-service ./product-service
docker build -t yourusername/order-service ./order-service
docker build -t yourusername/api-gateway ./api-getway
# Push to Docker Hub
docker push yourusername/user-service
docker push yourusername/product-service
docker push yourusername/order-service
docker push yourusername/api-gateway- 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.
Hariom Prajapati
- GitHub: @hariomop12
- Repository: MicroService
- Add authentication middleware to API Gateway
- Implement Redis caching for frequently accessed data
- Add Prometheus metrics and Grafana dashboards
- Implement event-driven architecture with message queues
- Add comprehensive unit and integration tests
- Set up CI/CD pipeline with GitHub Actions
- Add API versioning support
- Implement distributed tracing with Jaeger
- Add rate limiting and circuit breaker patterns
- Create Kubernetes deployment manifests
π Happy Coding! If you find this project helpful, please give it a βοΈ on GitHub!