mirror of
https://github.com/geoffsee/predict-otron-9001.git
synced 2025-09-08 22:46:44 +00:00
cleanup, add ci
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
use tracing::info;
|
||||
use tracing::log::error;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ServerConfig {
|
||||
#[serde(default = "default_server_host")]
|
||||
@@ -10,14 +12,16 @@ pub struct ServerConfig {
|
||||
pub server_port: u16,
|
||||
pub server_mode: ServerMode,
|
||||
#[serde(default)]
|
||||
pub services: Services,
|
||||
pub services: Option<Services>,
|
||||
}
|
||||
|
||||
fn default_server_host() -> String {
|
||||
"127.0.0.1".to_string()
|
||||
}
|
||||
|
||||
fn default_server_port() -> u16 { 8080 }
|
||||
fn default_server_port() -> u16 {
|
||||
8080
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
|
||||
#[serde(rename_all = "PascalCase")]
|
||||
@@ -34,17 +38,15 @@ impl Default for ServerMode {
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub struct Services {
|
||||
#[serde(default = "inference_service_url")]
|
||||
pub inference_url: String,
|
||||
#[serde(default = "embeddings_service_url")]
|
||||
pub embeddings_url: String,
|
||||
pub inference_url: Option<String>,
|
||||
pub embeddings_url: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for Services {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
inference_url: inference_service_url(),
|
||||
embeddings_url: embeddings_service_url(),
|
||||
inference_url: None,
|
||||
embeddings_url: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -63,7 +65,7 @@ impl Default for ServerConfig {
|
||||
server_host: "127.0.0.1".to_string(),
|
||||
server_port: 8080,
|
||||
server_mode: ServerMode::Standalone,
|
||||
services: Services::default(),
|
||||
services: Some(Services::default()),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,21 +75,19 @@ impl ServerConfig {
|
||||
/// Falls back to default (Local mode) if not set or invalid
|
||||
pub fn from_env() -> Self {
|
||||
match env::var("SERVER_CONFIG") {
|
||||
Ok(config_str) => {
|
||||
match serde_json::from_str::<ServerConfig>(&config_str) {
|
||||
Ok(config) => {
|
||||
tracing::info!("Loaded server configuration: {:?}", config);
|
||||
config
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
"Failed to parse SERVER_CONFIG environment variable: {}. Using default configuration.",
|
||||
e
|
||||
);
|
||||
ServerConfig::default()
|
||||
}
|
||||
Ok(config_str) => match serde_json::from_str::<ServerConfig>(&config_str) {
|
||||
Ok(config) => {
|
||||
tracing::info!("Loaded server configuration: {:?}", config);
|
||||
config
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::warn!(
|
||||
"Failed to parse SERVER_CONFIG environment variable: {}. Using default configuration.",
|
||||
e
|
||||
);
|
||||
ServerConfig::default()
|
||||
}
|
||||
},
|
||||
Err(_) => {
|
||||
tracing::info!("SERVER_CONFIG not set, Standalone mode active");
|
||||
ServerConfig::default()
|
||||
@@ -96,18 +96,52 @@ impl ServerConfig {
|
||||
}
|
||||
|
||||
/// Check if the server should run in high availability mode
|
||||
pub fn is_high_availability(&self) -> bool {
|
||||
self.server_mode == ServerMode::HighAvailability
|
||||
pub fn is_high_availability(&self) -> Result<bool, std::io::Error> {
|
||||
if self.server_mode == ServerMode::HighAvailability {
|
||||
let services_well_defined: bool = self.clone().services.is_some();
|
||||
|
||||
let inference_url_well_defined: bool =
|
||||
services_well_defined && self.clone().services.unwrap().inference_url.is_some();
|
||||
|
||||
let embeddings_well_defined: bool =
|
||||
services_well_defined && self.clone().services.unwrap().embeddings_url.is_some();
|
||||
|
||||
let is_well_defined_for_ha =
|
||||
services_well_defined && inference_url_well_defined && embeddings_well_defined;
|
||||
|
||||
if !is_well_defined_for_ha {
|
||||
let config_string = serde_json::to_string_pretty(&self).unwrap();
|
||||
error!(
|
||||
"HighAvailability mode configured but services not well defined! \n## Config Used:\n {}",
|
||||
config_string
|
||||
);
|
||||
let err = std::io::Error::new(
|
||||
std::io::ErrorKind::Other,
|
||||
"HighAvailability mode configured but services not well defined!",
|
||||
);
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(self.server_mode == ServerMode::HighAvailability)
|
||||
}
|
||||
|
||||
/// Get the inference service URL for proxying
|
||||
pub fn inference_url(&self) -> &str {
|
||||
&self.services.inference_url
|
||||
pub fn inference_url(&self) -> Option<String> {
|
||||
if self.services.is_some() {
|
||||
self.services.clone()?.inference_url
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the embeddings service URL for proxying
|
||||
pub fn embeddings_url(&self) -> &str {
|
||||
&self.services.embeddings_url
|
||||
pub fn embeddings_url(&self) -> Option<String> {
|
||||
if self.services.is_some() {
|
||||
self.services.clone()?.embeddings_url
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,7 +153,7 @@ mod tests {
|
||||
fn test_default_config() {
|
||||
let config = ServerConfig::default();
|
||||
assert_eq!(config.server_mode, ServerMode::Standalone);
|
||||
assert!(!config.is_high_availability());
|
||||
assert!(!config.is_high_availability().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -134,23 +168,26 @@ mod tests {
|
||||
|
||||
let config: ServerConfig = serde_json::from_str(config_json).unwrap();
|
||||
assert_eq!(config.server_mode, ServerMode::HighAvailability);
|
||||
assert!(config.is_high_availability());
|
||||
assert_eq!(config.inference_url(), "http://inference-service:8080");
|
||||
assert_eq!(config.embeddings_url(), "http://embeddings-service:8080");
|
||||
assert!(config.is_high_availability().unwrap());
|
||||
assert_eq!(
|
||||
config.inference_url().unwrap(),
|
||||
"http://inference-service:8080"
|
||||
);
|
||||
assert_eq!(
|
||||
config.embeddings_url().unwrap(),
|
||||
"http://embeddings-service:8080"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_local_mode_config() {
|
||||
let config_json = r#"{
|
||||
"serverMode": "Local"
|
||||
"serverMode": "Standalone"
|
||||
}"#;
|
||||
|
||||
let config: ServerConfig = serde_json::from_str(config_json).unwrap();
|
||||
assert_eq!(config.server_mode, ServerMode::Standalone);
|
||||
assert!(!config.is_high_availability());
|
||||
// Should use default URLs
|
||||
assert_eq!(config.inference_url(), "http://inference-service:8080");
|
||||
assert_eq!(config.embeddings_url(), "http://embeddings-service:8080");
|
||||
assert!(!config.is_high_availability().unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -164,17 +201,26 @@ mod tests {
|
||||
}"#;
|
||||
|
||||
let config: ServerConfig = serde_json::from_str(config_json).unwrap();
|
||||
assert_eq!(config.inference_url(), "http://custom-inference:9000");
|
||||
assert_eq!(config.embeddings_url(), "http://custom-embeddings:9001");
|
||||
assert_eq!(
|
||||
config.inference_url().unwrap(),
|
||||
"http://custom-inference:9000"
|
||||
);
|
||||
assert_eq!(
|
||||
config.embeddings_url().unwrap(),
|
||||
"http://custom-embeddings:9001"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_minimal_high_availability_config() {
|
||||
fn test_minimal_high_availability_config_error() {
|
||||
let config_json = r#"{"serverMode": "HighAvailability"}"#;
|
||||
let config: ServerConfig = serde_json::from_str(config_json).unwrap();
|
||||
assert!(config.is_high_availability());
|
||||
// Should use default URLs
|
||||
assert_eq!(config.inference_url(), "http://inference-service:8080");
|
||||
assert_eq!(config.embeddings_url(), "http://embeddings-service:8080");
|
||||
|
||||
let is_high_availability = config.is_high_availability();
|
||||
|
||||
assert!(is_high_availability.is_err());
|
||||
// // Should use default URLs
|
||||
// assert_eq!(config.inference_url().unwrap(), "http://inference-service:8080");
|
||||
// assert_eq!(config.embeddings_url().unwrap(), "http://embeddings-service:8080");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user