- 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

View File

@@ -1,59 +0,0 @@
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(())
}

View File

@@ -1,4 +1,3 @@
use crate::service_discovery::get_service_address;
use auth_service::auth::auth_service_server::AuthServiceServer;
use auth_service::database_client::DatabaseClient;
use auth_service::database_client::DatabaseClientTrait;
@@ -11,9 +10,8 @@ use tokio::{select, signal};
use tonic::transport::Server;
use tracing::{info, Level};
use warp::Filter;
mod consul_registration;
mod service_discovery;
use utils::consul_registration;
use utils::service_discovery::get_service_address;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@@ -27,13 +25,14 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set the gRPC server address
let addr = env::var("AUTH_SERVICE_ADDR").unwrap_or_else(|_| "127.0.0.1".to_string());
let port = env::var("AUTH_SERVICE_PORT").unwrap_or_else(|_| "50051".to_string());
let health_port = env::var("HEALTH_CHECK_PORT").unwrap_or_else(|_| "8081".to_string());
let consul_url = env::var("CONSUL_URL").unwrap_or_else(|_| "http://127.0.0.1:8500".to_string());
let service_name = env::var("SERVICE_NAME").unwrap_or_else(|_| "auth-service".to_string());
let service_address = addr.as_str();
let service_port = port.clone();
let health_check_url = format!("http://{}:{}/health", service_address, service_port);
let health_check_endpoint_addr = format!("{}:8081", service_address);
let health_check_url = format!("http://{}:{}/health", service_address, health_port);
let health_check_endpoint_addr = format!("{}:{}", service_address, health_port);
let db_address = get_service_address(&consul_url, "database-service").await?;
// Register service with Consul

View File

@@ -1,48 +0,0 @@
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(crate) ID: String,
pub(crate) Service: String,
pub(crate) Tags: Vec<String>,
pub(crate) Port: u16,
pub(crate) Address: String,
pub(crate) TaggedAddresses: TaggedAddresses,
pub(crate) Weights: Weights,
pub(crate) EnableTagOverride: bool,
pub(crate) ContentHash: String,
pub(crate) 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)
}