pandaparty is a backend system for a game, built using a microservice architecture in Go. It features services for login, game logic, room management, friend lists, and payments.
- Services Overview
- Architecture
- Configuration
- Getting Started
- Protobuf Generation
- API Documentation
- Project Structure (Brief)
The backend is composed of the following microservices:
loginserver:- Handles user authentication via HTTP/JSON API (endpoints:
/api/login,/api/validate_session). - Manages user sessions using Redis, with a default session expiry of 24 hours.
- Handles user authentication via HTTP/JSON API (endpoints:
gameserver:- Manages core player state (e.g., inventory, stats - future).
- Orchestrates game-related actions and communicates with other backend services (like
roomserver) via RPC.
roomserver:- Manages game rooms, including creation, joining, and game state (actual game logic to be implemented within rooms).
- Exposes an RPC interface for room operations (e.g.,
CreateRoom,JoinRoom).
friendserver:- Manages users' friend lists and relationships.
- Exposes an RPC interface (e.g.,
GetFriendsList).
payserver:- Handles payment information, transaction status, and potentially interacts with payment gateways.
- Exposes an RPC interface (e.g.,
GetPaymentStatus).
- HTTP/JSON: Used by
loginserverfor client-facing authentication and session validation. - RPC (Remote Procedure Call): Used for internal communication between microservices (e.g.,
gameservercallingroomserver). A custom TCP-based RPC framework with connection pooling is implemented ininfra/network/rpc.go.- Message Framing: The RPC framework uses a length-prefixed message framing protocol:
- Request:
TotalFrameLength (int32) | MethodNameLength (int32) | MethodName ([]byte) | PayloadLength (int32) | Payload ([]byte) - Response:
TotalFrameLength (int32) | ErrorLength (int32) | ErrorString ([]byte) | PayloadLength (int32) | Payload ([]byte)
- Request:
- Message Framing: The RPC framework uses a length-prefixed message framing protocol:
- Protobuf (Protocol Buffers): Used as the primary data serialization format for RPC messages and potentially for some data storage or NSQ messages.
- Consul: Services register themselves with Consul, and other services can discover their addresses and ports using Consul. This allows for dynamic scaling and resilience.
- MongoDB: The primary persistent data store for:
- Player profiles and game-related data (e.g., in
loginserver,gameserver). - Friendship information (in
friendserver). - Payment records (in
payserver). - Room state snapshots (potentially in
roomserver).
- Player profiles and game-related data (e.g., in
- Redis: Used for:
- Session management by
loginserver(session tokens and associated user IDs). - Caching (future).
- Leaderboards (future).
- Real-time features (future, e.g., presence).
- Session management by
- NSQ: Integrated for asynchronous task processing and communication where immediate responses are not required (e.g., broadcasting events, logging, delayed tasks). Each service that uses NSQ will typically have its own producer or consumer logic.
Service configurations are managed via YAML files, primarily config/server.yaml.
config/server_config.go: Defines the Go structs that map to the YAML configuration.config/server.yaml: Contains connection details for Redis, MongoDB, Consul, NSQ, and server-specific settings.servicerpcports: A key map under theserversection that defines the RPC listening ports for each microservice. For example:server: host: "localhost" servicerpcports: roomserver: 50052 friendserver: 50053 payserver: 50054
- The
loginserverHTTP port is typically configured viaserver.port.
Refer to the comments within config/server.yaml for more details on specific parameters.
- Go (version 1.18 or higher recommended)
- MongoDB instance running
- Redis instance running
- Consul agent running
- NSQD instance running (if using NSQ features)
- Protocol Buffer Compiler (
protoc) and Go plugins (protoc-gen-go) for protobuf generation.
Each service can be run from the root of the project using go run:
# To run the login server (HTTP):
go run cmd/loginserver/loginserver.go
# To run the gameserver:
go run cmd/gameserver/gameserver.go
# To run the roomserver (RPC):
go run cmd/roomserver/roomserver.go
# To run the friendserver (RPC):
go run cmd/friendserver/friendserver.go
# To run the payserver (RPC):
go run cmd/payserver/payserver.goEnsure that the dependent services (MongoDB, Redis, Consul, NSQ) are running and accessible as per the configurations in config/server.yaml.
Protocol Buffers are used for defining data structures for RPC messages and potentially other uses.
To generate Go code from .proto files:
- Ensure
protocandprotoc-gen-goare installed and in your PATH.protoc-gen-gocan be installed via:go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
- Run the script:
This script will find all
bash tools/genproto.sh
.protofiles ininfra/protocol/andinfra/model/and generate the corresponding.pb.gofiles ininfra/pb/.
For more detailed information on the original project structure that inspired parts of this setup, refer to docs/readme.md (Note: This link points to a document that might have been part of an initial template; adapt as necessary if this file doesn't exist or has different content in your current project structure).
Detailed API specifications:
- Login Server API: See docs/API_GUIDE.md for HTTP/JSON endpoints like
/api/loginand/api/validate_session. - Internal RPC APIs: Defined by
.protofiles ininfra/protocol/. Each service exposing an RPC interface will have its methods and message structures defined there. Refer to the GoDoc comments ininfra/network/rpc.gofor details on the RPC framework itself.
cmd/: Main applications for each microservice (e.g.,cmd/loginserver/loginserver.go).config/: Configuration files (server.yaml) and Go structs for parsing them.docs/: Project documentation, including this README and API guides.infra/: Shared infrastructure components:infra/consul/: Consul client for service discovery.infra/mongo/: MongoDB client utilities.infra/network/: Custom RPC framework (rpc.go).infra/nsq/: NSQ producer/consumer utilities.infra/pb/: Generated Protocol Buffer Go files.infra/protocol/:.protodefinitions for RPC messages.infra/model/:.protodefinitions for data models (e.g.,player.proto).infra/redis/: Redis client utilities.
internal/: Service-specific business logic and handlers:internal/<servicename>/: Contains implementation details for each service (e.g.,internal/loginserver/,internal/roomserver/).
tools/: Utility scripts (e.g.,genproto.shfor protobuf generation).pkg/: (Optional) Shared utility libraries that are not specific to this project's infrastructure. (Currently not extensively used).
#!/bin/bash
# apt install -y protobuf-compiler
# protoc --version # Ensure compiler version is 3+
# go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
#go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest
docker run --name nsqd -p 4150:4150 -p 4151:4151 nsqio/nsq /nsqd --broadcast-address=127.0.0.1 --lookupd-tcp-address=127.0.0.1:4160
go install github.com/favadi/protoc-go-inject-tag@latest
This README provides a high-level overview. For more details, consult the specific documentation files and code comments within each module.

