Social Multiplayer Game Backend (Go)
Overview
- Go 1.22+, MySQL 8, Redis 7
- DDD + Clean Architecture
- Features: Auth, Groups, Battles (goroutines/channels), Social (gifts/help + cooldown), Leaderboard (Redis ZSET), Bot webhook
- Swagger via swaggo at
/swagger/index.html
Quick Start
- Configure app using
config.yaml(see file at repo root). ENV can override any key; setCONFIG_FILEto use a different path. - Bring up stack:
- docker compose up --build
- Seed a user (example):
- Connect to MySQL and create a user with bcrypt password hash.
MySQL seed example
INSERT INTO users (email, username, password_hash)
VALUES (
'alice@example.com',
'alice',
'$2a$10$uXg5HkqE2z0bD2WvZqvSlOHTlD4pVk6lYy1x4XH5Pp0W2oXk1E0wK' -- bcrypt for "password"
);Swagger
- Generate docs from annotations (requires swag installed):
- go install github.com/swaggo/swag/cmd/swag@latest
- swag init -g cmd/api/main.go -o docs
- Open: http://localhost:8080/swagger/index.html
API Endpoints (v1)
- POST /v1/auth/login
- POST /v1/groups
- POST /v1/groups/{id}/join
- GET /v1/groups/{id}
- POST /v1/battles
- POST /v1/battles/{id}/actions
- GET /v1/battles/{id}
- POST /v1/social/gifts
- POST /v1/social/help
- GET /v1/leaderboard/global?season=2025Q3&limit=50
- POST /v1/bot/webhook
Curl Examples
# Login (rate limited 5/min/IP)
curl -s -X POST http://localhost:8080/v1/auth/login \
-H 'Content-Type: application/json' \
-d '{"email":"alice@example.com","password":"password"}'
# Create group
TOKEN=... # from login
curl -s -X POST http://localhost:8080/v1/groups \
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
-d '{"name":"Alpha"}'
# Join group
curl -s -X POST http://localhost:8080/v1/groups/1/join \
-H "Authorization: Bearer $TOKEN"
# Start battle
curl -s -X POST http://localhost:8080/v1/battles \
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
-d '{"groupAId":1, "groupBId":2}'
# Submit action
curl -s -X POST http://localhost:8080/v1/battles/<id>/actions \
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
-d '{"type":"attack","payload":{"side":"A"}}'
# Get battle state
curl -s http://localhost:8080/v1/battles/<id> -H "Authorization: Bearer $TOKEN"
# Social gift
curl -s -X POST http://localhost:8080/v1/social/gifts \
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
-d '{"toUser":2, "item":"coin"}'
# Leaderboard
curl -s 'http://localhost:8080/v1/leaderboard/global?season=2025Q3&limit=5'
# Bot webhook
curl -s -X POST http://localhost:8080/v1/bot/webhook \
-H "Authorization: Bearer $TOKEN" -H 'Content-Type: application/json' \
-d '{"text":"/rank 2025Q3"}'Project Structure
- cmd/api: main entrypoint
- internal/domain: entities + interfaces
- internal/application: use-cases/services
- internal/infra/db: MySQL, Redis
- internal/infra/httpserver: Swagger docs, logger, handlers, middleware
- internal/router.go: API routes wiring
- pkg: config (YAML loader), dto, utils
- migrations: SQL migrations
- tests: unit tests (testify)
Directory Tree (format seperti di Explorer)
Sand-ai
├─ cmd/
│ └─ api/
│ └─ main.go
├─ internal/
│ ├─ application/
│ │ ├─ auth/
│ │ │ └─ service.go
│ │ ├─ battle/
│ │ │ └─ service.go
│ │ ├─ group/
│ │ │ └─ service.go
│ │ ├─ leaderboard/
│ │ │ └─ service.go
│ │ └─ social/
│ │ └─ service.go
│ ├─ domain/
│ │ ├─ battle/
│ │ │ └─ battle.go
│ │ ├─ group/
│ │ │ └─ group.go
│ │ ├─ leaderboard/
│ │ │ └─ leaderboard.go
│ │ ├─ social/
│ │ │ └─ social.go
│ │ └─ user/
│ │ └─ user.go
│ ├─ infra/
│ │ ├─ db/
│ │ │ ├─ mysql/
│ │ │ │ ├─ db.go
│ │ │ │ ├─ group_repo.go
│ │ │ │ └─ user_repo.go
│ │ │ └─ redis/
│ │ │ └─ client.go
│ │ └─ httpserver/
│ │ ├─ docs.go
│ │ ├─ logger.go
│ │ ├─ handlers/
│ │ │ ├─ auth_handler.go
│ │ │ ├─ battle_handler.go
│ │ │ ├─ bot_handler.go
│ │ │ ├─ group_handler.go
│ │ │ ├─ leaderboard_handler.go
│ │ │ └─ social_handler.go
│ │ └─ middleware/
│ │ ├─ auth.go
│ │ ├─ ratelimit.go
│ │ └─ response.go
│ └─ router.go
├─ pkg/
│ ├─ config/
│ │ └─ config.go
│ ├─ dto/
│ │ ├─ auth.go
│ │ ├─ battle.go
│ │ ├─ group.go
│ │ ├─ leaderboard.go
│ │ └─ social.go
│ └─ utils/
│ └─ crypto.go
├─ config.yaml
├─ migrations/
│ └─ 001_init.sql
├─ tests/
│ ├─ battle_manager_test.go
│ └─ utils_test.go
├─ docs/
│ └─ README.md
├─ Dockerfile
├─ docker-compose.yml
├─ .env.example
├─ go.mod
└─ README.md
Notes
- Sessions: Redis
session:{token}-> userId, TTL 24h - Rate limit: Redis fixed-window, login limited to 5/min/IP
- Battle loop: 200ms tick, goroutines + channels per battle
- Leaderboard: Redis ZSET
lb:{season}; periodic snapshot to MySQL can be added as a background job
Configuration
- Default config file:
config.yaml - Change path with env
CONFIG_FILE. - Environment variables override YAML when set (e.g.,
APP_PORT,MYSQL_HOST,REDIS_ADDR, etc.).