From 6805c7ac943a6d02ea21f09d71827d7f56f1cbb3 Mon Sep 17 00:00:00 2001 From: prplV Date: Tue, 24 Jun 2025 06:57:37 -0400 Subject: [PATCH] config + not implemented endpoints --- .env.example | 40 +++++++++++++++++++++++++---------- Cargo.toml | 3 ++- src/endpoints.rs | 21 +++++++++++++++--- src/main.rs | 40 +++++++++++++++++++++++++++++++++-- src/models.rs | 0 src/schemas.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ src/setup.rs | 45 +++++++++++++++++++++++++++++++++++++++ 7 files changed, 187 insertions(+), 17 deletions(-) create mode 100644 src/models.rs create mode 100644 src/setup.rs diff --git a/.env.example b/.env.example index 3833473..a031e10 100644 --- a/.env.example +++ b/.env.example @@ -7,14 +7,32 @@ # ------------------------------------------------------------------- ML_TARGET_URL="http://url.to/ml/api" -# `ML_API` log level selecetion (default - `INFO`) -# ----------------------------------------------- -# Existing options: -# 1) TRACE - full log info -# 2) DEBUG -# 3) INFO - common log info -# 4) WARN -# 5) ERROR -# 6) OFF - disabled logs -# ----------------------------------------------- -ML_LOG_LEVEL="INFO" +# `ML_MODEL_NAME` +ML_MODEL_NAME="kis-test" + +# `ML_REQUEST_TIMEOUT` for setting ML response wait time +# ------------------------------------------------------ +# - Measures in `secs` +# - Default value - 10 +# ------------------------------------------------------ +ML_REQUEST_TIMEOUT="10" + + +# `ML_API_LOG_LEVEL` log level selection +# -------------------------------------- +# - Default value - `INFO` +# - Existing options: +# 1) TRACE - full log info +# 2) DEBUG +# 3) INFO - common log info +# 4) WARN +# 5) ERROR +# 6) OFF - disabled logs +# -------------------------------------- +ML_API_LOG_LEVEL="INFO" + +# `ML_API_PORT` for setting up virt port number usage +# --------------------------------------------------- +# - Deault value - 5143 +# --------------------------------------------------- +ML_API_PORT="5134" \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index fd305d5..102879a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,9 @@ edition = "2024" anyhow = "1.0.98" axum = { version = "0.8.4", features = ["ws"] } dotenv = "0.15.0" +futures = "0.3.31" lazy_static = "1.5.0" -reqwest = "0.12.20" +reqwest = { version = "0.12.20", features = ["rustls-tls"] } serde = { version = "1.0.219", features = ["derive"] } serde_json = "1.0.140" tokio = { version = "1.45.1", features = ["full"] } diff --git a/src/endpoints.rs b/src/endpoints.rs index 2e84c49..144e600 100644 --- a/src/endpoints.rs +++ b/src/endpoints.rs @@ -1,5 +1,20 @@ -pub mod openapi {} +use axum::response::IntoResponse; +use axum::http::StatusCode; -pub mod ws {} +pub mod openapi { + use super::{IntoResponse, StatusCode}; -pub mod rest {} \ No newline at end of file + pub async fn swagger() -> impl IntoResponse { (StatusCode::NOT_IMPLEMENTED, "still in development ...") } +} + +pub mod rest { + use super::{IntoResponse, StatusCode}; + + pub async fn model_rest_handler() -> impl IntoResponse { (StatusCode::NOT_IMPLEMENTED, "still in development ...") } +} + +pub mod ws { + use super::{IntoResponse, StatusCode}; + + pub async fn model_ws_handler() -> impl IntoResponse { (StatusCode::NOT_IMPLEMENTED, "still in development ...") } +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index a591904..de198b4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,7 +1,43 @@ mod endpoints; mod schemas; +mod models; +mod setup; + +use std::sync::Arc; +use tracing::info; +use dotenv::dotenv; +use endpoints::{ + openapi::swagger, + rest::model_rest_handler, + ws::model_ws_handler, +}; +use setup::setup_self_config; +use schemas::ApiSessionConfig; +use axum::{ + routing::get, + Router}; #[tokio::main(flavor = "multi_thread")] -async fn main() { - println!("Hello, world!"); +async fn main() -> anyhow::Result<()> { + dotenv().ok(); + let port = setup_self_config().await; + let app_state = Arc::new(ApiSessionConfig::from_env()?); + let base_url = format!("0.0.0.0:{}", port); + + // routing + let api = Router::new() + .route("/rest", get(model_rest_handler)) + .route("/ws", get(model_ws_handler)); + + let router = Router::new() + .nest("/api", api) + .route("/swagger", get(swagger)) + .with_state(app_state.clone()); + + // running + info!("serving on {} ...", &base_url); + let listener = tokio::net::TcpListener::bind(&base_url).await?; + + axum::serve(listener, router).await?; + Ok(()) } diff --git a/src/models.rs b/src/models.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/schemas.rs b/src/schemas.rs index e69de29..d59a695 100644 --- a/src/schemas.rs +++ b/src/schemas.rs @@ -0,0 +1,55 @@ +use tracing::{debug, instrument, warn, info}; +use std::fmt; + +/// +#[derive(Debug)] +pub struct ApiSessionConfig { + // integration config + target_url : String, + model_name : String, + request_timeout : usize, +} + +impl fmt::Display for ApiSessionConfig { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { + let param_name_width = 25; + let param_name_val = (self.model_name.as_bytes().iter().count() + 4).max(self.target_url.as_bytes().iter().count() + 4); + let param_name_measure = 10; + + // header + writeln!(f, "+{:- anyhow::Result { + let config = Self { + target_url : std::env::var("ML_TARGET_URL")?, + model_name : std::env::var("ML_MODEL_NAME")?, + request_timeout : std::env::var("ML_REQUEST_TIMEOUT")?.parse().unwrap_or_else(|_| { + warn!("invalid port was given, setting up default one ..."); + 10 + }), + }; + debug!("{:?}", config); + info!("app is configurated. config :\n{}", config); + Ok(config) + } +} \ No newline at end of file diff --git a/src/setup.rs b/src/setup.rs new file mode 100644 index 0000000..74db529 --- /dev/null +++ b/src/setup.rs @@ -0,0 +1,45 @@ +use std::str::FromStr; +use tracing::{Level, trace, info, warn, instrument}; + +/// no docs +#[instrument(name = "configurating")] +pub async fn setup_self_config() -> u32 { + set_api_logger().await; + trace!("searching for port config ..."); + get_api_port().await +} + +/// no docs +#[instrument(name = "logger")] +async fn set_api_logger() { + let level = match std::env::var("ML_LOG_LEVEL") { + Ok(var) => Level::from_str(&var).unwrap_or_else(|_| Level::INFO), + Err(_) => Level::INFO, + }; + tracing_subscriber::fmt() + .with_max_level(level) + .with_writer(std::io::stdout) + .with_span_events(tracing_subscriber::fmt::format::FmtSpan::NEW) + .with_line_number(false) + .with_target(false) + .with_file(false) + .compact() + .init(); + + info!("logger was configured"); +} + +/// no docs +#[instrument(name = "app-port")] +async fn get_api_port() -> u32 { + return match std::env::var("ML_API_PORT") { + Ok(var) => var.parse::().unwrap_or_else(|_| { + warn!("invalid port var ({} was given), setting up default ...", &var); + 5134 + }), + Err(er) => { + warn!("cannot find port config ({}), setting up default ...", er); + 5134 + }, + }; +} \ No newline at end of file