liveBook · Manning

Routing configuration

As explained in Rust Servers, Services, and Apps

A route configuration is a declaration that associates a URL path and request pattern with a handler or service operation.

Routing configuration maps incoming URL paths and HTTP methods to handler functions. In an Actix Web service, the application inspects the configured routes, selects the matching handler, and forwards the request with application state and path parameters. section 3.2.1

Build the course routes incrementally in src/routes.rs:

  • Group endpoints under the /courses scope and map POST / to new_course. section 3.2.5
  • Map GET /{tutor_id} to get_courses_for_tutor; the handler receives application state and a tutor identifier, then returns matching courses. section 3.2.5
  • Add GET /{tutor_id}/{course_id} for get_course_detail, using two path parameters to locate one course. section 3.2.6

Testing and exercising the routes

Test handlers by constructing AppState and web::Path values, invoking the async function, and asserting the HTTP status. Run the service with cargo run, create courses with curl, then request localhost:3000/courses/1 or localhost:3000/courses/1/1 to inspect the responses. section 3.2.5 section 3.2.6

pub fn course_routes(cfg: &mut web::ServiceConfig) {
    cfg.service(
        web::scope("/courses")
            .route("/", web::post().to(new_course))
            .route("/{tutor_id}", web::get().to(get_courses_for_tutor)),  #1
    );
}

Inspect the concrete Actix route declaration for the course collection and tutor-filtered GET endpoint. section 3.2.5

pub fn course_routes(cfg: &mut web::ServiceConfig) {
  cfg.service(
    web::scope("/courses")
     .route("/", web::post().to(new_course))
     .route("/{tutor_id}", web::get().to(get_courses_for_tutor))
     .route("/{tutor_id}/{course_id}", web::get().to(get_course_detail)), #1
  );
}

Inspect the completed route group, including the nested path for retrieving a single course. section 3.2.6

How Rust Servers, Services, and Apps covers it

How deep it goes
Explains scoped paths, handler mapping, parameters, testing, and execution, while deferring exhaustive production tests.
Examples
Uses course creation, tutor course lists, single-course lookup, Actix declarations, curl requests, and handler status tests.
What it assumes
Assumes a structured Rust service with handlers and application state, while demonstrating route declarations and test argument construction.
What you can do after
Build it from scratch by declaring Actix routes, connecting handlers, testing parameters, and exercising endpoints with curl.
How it connects
Connects URL paths with HTTP methods, request handlers, application state, path parameters, and HTTP responses.

Also explained in

Compare how 2 books explain routing configuration →

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.