A microservice-based inventory system built entirely in Go that demonstrates best practices for production-ready backend systems. This project showcases idiomatic Go, microservices architecture, API gateway patterns, and modern software engineering principles.
-
API Gateway (port 8000)
- Routes external requests to internal microservices
- Handles JWT authentication, rate limiting, logging, and metrics
- Provides unified entry point for all client requests
-
Auth Service (port 8083)
- Handles user registration and login
- Issues and validates JWT tokens
- Manages user authentication state
-
User Service (port 8081)
- Handles CRUD operations for user accounts
- Manages user profiles and preferences
- Provides user data management
-
Order Service (port 8082)
- Handles CRUD operations for product orders
- Manages order lifecycle and status
- Provides order tracking and history
-
Shared Module
- Common models and utility functions
- Shared configuration and database schemas
- Reusable components across services
- Go 1.21+ - Primary programming language
- SQLite - Lightweight database (can be replaced with PostgreSQL)
- GORM - Object-Relational Mapping
- JWT - JSON Web Tokens for authentication
- Gorilla Mux - HTTP router and URL matcher
- Prometheus - Metrics collection and monitoring
- Docker & Docker Compose - Containerization and orchestration
go-inventory-system/
├── gateway/ # API Gateway service
│ ├── config/ # Configuration loading
│ ├── middleware/ # Auth, rate-limit, logging, metrics
│ ├── router/ # Route registration
│ └── main.go # Entry point
│
├── services/
│ ├── auth/ # Auth microservice
│ │ ├── handler.go # Handlers for login/register
│ │ ├── db.go # Database initialization
│ │ └── main.go # Service entry point
│ │
│ ├── users/ # User microservice
│ │ ├── handler.go # CRUD + profile endpoints
│ │ ├── db.go # Database initialization
│ │ └── main.go # Service entry point
│ │
│ └── orders/ # Order microservice
│ ├── handler.go # Order endpoints
│ ├── db.go # Database initialization
│ └── main.go # Service entry point
│
├── shared/ # Common utilities
│ ├── models.go # Shared model types
│ ├── utils.go # Hashing, validation, etc.
│ └── config.go # Shared config structs
│
├── docker-compose.yml # Service orchestration
├── go.mod # Module definition
├── routes.yaml # Gateway routing config
└── README.md # Documentation
- Go 1.21 or higher
- Docker and Docker Compose (for containerized deployment)
-
Clone the repository
git clone <repository-url> cd go-inventory-system
-
Install dependencies
go mod tidy
-
Start individual services
# Start API Gateway go run gateway/main.go # Start Auth Service (in new terminal) go run services/auth/main.go # Start Users Service (in new terminal) go run services/users/main.go # Start Orders Service (in new terminal) go run services/orders/main.go
-
Build and start all services
docker-compose up --build
-
Access the API Gateway
- Gateway: http://localhost:8000
- Auth Service: http://localhost:8083
- Users Service: http://localhost:8081
- Orders Service: http://localhost:8082
POST /auth/register- Register a new userPOST /auth/login- Login user and get JWT token
GET /users- List all usersPOST /users- Create a new userGET /users/{id}- Get specific userPUT /users/{id}- Update userDELETE /users/{id}- Delete userGET /users/me- Get current user profile
GET /orders- List all ordersPOST /orders- Create a new orderGET /orders/{id}- Get specific orderPUT /orders/{id}- Update orderDELETE /orders/{id}- Delete orderGET /orders/user/{user_id}- Get orders for specific user
GET /health- Service health checkGET /metrics- Prometheus metrics
PORT- Service port (default: 8080)DATABASE_URL- Database connection stringJWT_SECRET- JWT signing secretENVIRONMENT- Environment (development/production)LOG_LEVEL- Logging level
Routes are configured in routes.yaml:
routes:
- path: /auth
backend: http://localhost:8083
methods: ["GET", "POST"]
- path: /users
backend: http://localhost:8081
methods: ["GET", "POST", "PUT", "DELETE"]
- path: /orders
backend: http://localhost:8082
methods: ["GET", "POST", "PUT", "DELETE"]-
Register a user
curl -X POST http://localhost:8000/auth/register \ -H "Content-Type: application/json" \ -d '{ "email": "user@example.com", "password": "password123", "username": "testuser" }'
-
Login and get token
curl -X POST http://localhost:8000/auth/login \ -H "Content-Type: application/json" \ -d '{ "email": "user@example.com", "password": "password123" }'
-
Access protected endpoint
curl -X GET http://localhost:8000/users/me \ -H "Authorization: Bearer <your-jwt-token>" -
Create an order
curl -X POST http://localhost:8000/orders \ -H "Content-Type: application/json" \ -H "Authorization: Bearer <your-jwt-token>" \ -d '{ "product_name": "Laptop", "quantity": 1, "total_price": 999.99 }'
- JWT Authentication - Stateless token-based authentication
- Password Hashing - bcrypt for secure password storage
- Rate Limiting - Prevents abuse with configurable limits
- CORS Support - Cross-origin resource sharing
- Input Validation - Request validation and sanitization
- Request Logging - Detailed request/response logging
- Prometheus Metrics - HTTP request counts and durations
- Health Checks - Service health monitoring
- Graceful Shutdown - Proper service termination
- Modular Design - Single responsibility principle
- Interface-driven - Loose coupling between components
- Error Handling - Comprehensive error management
- Configuration Management - Environment-based configuration
- Authentication Middleware - JWT validation
- Password Security - bcrypt hashing
- Input Validation - Request sanitization
- CORS Configuration - Cross-origin security
- Connection Pooling - Database connection management
- Rate Limiting - Request throttling
- Graceful Shutdown - Proper resource cleanup
- Metrics Collection - Performance monitoring
- Containerization - Docker for consistent environments
- Service Orchestration - Docker Compose for multi-service deployment
- Environment Configuration - Flexible configuration management
- Health Monitoring - Service health checks
- Microservices Architecture - Independent service scaling
- Database Optimization - Indexing and query optimization
- Load Balancing - Multiple service instances
- Caching - Redis for session and data caching
- Application Metrics - Custom business metrics
- Infrastructure Monitoring - System resource monitoring
- Log Aggregation - Centralized logging (ELK stack)
- Alerting - Proactive issue detection
- HTTPS/TLS - Encrypted communication
- API Rate Limiting - DDoS protection
- Input Sanitization - XSS and injection prevention
- Audit Logging - Security event tracking
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.