Add comprehensive documentation and unit tests

Documentation:
- Add detailed README files for all services (auth, character, database, launcher, packet, utils, world)
- Create API documentation for the database service with detailed endpoint specifications
- Document database schema and relationships
- Add service architecture overviews and configuration instructions

Unit Tests:
- Implement comprehensive test suite for database repositories (user, character, session)
- Add gRPC service tests for database interactions
- Create tests for packet service components (bufferpool, connection, packets)
- Add utility service tests (health check, logging, load balancer, redis cache, service discovery)
- Implement auth service user tests
- Add character service tests

Code Structure:
- Reorganize test files into a more consistent structure
- Create a dedicated tests crate for integration testing
- Add test helpers and mock implementations for easier testing
This commit is contained in:
2025-04-09 13:29:38 -04:00
parent d47d5f44b1
commit a8755bd3de
85 changed files with 4218 additions and 764 deletions

View File

@@ -12,7 +12,6 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3.19", features = ["env-filter", "chrono"] }
tonic = "0.12.3"
prost = "0.13.4"
warp = "0.3.7"
async-trait = "0.1.83"
serde_json = "1.0.133"
tonic-health = "0.12.3"

View File

@@ -0,0 +1,93 @@
# Character Service
The Character Service manages character creation, deletion, and retrieval in the MMORPG server architecture.
## Overview
The Character Service provides gRPC endpoints for:
- Retrieving character lists for users
- Creating new characters
- Deleting characters
- Retrieving detailed character information
It communicates with the database service to store and retrieve character data.
## Architecture
The service is built using the following components:
- **gRPC Server**: Exposes character management endpoints
- **Character DB Client**: Communicates with the database service for character data
## Service Endpoints
The Character Service exposes the following gRPC endpoints:
### GetCharacterList
Retrieves a list of characters for a user.
```protobuf
rpc GetCharacterList(GetCharacterListRequest) returns (GetCharacterListResponse);
```
### CreateCharacter
Creates a new character for a user.
```protobuf
rpc CreateCharacter(CreateCharacterRequest) returns (CreateCharacterResponse);
```
### DeleteCharacter
Marks a character for deletion or permanently deletes it.
```protobuf
rpc DeleteCharacter(DeleteCharacterRequest) returns (DeleteCharacterResponse);
```
### GetCharacter
Retrieves detailed information about a specific character.
```protobuf
rpc GetCharacter(GetCharacterRequest) returns (GetCharacterResponse);
```
## Character Data Structure
Characters in the system have the following key attributes:
- **Basic Information**: ID, name, user ID, creation/deletion dates
- **Appearance**: Race, face, hair, stone
- **Stats**: Level, attributes (STR, DEX, INT, etc.), HP, MP, experience
- **Inventory**: Items, equipment
- **Position**: Map ID, coordinates
## Configuration
The service can be configured using environment variables:
- `LISTEN_ADDR`: The address to listen on (default: "0.0.0.0")
- `SERVICE_PORT`: The port to listen on (default: "50053")
- `LOG_LEVEL`: Logging level (default: "info")
## Running the Service
### Local Development
```bash
cargo run
```
### Docker
```bash
docker build -t character-service .
docker run -p 50053:50053 character-service
```
## Integration with External Systems
The Character Service integrates with:
- **Database Service**: For character data storage and retrieval
- **Auth Service**: For user authentication and authorization
- **Packet Service**: For handling client requests related to characters

View File

@@ -5,10 +5,7 @@ fn main() {
.compile_well_known_types(true)
.type_attribute(".", "#[derive(serde::Serialize, serde::Deserialize)]")
.compile_protos(
&[
"../proto/character_common.proto",
"../proto/character.proto",
],
&["../proto/character_common.proto", "../proto/character.proto"],
&["../proto"],
)
.unwrap_or_else(|e| panic!("Failed to compile protos {:?}", e));

View File

@@ -1,9 +1,8 @@
use crate::database::character_db_service_client::CharacterDbServiceClient;
use crate::database::{
Character, CharacterListRequest, CharacterListResponse, CharacterRequest,
CreateCharacterRequest, CreateCharacterResponse, DeleteCharacterRequest,
DeleteCharacterResponse,
character_db_service_client::CharacterDbServiceClient, Character, CharacterListRequest, CharacterListResponse,
CharacterRequest, CreateCharacterRequest, CreateCharacterResponse, DeleteCharacterRequest, DeleteCharacterResponse,
};
use serde::{Deserialize, Serialize};
use tonic::transport::Channel;

View File

@@ -1,9 +1,8 @@
use crate::character_db_client::CharacterDbClient;
use crate::character_service::character::character_service_server::CharacterService;
use crate::character_service::character::{
CreateCharacterRequest, CreateCharacterResponse, DeleteCharacterRequest,
DeleteCharacterResponse, GetCharacterListRequest, GetCharacterListResponse,
GetCharacterRequest, GetCharacterResponse,
CreateCharacterRequest, CreateCharacterResponse, DeleteCharacterRequest, DeleteCharacterResponse,
GetCharacterListRequest, GetCharacterListResponse, GetCharacterRequest, GetCharacterResponse,
};
use crate::character_service::character_common::{Character, CharacterFull};
use std::sync::Arc;
@@ -69,14 +68,7 @@ impl CharacterService for MyCharacterService {
.character_db_client
.as_ref()
.clone()
.create_character(
&req.user_id,
&req.name,
req.race,
req.face,
req.hair,
req.stone,
)
.create_character(&req.user_id, &req.name, req.race, req.face, req.hair, req.stone)
.await
.map_err(|_| Status::aborted("Unable to create character"))?;

View File

@@ -0,0 +1,5 @@
// Include the generated code
tonic::include_proto!("character_db_api");
// Re-export the types we need
pub use character_db_service_client::CharacterDbServiceClient;

View File

@@ -0,0 +1,3 @@
pub mod character_db_client;
pub mod character_service;
pub mod database;

View File

@@ -14,7 +14,7 @@ use std::sync::Arc;
use tracing::Level;
use tracing_subscriber::EnvFilter;
use utils::logging;
use utils::service_discovery::{get_kube_service_endpoints_by_dns};
use utils::service_discovery::get_kube_service_endpoints_by_dns;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -25,17 +25,22 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set the gRPC server address
let addr = env::var("LISTEN_ADDR").unwrap_or_else(|_| "0.0.0.0".to_string());
let port = env::var("SERVICE_PORT").unwrap_or_else(|_| "50053".to_string());
let db_url = format!("http://{}",get_kube_service_endpoints_by_dns("database-service","tcp","database-service").await?.get(0).unwrap());
let db_url = format!(
"http://{}",
get_kube_service_endpoints_by_dns("database-service", "tcp", "database-service")
.await?
.get(0)
.unwrap()
);
let full_addr = format!("{}:{}", &addr, port);
let address = full_addr.parse().expect("Invalid address");
let character_db_client = Arc::new(CharacterDbClient::connect(&db_url).await?);
let character_service = MyCharacterService {
character_db_client,
};
let character_service = MyCharacterService { character_db_client };
let (mut health_reporter, health_service) = tonic_health::server::health_reporter();
health_reporter.set_serving::<CharacterServiceServer<MyCharacterService>>().await;
health_reporter
.set_serving::<CharacterServiceServer<MyCharacterService>>()
.await;
tonic::transport::Server::builder()
.add_service(health_service)