1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
// 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())
}
|