// 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 }