High-performance, real-time crash game backend built with Go, Fiber, Redis, and PostgreSQL. The system is designed for thousands of concurrent players, provably fair gameplay, and production-grade reliability.
- Go 1.21+
- Redis 7+
- PostgreSQL 14+
- Docker & Docker Compose (optional but recommended)
-
Clone and configure
git clone https://github.com/nutcas3/aviator-fun.git cd aviator cp .env.example .env # Edit .env with your configuration if needed (defaults are fine for local)
-
Run with Docker (Recommended)
# This will start the Go app, PostgreSQL, and Redis make docker-run -
Run Locally
If you prefer not to use Docker for the Go app, you can run it directly. You'll still need PostgreSQL and Redis running.
# 1. Start database and cache (if not already running) # You can use Docker for this: # docker run -d --name postgres ... # docker run -d --name redis ... # 2. Run the application make run
-
Check Health
Once running, check the health endpoint:
curl http://localhost:3000/health
| Command | Description |
|---|---|
make all |
Build and test the project |
make build |
Compile the API binary |
make run |
Run the API directly (go run cmd/api/main.go) |
make docker-run |
Start the full stack (app, db, cache) via Docker |
make docker-down |
Stop all Docker containers |
make watch |
Live reload using air (auto-installs if missing) |
make test |
Run unit tests (skips integration tests) |
make test-all |
Run the full test suite, including integration tests |
make itest |
Run database integration tests only |
make migrate-up |
Apply all pending database migrations |
make migrate-down |
Roll back the last database migration |
make migrate-version |
Show the current migration version |
make migrate-create name=<name> |
Scaffold a new migration file |
make db-reset |
Convenience: down then up |
make clean |
Remove build artifacts |
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Load Balancer โ
โ (Nginx/HAProxy) โ
โโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโโดโโโโโโโโโโโโโฌโโโโโโโโโโโโโ
โ โ โ
โโโโโโโโโผโโโโโโโโโ โโโโโโโโโโโโผโโโโโโ โโโโผโโโโโโโโโโโ
โ Go Backend 1 โ โ Go Backend 2 โ โ Go Backend Nโ
โ (Fiber+WS) โ โ (Fiber+WS) โ โ (Fiber+WS) โ
โโโโโโโโโฌโโโโโโโโโ โโโโโโโโโโโโฌโโโโโโ โโโโฌโโโโโโโโโโโโ
โ โ โ
โโโโโโโโโโโโโโฌโโโโโโโโโโโโดโโโโโโโโโโโโโ
โ
โโโโโโโโโโโโโดโโโโโโโโโโโโโ
โ โ
โโโโโโผโโโโโโ โโโโโโโโผโโโโโโโ
โ Redis โ โ PostgreSQL โ
โ Cluster โ โ Primary โ
โ(Sentinel)โ โ + Replicas โ
โโโโโโโโโโโโ โโโโโโโโโโโโโโโ
- Real-time WebSocket engine with per-round broadcasts
- Provably fair algorithm using HMAC-SHA256
- Atomic bet processing backed by Redis
- Auto-cashout and manual cashout support
- High-concurrency safe game manager (goroutines + channels)
- Graceful shutdown & panic recovery middleware
- Structured logging and health endpoints
- Database migrations via
golang-migrate - Docker + Compose orchestration
- Rate limiting, CORS, connection pooling
GET /healthโ Database, cache, and game statusGET /api/v1/game/stateโ Current round statePOST /api/v1/game/betโ Place a betPOST /api/v1/game/cashoutโ Cash out a betGET /api/v1/user/:userId/balanceโ Fetch user balancePOST /api/v1/user/:userId/balanceโ Update balance (admin/testing)
Connect: ws://localhost:3000/ws?user_id=<id>
Client โ Server
place_betโ{ "type": "place_bet", "amount": 100, "auto_cashout": 2.5 }cashoutโ{ "type": "cashout", "bet_id": "BET-..." }ping
Server โ Client
initial_state,round_start,round_runningupdate(multiplier tick),crashbet_placed,cashout
- Server generates a secret
server_seedand publisheshash_commitment = SHA256(server_seed). - Players place bets while only the commitment is known.
- After the crash, the server reveals
server_seed. Players verify with:
HMAC-SHA256(server_seed, client_seed:nonce) โ crash_multiplier
Use POST /api/v1/game/verify to validate multipliers client-side.
The core Aviator backend architecture is robust and highly scalable. To support variations like JetX, Mines, and Plinko (as seen on platforms like Odibets), you primarily need to introduce new Game Manager modules and specific API logic.
The key to supporting multiple game types is defining a common interface for the game engine.
| Game Module | Core Responsibility |
|---|---|
| AviatorGame | Manages the rising multiplier and the crash point using the Provably Fair system. |
| MinesGame | Manages the grid state, handles tile clicks, and determines the location of the mines. |
| PlinkoGame | Determines the ball's landing slot and its resulting multiplier using the Provably Fair system. |
- Modification: Minimal. The existing
AviatorGamelogic handles the rising curve (round_runningandcrashevents). - Backend Task: Add a
GameTypefield to the database/Redis model (e.g.,type: 'aviator',type: 'jetx'). The core logic remains the same; the front-end handles the visual change (a jet instead of a plane).
- Game Logic: Needs a new
MinesGamemanager. - Setup: Generate a 5ร5 grid and secretly place a random number of "Mines" (e.g., 3, 5, or 7) using the Provably Fair seed to determine their positions.
- Betting: The
POST /api/v1/game/betendpoint must include the number of mines the player is playing with. - Action: Add a new API endpoint:
POST /api/v1/game/click_tile { bet_id: "...", tile_id: 12 }. The backend verifies the click:- If Safe: Update the player's potential payout.
- If Mine: Instantly crash the game for that user, and log the loss.
- Cashout: Add a cashout endpoint,
POST /api/v1/game/cashout, which finalizes the bet at the current potential payout.
- Game Logic: Needs a new
PlinkoGamemanager. - Action: Add a new API endpoint:
POST /api/v1/game/drop_ball { bet_id: "...", row_count: 8 }. Therow_countdetermines the risk. - Result: Use the Provably Fair seed to determine the final horizontal landing position (e.g., which slot the ball lands in) and the final multiplier instantly. There are no ongoing updatesโthe result is calculated immediately upon the "drop."
The base API must be extended to support the different interaction models:
| Endpoint | Description | Interaction Type |
|---|---|---|
POST /api/v1/mines/bet |
Place a bet and set the number of mines. | REST |
POST /api/v1/mines/click |
Reveal a tile (Win/Mine result). | REST |
POST /api/v1/mines/cashout |
Cash out the current accumulated win. | REST |
| Endpoint | Description | Interaction Type |
|---|---|---|
POST /api/v1/plinko/drop |
Place a bet and initiate the ball drop. Returns the final multiplier. | REST |
The core principle remains HMAC-SHA256, but the seed result is interpreted differently for each game:
| Game Type | Seed Interpretation |
|---|---|
| Aviator | The HMAC-SHA256 result (a hex string) is converted into a floating-point number representing the crash multiplier. |
| Mines | The HMAC-SHA256 result is used to generate a sequence of pseudo-random numbers, which are mapped to the Mine grid coordinates. |
| Plinko | The HMAC-SHA256 result is used to generate a sequence of random left/right movements for the ball as it falls through the pegs, determining the final landing slot. |
Your existing Makefile and Docker Compose setup are perfect for starting this expansion.
- Refactor: Create a
game_managerinterface in Go thatAviatorGame,MinesGame, andPlinkoGameall implement. - New Routes: Implement the new REST endpoints for Mines and Plinko (as listed above).
- New Logic: Build the
MinesGameandPlinkoGamelogic, focusing heavily on correctly translating the Provably Fair seed into the game outcome.
Would you like a detailed walkthrough of how to derive the Mines grid coordinates from the HMAC-SHA256 seed?
The platform supports multiple game categories, each with different mechanics and providers:
| Category | Provider | Mechanic | Description |
|---|---|---|---|
| Mines/Tile Games | Spribe, Turbo Games | Tile-Click | Players click tiles to reveal multipliers while avoiding hidden mines |
| Plinko | Spribe, Turbo Games | Peg-Board | Ball drops through pegs to land in multiplier slots |
| Dice/HiLo | Spribe, Turbo Games | Probability | Bet on number ranges with adjusted payouts |
| Virtuals | Elbet, Golden Race | Simulated Sports | Fast-paced graphical sports simulations |
| Category | Provider | Examples | Key Feature |
|---|---|---|---|
| Video Slots | Pragmatic Play, Booming | Gates of Olympus, Sweet Bonanza | Spinning reels, bonus rounds |
| Drops and Wins | Pragmatic Play | Network Promotions | Linked progressive jackpots |
| Category | Provider | Examples | Key Feature |
|---|---|---|---|
| Live Games | Evolution, Pragmatic | Roulette, Blackjack | Real dealers, live streaming |
| Game Shows | Evolution | Crazy Time, Dream Catcher | Interactive entertainment games |
The existing Go, Fiber, Redis, and PostgreSQL stack can be extended with a modular game engine system:
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Go Backend (API/WS) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Game Factory / Router โ <-- Routes requests by GameType
โโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโค
โ โโโโโโโโโโโโโโโโผโโโโโโโโโโโ โโโโโโโโโโโโโโโโผโโโโโโโโโโโ โโโโโโโโโโโโโโโโผโโโโโโโโโโโ
โ โ ๐ Aviator Engine (WS) โ โ โ๏ธ Mines Engine (REST) โ โ ๐ฒ Plinko/Dice (REST) โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโโโโโค โโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ โ - Real-Time Multiplier โ โ - Grid State Management โ โ - Instant Seed Mapping โ
โ โ - Per-Round Broadcast โ โ - Tile Click Logic โ โ - High-Roll Calculation โ
โ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโ โโโโโโโโโโโโโโโโโโโโโโโโโโโ
| Game Type | Key Backend Changes in Go | Provably Fair Logic Change |
|---|---|---|
| Mines | New REST Endpoints: POST /mines/bet, POST /mines/click, POST /mines/cashout. Uses Redis to store the current unrevealed grid state per user. |
The Provably Fair seed determines the exact position of all mines at the start of the round. The client verifies the full mine layout. |
| Plinko/Dice | Single REST Endpoint: POST /plinko/drop or POST /dice/roll. This must be a single, transactional operation. |
The Provably Fair seed is used to instantly map to the final landing slot (Plinko) or the final roll number (Dice). No real-time updates are needed. |
| Slots | Integration: Slots are typically provided by the vendors (e.g., Pragmatic Play). You build a seamless API Wrapper that authenticates the player and forwards the spin request to the vendor's external API, then handles the balance update. | The random number generation (RNG) is handled entirely by the vendor's certified server (no custom Provably Fair needed). |
The existing system's use of Redis for fast state management and PostgreSQL for immutable transaction logging is essential and remains the foundation for all these diverse game types.
-
Core Services (Existing):
- User authentication/authorization
- Wallet/balance management
- Transaction logging
- WebSocket infrastructure
-
Game Engine Layer (New):
GameEngineinterface for all game types- Common game lifecycle management
- Standardized event system
-
Game Modules (Modular):
- Each game type implements the
GameEngineinterface - Self-contained game logic and state management
- Game-specific API endpoints
- Each game type implements the
-
Vendor Integration (For Slots/Live):
- Unified API gateway for external providers
- Session management
- Bet resolution handling
-
Define the Game Contract:
- Game parameters and rules
- Betting model
- Payout structure
-
Implement Core Logic:
- Game state management
- Win/loss determination
- Integration with provably fair system
-
Create API Endpoints:
- Game-specific endpoints
- WebSocket events (if real-time)
- Admin controls
-
Integrate with Core Services:
- User authentication
- Balance updates
- Transaction logging
-
Add Monitoring:
- Game-specific metrics
- Error tracking
- Performance monitoring
This project has comprehensive test coverage for all major components.
# Run unit tests (fast, skips integration tests)
make test
# Run the full test suite (requires Docker to be running)
make test-all
# Run only database integration tests
make itest
# Generate a coverage report
go test ./... -coverprofile=coverage.out && go tool cover -html=coverage.out- Unit Tests: Provably fair logic, WebSocket hub, data structures, and handlers.
- Integration Tests: Database operations using
testcontainers(skipped by default). - Concurrency Tests: Thread-safe operations for broadcasting and client management.
- Performance Benchmarks: Key algorithms are benchmarked. Run with
go test ./internal/game -bench=. -benchmem.
- Checklist: Enable TLS, implement JWT auth, configure Redis auth, set CORS policies, and monitor system metrics.
- Scaling: The architecture supports horizontal scaling of the Go backend instances, Redis (via Sentinel/Cluster), and PostgreSQL (via read replicas).
| Issue | Resolution |
|---|---|
| Redis connection refused | Ensure Redis is running and REDIS_URL in .env is correct. |
| Migrations fail | Run make migrate-up and check the database connection string. |
| Integration tests hang | Run make test to skip them, or ensure Docker is running before make test-all. |
- Fork the repository
- Create a feature branch
- Submit a Pull Request with tests and documentation updates
Just a simple crash game backend by Nutcase