How to build REST API with Actix-web

Initialize a Rust project with actix-web, define a handler, and start the server to serve HTTP requests.

Create a new project, add actix-web to Cargo.toml, and define a handler function to return a response.

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

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

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