// vim: tabstop=4 shiftwidth=4 expandtab use std::fs; extern crate clap; use actix_files; use actix_web::{get, App, HttpResponse, HttpServer, Responder}; #[derive(Debug)] struct RuntimeConfig { address: String, port: String, } #[actix_web::main] async fn main() -> std::io::Result<()> { let cfg = get_runtime_config(); println!("Binding to http://{}:{}", cfg.address, cfg.port); HttpServer::new(|| { App::new() .service(index) .service(actix_files::Files::new("/static", "./static")) // you may append .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")) .get_matches(); RuntimeConfig { address: cli_matches.value_of("address").unwrap().to_string(), port: cli_matches.value_of("port").unwrap().to_string(), } } #[get("/")] async fn index() -> impl Responder { HttpResponse::Ok() .content_type("text/html") .body(fs::read_to_string("./static/index.html").unwrap()) }