// vim: tabstop=4 shiftwidth=4 expandtab extern crate clap; 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 cfg = get_runtime_config(); println!("{:?}", cfg); 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!("{}:{}", cfg.address, cfg.port))? .run() .await } 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") .long("address") .short("a") .takes_value(true) .default_value("127.0.0.1")) .arg(clap::Arg::with_name("port") .help("The port to which the server binds") .long("port") .short("p") .takes_value(true) .default_value("8080")) .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("/")] async fn index_handler() -> impl Responder { HttpResponse::Ok().body("Hello world!") }