diff options
author | xengineering <mail2xengineering@protonmail.com> | 2021-03-04 18:41:41 +0100 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2021-03-04 18:41:41 +0100 |
commit | 1e59286e7d3516b27f143dd2db8760bac2af30ed (patch) | |
tree | 5c2ec0a93ec94baa660b8a19cde27de190e21ef1 | |
parent | baefaadd35f915834b4c6a4a097908dddceab06f (diff) | |
download | web-template-1e59286e7d3516b27f143dd2db8760bac2af30ed.tar web-template-1e59286e7d3516b27f143dd2db8760bac2af30ed.tar.zst web-template-1e59286e7d3516b27f143dd2db8760bac2af30ed.zip |
Introduce RuntimeConfig Struct
-rw-r--r-- | actix/src/main.rs | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/actix/src/main.rs b/actix/src/main.rs index 6368c76..c350cdc 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -6,27 +6,35 @@ use actix_files as fs; use actix_web::{get, App, HttpResponse, HttpServer, Responder}; +#[derive(Debug)] +struct RuntimeConfig { + address: String, + port: String, + webroot_path: String, +} + + #[actix_web::main] async fn main() -> std::io::Result<()> { - let cli_args = get_cli_args(); - let address = cli_args.value_of("address").unwrap(); - let port = cli_args.value_of("port").unwrap(); + let cfg = get_runtime_config(); + println!("{:?}", cfg); - println!("Binding to http://{}:{}", address, port); + println!("Binding to http://{}:{}", cfg.address, cfg.port); HttpServer::new(|| { App::new() .service(index_handler) .service(fs::Files::new("/static", "./static").show_files_listing()) }) - .bind(format!("{}:{}", address, port))? + .bind(format!("{}:{}", cfg.address, cfg.port))? .run() .await } -fn get_cli_args() -> clap::ArgMatches<'static> { - clap::App::new("Actix Web Server") +fn get_runtime_config() -> RuntimeConfig { + + let cli_matches = clap::App::new("Actix Web Server") .about("Executable to run the web-template Web Server") .arg(clap::Arg::with_name("address") .help("The address to which the server binds") @@ -40,7 +48,19 @@ fn get_cli_args() -> clap::ArgMatches<'static> { .short("p") .takes_value(true) .default_value("8080")) - .get_matches() + .arg(clap::Arg::with_name("webroot_path") + .help("Path to static files") + .long("webroot-path") + .short("w") + .takes_value(true) + .default_value("/srv/http")) + .get_matches(); + + RuntimeConfig { + address: cli_matches.value_of("address").unwrap().to_string(), + port: cli_matches.value_of("port").unwrap().to_string(), + webroot_path: cli_matches.value_of("webroot_path").unwrap().to_string(), + } } #[get("/")] |