summaryrefslogtreecommitdiff
path: root/actix/src/main.rs
blob: 6368c768d80978e984888eb9e3f8134b4467d6c4 (plain)
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
// vim: tabstop=4 shiftwidth=4 expandtab


extern crate clap;
use actix_files as fs;
use actix_web::{get, App, HttpResponse, HttpServer, Responder};


#[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();

    println!("Binding to http://{}:{}", address, port);

    HttpServer::new(|| {
        App::new()
            .service(index_handler)
            .service(fs::Files::new("/static", "./static").show_files_listing())
    })
    .bind(format!("{}:{}", address, port))?
    .run()
    .await
}

fn get_cli_args() -> clap::ArgMatches<'static> {
    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()
}

#[get("/")]
async fn index_handler() -> impl Responder {
    HttpResponse::Ok().body("Hello world!")
}