diff --git a/src/api/Cargo.toml b/src/api/Cargo.toml index 5a6f3d0..69a38db 100644 --- a/src/api/Cargo.toml +++ b/src/api/Cargo.toml @@ -8,9 +8,12 @@ edition = "2021" [dependencies] actix-web = "4.5.1" serde = "1.0.197" +clap = { version = "4.5.3", features = ["derive"] } tonic = "0.9" prost = "0.11" shared_models = { path="../shared-models" } +serde_yaml = "0.9.34" +tracing = "0.1.40" [build-dependencies] -tonic-build = "0.9" \ No newline at end of file +tonic-build = "0.9" diff --git a/src/api/config/config.template.yaml b/src/api/config/config.template.yaml new file mode 100644 index 0000000..12a4f87 --- /dev/null +++ b/src/api/config/config.template.yaml @@ -0,0 +1,2 @@ +bind_port: 3000 +bind_ip: "127.0.0.1" \ No newline at end of file diff --git a/src/api/src/args.rs b/src/api/src/args.rs new file mode 100644 index 0000000..688f884 --- /dev/null +++ b/src/api/src/args.rs @@ -0,0 +1,9 @@ +use clap::Parser; +use std::path::PathBuf; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] +pub struct ApiArgs { + #[arg(short, long)] + pub config_path: Option +} \ No newline at end of file diff --git a/src/api/src/config.rs b/src/api/src/config.rs new file mode 100644 index 0000000..d596004 --- /dev/null +++ b/src/api/src/config.rs @@ -0,0 +1,15 @@ +use shared_models::YamlApiConfigFile; +use std::fs::File; +use std::io::{self, Read}; +use std::path::PathBuf; + +pub fn load_config(config_path: &PathBuf) -> io::Result { + let mut file = File::open(config_path)?; + let mut contents = String::new(); + file.read_to_string(&mut contents)?; + let config_result = serde_yaml::from_str(&contents); + if let Err(e) = config_result { + return Err(io::Error::new(io::ErrorKind::InvalidData, e)); + } + return Ok(config_result.unwrap()); +} diff --git a/src/api/src/lib.rs b/src/api/src/lib.rs index 24f4aba..bb6477a 100644 --- a/src/api/src/lib.rs +++ b/src/api/src/lib.rs @@ -1,2 +1,4 @@ pub mod client; pub mod services; +pub mod config; +pub mod args; \ No newline at end of file diff --git a/src/api/src/main.rs b/src/api/src/main.rs index 2de501b..bd09e74 100644 --- a/src/api/src/main.rs +++ b/src/api/src/main.rs @@ -1,13 +1,28 @@ use actix_web::{App, HttpServer}; use api::services::{run, shutdown}; +use api::args::ApiArgs; +use api::config::load_config; +use clap::Parser; #[actix_web::main] async fn main() -> std::io::Result<()> { - let port = 3000; + let args = ApiArgs::parse(); + let mut port = 3000; + let mut bind_ip = "127.0.0.1".to_string(); + if let Some(config_path) = args.config_path.as_deref() { + let load_result = load_config(&config_path.to_path_buf()); + if let Err(e) = load_result { + eprintln!("Failed to load configuration file, using default configuration: \n\t{e}") + }else if let Ok(config) = load_result { + port = config.bind_port; + bind_ip = config.bind_ip; + } + } + println!("Starting server on port: {}", port); HttpServer::new(|| App::new().service(run).service(shutdown)) - .bind(("127.0.0.1", port))? + .bind((bind_ip, port))? .run() .await } diff --git a/src/shared-models/src/lib.rs b/src/shared-models/src/lib.rs index cfa7582..401b3b4 100644 --- a/src/shared-models/src/lib.rs +++ b/src/shared-models/src/lib.rs @@ -28,6 +28,12 @@ pub struct YamlClientConfigFile { pub log_level: LogLevel, } +#[derive(Deserialize, Debug)] +pub struct YamlApiConfigFile { + pub bind_ip: String, + pub bind_port: u16, +} + #[derive(Serialize, Deserialize, Debug)] pub struct CloudletDtoRequest { pub language: Language,