liveBook · Manning

Actix Web

As explained in Rust Servers, Services, and Apps

Actix Web is a Rust web framework for building HTTP servers and web applications.

Actix Web is a Rust framework for asynchronous HTTP servers, REST APIs, and server-rendered web applications. An `HttpServer` receives requests, an `App` groups routes, and each route selects a handler by path and HTTP method. A handler constructs the HTTP response. section 3.3 section 7.0

Start with a health check

Register `cfg.route("/health", web::get().to(health_check_handler))` in a `web::ServiceConfig`. The async handler returns `HttpResponse::Ok().json(...)`. Build the app with `App::new().configure(general_routes)`, then start it with `HttpServer::new(app).bind("127.0.0.1:3000")?.run().await`. A GET request to `/health` reaches that handler. section 3.1.1 section 3.1.2

Extend the same structure into a course API:

  • Define `POST /courses` to create a course, `GET /courses/{tutor_id}` to list a tutor’s courses, and `GET /courses/{tutor_id}/{course_id}` to retrieve one course. section 3.2.1 section 3.2.6
  • Keep route definitions, handlers, models, and application state in separate modules. Use `web::Path<(i32, i32)>` for the two identifiers in the detail handler. section 3.2.1 section 3.2.6
  • Put shared data in `web::Data<AppState>` and register it with `App::app_data`. Handlers receive that state as a parameter; a `Mutex` protects mutable in-memory values shared across workers. section 3.2.2

The server runs application instances on worker threads and also uses asynchronous I/O. The initial course API stores data in memory; a later iteration replaces that store with Postgres while retaining the APIs. Test handlers with constructed state and path parameters, and exercise running endpoints with a browser or `curl`. These sample tests demonstrate the technique rather than exhaustive failure coverage. section 3.3 section 4.0 section 3.2.6

For a web frontend, the same framework can serve static pages with `actix-files` and render dynamic pages with a template engine. A static-file route can be registered with `App::service(fs::Files::new("/static", "./static"))`. section 7.2 section 7.0

How Rust Servers, Services, and Apps covers it

How deep it goes
Works through server setup, routing, handlers, shared state, and API tests, then extends the application toward persistence and web pages.
Examples
Uses a health-check endpoint, three course APIs, a shared visit counter, handler tests, and a static HTML page.
What it assumes
Assumes familiarity with Rust and Cargo while introducing Actix server, route, and handler conventions through a basic endpoint.
What you can do after
Build it from scratch as an Actix Web service with routes, handlers, shared state, tests, and a path toward persistent data.
How it connects
Connects HTTP routing and request handlers to application state, asynchronous I/O, REST APIs, Postgres persistence, and server-side rendering.

Also explained in

Compare how 2 books explain Actix Web →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust Servers, Services, and Apps. The section links point to the parts of the book it draws on.