- add: utils library

- add: packet-service to handle game client packets
- fix: health check for database-service
- fix: health check for auth-service
This commit is contained in:
2024-12-09 23:10:26 -05:00
parent 20e86dd117
commit e5c961d1b4
25 changed files with 1176 additions and 120 deletions

9
utils/Cargo.toml Normal file
View File

@@ -0,0 +1,9 @@
[package]
name = "utils"
version = "0.1.0"
edition = "2021"
[dependencies]
serde = { version = "1.0", features = ["derive"] }
reqwest = { version = "0.12.9", features = ["json"] }
tracing = "0.1"

View File

@@ -0,0 +1,59 @@
use reqwest::Client;
use serde::Serialize;
#[derive(Serialize)]
struct ConsulRegistration {
name: String,
address: String,
port: u16,
check: ConsulCheck,
}
#[derive(Serialize)]
struct ConsulCheck {
http: String,
interval: String,
}
pub async fn register_service(
consul_url: &str,
service_name: &str,
service_address: &str,
service_port: u16,
health_check_url: &str,
) -> Result<(), Box<dyn std::error::Error>> {
let registration = ConsulRegistration {
name: service_name.to_string(),
address: service_address.to_string(),
port: service_port,
check: ConsulCheck {
http: health_check_url.to_string(),
interval: "10s".to_string(), // Health check interval
},
};
let client = Client::new();
let consul_register_url = format!("{}/v1/agent/service/register", consul_url);
client
.put(&consul_register_url)
.json(&registration)
.send()
.await?
.error_for_status()?; // Ensure response is successful
Ok(())
}
pub async fn deregister_service(consul_url: &str, service_name: &str) -> Result<(), Box<dyn std::error::Error>> {
let client = Client::new();
let consul_deregister_url = format!("{}/v1/agent/service/deregister/{}", consul_url, service_name);
client
.put(&consul_deregister_url)
.send()
.await?
.error_for_status()?; // Ensure response is successful
Ok(())
}

2
utils/src/lib.rs Normal file
View File

@@ -0,0 +1,2 @@
pub mod consul_registration;
pub mod service_discovery;

View File

@@ -0,0 +1,48 @@
use serde::Deserialize;
#[derive(Deserialize)]
struct Address {
Address: String,
Port: u16,
}
#[derive(Deserialize)]
struct TaggedAddresses {
lan_ipv4: Address,
wan_ipv4: Address,
}
#[derive(Deserialize)]
struct Weights {
Passing: u8,
Warning: u8,
}
#[derive(Deserialize)]
pub struct Service {
pub ID: String,
pub Service: String,
pub Tags: Vec<String>,
pub Port: u16,
pub Address: String,
pub TaggedAddresses: TaggedAddresses,
pub Weights: Weights,
pub EnableTagOverride: bool,
pub ContentHash: String,
pub Datacenter: String,
}
pub async fn get_service_address(consul_url: &str, service_name: &str) -> Result<(Service), Box<dyn std::error::Error>> {
let client = reqwest::Client::new();
let consul_service_url = format!("{}/v1/agent/service/{}", consul_url, service_name);
let response = client
.get(&consul_service_url)
.send()
.await?
.error_for_status()?
.json::<Service>()
.await?; // Ensure response is successful
Ok(response)
}