-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Font Vincent <vincent.font@etu.umontpellier.fr>
- Loading branch information
Showing
7 changed files
with
55 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
bind_port: 3000 | ||
bind_ip: "127.0.0.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters