A microservices-based game backend system built with Go, featuring claw machine and player management services with real-time communication capabilities.
This project implements a distributed microservices architecture with the following components:
- API Service - REST API gateway (Port 8080)
- WebSocket Service - Real-time WebSocket connections (Port 8081)
- TCP Service - TCP socket connections (Port 8082)
- RPC Services - gRPC microservices:
- ClawMachine Service (Port 9091)
- Player Service (Port 9094)
- Infrastructure - MySQL database, etcd service discovery
- Microservices Architecture - Scalable, independent services
- Real-time Communication - WebSocket and TCP support
- Service Discovery - etcd-based service registration and discovery
- Database Integration - MySQL with GORM ORM
- Protocol Buffers - Efficient inter-service communication
- Docker Support - Containerized deployment
- Configuration Management - YAML-based configuration
- Go 1.24+
- Docker & Docker Compose
- MySQL 8.0+
- etcd (optional, for service discovery)
- Protocol Buffers compiler (protoc)
- FlatBuffers compiler (flatc)
- Clone the repository:
git clone https://github.com/Richard-inter/game.git
cd game- Install dependencies:
make deps- Start infrastructure:
make docker-up-infraStart all services individually:
# Start each service in separate terminals
make run-game # Game service (Port 9090)
make run-api # API service (Port 8080)
make run-websocket # WebSocket service (Port 8081)
make run-tcp # TCP service (Port 8082)
make run-clawmachine # ClawMachine RPC service (Port 9091)
make run-player # Player RPC service (Port 9094)Start all services with Docker Compose:
make docker-upBuild all services:
make buildBuild individual services:
make build-api
make build-websocket
make build-tcp
make build-clawmachine
make build-playergame/
βββ cmd/ # Service entry points
β βββ api-service/ # REST API gateway
β βββ websocket-service/ # WebSocket handler
β βββ tcp-service/ # TCP socket handler
β βββ rpc/ # gRPC microservices
β βββ rpc-clawmachine-service/
β βββ rpc-player-service/
βββ internal/ # Private application code
β βββ config/ # Configuration management
β βββ db/ # Database connections
β βββ discovery/ # Service discovery (etcd)
β βββ domain/ # Business logic models
β βββ repository/ # Data access layer
β βββ service/ # Business services
β βββ transport/ # Transport layer (HTTP, gRPC, WebSocket, TCP)
βββ pkg/ # Public library code
β βββ common/ # Common utilities
β βββ logger/ # Logging utilities
β βββ protocol/ # Protocol definitions (Proto, FlatBuffers)
βββ config/ # Configuration files
βββ scripts/ # Development scripts
Services use YAML configuration files in the config/ directory:
api-service.yaml- API service configurationwebsocket-service.yaml- WebSocket service configurationgame-service.yaml- Game service configurationrpc-clawmachine-service.yaml- ClawMachine RPC service configurationrpc-player-service.yaml- Player RPC service configuration
CONFIG_PATH- Path to configuration file (optional, defaults to service-specific config)
Generate protocol buffer files:
make protoGenerate FlatBuffer files:
make flatbuffersGenerate all code:
make generateRun tests:
make testRun tests with coverage:
make test-coverageLint code:
make lintLint and fix:
make lint-fixFormat code:
make fmtBuild development image:
make docker-build-devBuild production image:
make docker-buildStart services:
make docker-upStop services:
make docker-downView logs:
make docker-logsREST API gateway that handles HTTP requests and forwards them to appropriate microservices.
Handles real-time WebSocket connections for live game updates.
Manages TCP socket connections for low-latency communication.
gRPC service managing claw machine game logic and state.
gRPC service handling player data, authentication, and profiles.
The API service exposes REST endpoints for:
-
Player Management
GET /players- List playersPOST /players- Create playerGET /players/{id}- Get player detailsPUT /players/{id}- Update playerDELETE /players/{id}- Delete player
-
Claw Machine Management
GET /clawmachines- List claw machinesPOST /clawmachines- Create claw machineGET /clawmachines/{id}- Get claw machine detailsPUT /clawmachines/{id}- Update claw machineDELETE /clawmachines/{id}- Delete claw machine
The project uses MySQL 8.0 as the primary database. The database schema includes:
players- Player information and statisticsclaw_machines- Claw machine configuration and stategames- Game sessions and results
Database connection is managed through GORM ORM with connection pooling.
Services can be discovered using etcd. When service discovery is enabled:
- Services register themselves with etcd on startup
- Client services discover service endpoints dynamically
- Load balancing and health checking are handled automatically
To disable service discovery, set discovery.enabled: false in configuration.
The project includes unit tests for business logic and integration tests for API endpoints. Tests are located alongside the source code.
Structured logging using zap with high-performance JSON output:
- DEBUG
- INFO
- WARN
- ERROR
- FATAL
Logs include service name, version, request correlation IDs, and structured fields for better observability. The logger supports both structured logging with zap.SugaredLogger and typed logging with zap.Logger for optimal performance.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Run linting and tests
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- Port conflicts - Ensure ports 8080-8082 and 9090-9094 are available
- Database connection - Verify MySQL is running and credentials are correct
- Service discovery - Check etcd is accessible if enabled
- Docker issues - Ensure Docker daemon is running and ports are exposed
Each service exposes a health check endpoint:
GET /health- Service health status
View service logs for debugging:
# Docker logs
make docker-logs
# Individual service logs
docker-compose logs -f [service-name]For support and questions:
- Create an issue in the GitHub repository
- Check the documentation in the
docs/directory - Review configuration examples in
config/