Skip to content

Commit

Permalink
feat: config file
Browse files Browse the repository at this point in the history
Signed-off-by: Font Vincent <vincent.font@etu.umontpellier.fr>
  • Loading branch information
Hunh0w committed Apr 29, 2024
1 parent 5b3ffbb commit 4e3b335
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 3 deletions.
5 changes: 4 additions & 1 deletion src/api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
tonic-build = "0.9"
2 changes: 2 additions & 0 deletions src/api/config/config.template.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bind_port: 3000
bind_ip: "127.0.0.1"
9 changes: 9 additions & 0 deletions src/api/src/args.rs
Original file line number Diff line number Diff line change
@@ -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<PathBuf>
}
15 changes: 15 additions & 0 deletions src/api/src/config.rs
Original file line number Diff line number Diff line change
@@ -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<YamlApiConfigFile> {
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());
}
2 changes: 2 additions & 0 deletions src/api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
pub mod client;
pub mod services;
pub mod config;
pub mod args;
19 changes: 17 additions & 2 deletions src/api/src/main.rs
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions src/shared-models/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4e3b335

Please sign in to comment.