diff options
author | xengineering <mail2xengineering@protonmail.com> | 2021-03-05 18:35:48 +0100 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2021-03-05 18:35:48 +0100 |
commit | f9714353664507845bad9e5856f5396ab3d629b2 (patch) | |
tree | feded85f317a466ed7ee68ad8dfddab7a55ec71d /actix/src/main.rs | |
parent | 1e59286e7d3516b27f143dd2db8760bac2af30ed (diff) | |
download | web-template-actix.tar web-template-actix.tar.zst web-template-actix.zip |
Serve static Files and Index at /actix
Diffstat (limited to 'actix/src/main.rs')
-rw-r--r-- | actix/src/main.rs | 23 |
1 files changed, 9 insertions, 14 deletions
diff --git a/actix/src/main.rs b/actix/src/main.rs index c350cdc..90988c4 100644 --- a/actix/src/main.rs +++ b/actix/src/main.rs @@ -1,8 +1,9 @@ // vim: tabstop=4 shiftwidth=4 expandtab +use std::fs; extern crate clap; -use actix_files as fs; +use actix_files; use actix_web::{get, App, HttpResponse, HttpServer, Responder}; @@ -10,7 +11,6 @@ use actix_web::{get, App, HttpResponse, HttpServer, Responder}; struct RuntimeConfig { address: String, port: String, - webroot_path: String, } @@ -18,14 +18,13 @@ struct RuntimeConfig { 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()) + .service(index) + .service(actix_files::Files::new("/static", "./static")) // you may append .show_files_listing() }) .bind(format!("{}:{}", cfg.address, cfg.port))? .run() @@ -48,23 +47,19 @@ fn get_runtime_config() -> RuntimeConfig { .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!") +async fn index() -> impl Responder { + HttpResponse::Ok() + .content_type("text/html") + .body(fs::read_to_string("./static/index.html").unwrap()) + } |