How to Set Up Routing in Actix-Web

Set up Actix-Web routing by defining handlers, chaining them to paths in an App, and running the server with HttpServer.

You set up routing in Actix-Web by creating a web::App, adding routes with .route() or .service(), and binding the app to an address with HttpServer::new().

use actix_web::{web, App, HttpResponse, HttpServer, Responder};

async fn index() -> impl Responder {
    HttpResponse::Ok().body("Hello world!")
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .route("/", web::get().to(index))
    })
    .bind("127.0.0.1:8080")?
    .run()
    .await
}