summaryrefslogtreecommitdiff
path: root/actix/src/main.rs
blob: f11798f8a52c6d03775d314e37da4ee96666b1ac (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
51
52
53
54
55
56
// vim: tabstop=4 shiftwidth=4 expandtab


extern crate clap;
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder};


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

#[post("/echo")]
async fn echo(req_body: String) -> impl Responder {
    HttpResponse::Ok().body(req_body)
}

async fn manual_hello() -> impl Responder {
    HttpResponse::Ok().body("Hey there!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {

    let arg_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();

    let address = arg_matches.value_of("address").unwrap();
    let port = arg_matches.value_of("port").unwrap();

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

    HttpServer::new(|| {
        App::new()
            .service(hello)
            .service(echo)
            .route("/hey", web::get().to(manual_hello))
    })
    .bind(format!("{}:{}", address, port))?
    .run()
    .await
}