Module saphir::controller

source ·
Expand description

Controllers are responsible for handling requests and returning responses to the client.

More specifically a Controller defines a list of endpoint (Handlers) that handle a request and return a Future of a Responder. The Responder is responsible for the Response being generated.

To create a controller, simply implement the Controller trait on a struct:

use saphir::prelude::*;

struct BasicController;

impl Controller for BasicController {
    const BASE_PATH: &'static str = "/basic";

    fn handlers(&self) -> Vec<ControllerEndpoint<Self>>
    where
        Self: Sized {
        EndpointsBuilder::new()
            .add(Method::GET, "/healthz", BasicController::healthz)
            .build()
    }
}

impl BasicController {
    async fn healthz(&self, req: Request<Body>) -> impl Responder {200}
}

Structs

  • Builder to simplify returning a list of endpoints in the handlers method of the controller trait

Traits

  • Trait that defines how a controller handles its requests
  • Trait that defines a handler within a controller. This trait is not meant to be implemented manually as there is a blanket implementation for Async Fns

Type Definitions