Sign in

    moonbitstack/moonapi/route does not have a README file

    ApiHandler

    A moonapi route handler: request context in, response out. It may raise an HttpException (or any error) instead of returning — the app catches it and maps it to a response through the registered exception handlers, so handlers read like FastAPI's raise HTTPException(...) rather than threading a Result back by hand. A plain non-raising closure is still a valid handler.

    BackgroundHandler

    A background-aware handler: it additionally receives the request's BackgroundTasks queue, so it can schedule work to run after its response is sent (← declaring a BackgroundTasks parameter in FastAPI).

    Method

    The request method a route binds to: the nine standard ones and any other token, from moonhttp.

    StreamHandler

    A streaming route's handler: request context in, a chunked response out (← returning a StreamingResponse from a FastAPI path operation). Every chunk becomes one body event on the wire, so a client reads the first long before the last one exists — the point of streaming, and what a single buffered Response cannot express.

    Reply

    What a route produced. Two kinds because moonasgi types a buffered response and a chunked one differently, and the difference has to survive as far as to_asgi, which is the only place that can put chunks on the wire.

    Route

    pub(all) struct Route {
    verb :
    Method

    path : String
    run : (
    Context
    ,
    BackgroundTasks
    ) -> Reply raise
    summary : String
    description : String
    tags : Array[String]
    deprecated : Bool
    operation_id : String
    status_code : Int?
    responses : Array[
    ResponseSpec
    ]
    name : String
    openapi_extra : Json?
    endpoint :
    Endpoint
    ?
    security : Array[
    SecurityRequirement
    ]
    dependencies : Array[String]
    include_in_schema : Bool
    validate : Bool
    }

    One registered route, the record an app keeps for each operation — what FastAPI calls APIRoute. Public so a program can list and inspect an app's routes; building one by hand is possible but the App methods are the way.

    A route stores one background-aware handler; a plain ApiHandler is wrapped to ignore the queue. security is the per-operation requirements (emitted as OpenAPI security and enforced before the handler when their scheme has an enforcer).

    Router

    pub struct Router {
    routes : Array[Route]
    ws_routes : Array[
    WsRoute
    ]
    }

    A collection of routes built away from any application and folded into one with App::include_router (← FastAPI's APIRouter). It carries the registration surface of an App and nothing else: middleware, mounts, security schemes, documentation and lifespan belong to the application that includes it.

    Router::delete

    fn Router::delete(self : Router, path : String, handler : (
    Context
    ) ->
    Response
    raise, summary? : String, description? : String, tags? : Array[String], deprecated? : Bool, operation_id? : String, status_code? : Int, responses? : Array[
    ResponseSpec
    ], name? : String, endpoint? :
    Endpoint
    , security? : Array[
    SecurityRequirement
    ], dependencies? : Array[String], include_in_schema? : Bool, validate? : Bool, openapi_extra? : Json) -> Unit

    Register a DELETE route on the router.

    Router::get

    fn Router::get(self : Router, path : String, handler : (
    Context
    ) ->
    Response
    raise, summary? : String, description? : String, tags? : Array[String], deprecated? : Bool, operation_id? : String, status_code? : Int, responses? : Array[
    ResponseSpec
    ], name? : String, endpoint? :
    Endpoint
    , security? : Array[
    SecurityRequirement
    ], dependencies? : Array[String], include_in_schema? : Bool, validate? : Bool, openapi_extra? : Json) -> Unit

    Register a GET route on the router.

    Router::new

    fn Router::new() -> Router

    An empty router. The prefix and the attributes its routes share are given at App::include_router rather than here, so one router can be included twice — under a second prefix, or on another app with different tags.

    Router::patch

    fn Router::patch(self : Router, path : String, handler : (
    Context
    ) ->
    Response
    raise, summary? : String, description? : String, tags? : Array[String], deprecated? : Bool, operation_id? : String, status_code? : Int, responses? : Array[
    ResponseSpec
    ], name? : String, endpoint? :
    Endpoint
    , security? : Array[
    SecurityRequirement
    ], dependencies? : Array[String], include_in_schema? : Bool, validate? : Bool, openapi_extra? : Json) -> Unit

    Register a PATCH route on the router.

    Router::post

    fn Router::post(self : Router, path : String, handler : (
    Context
    ) ->
    Response
    raise, summary? : String, description? : String, tags? : Array[String], deprecated? : Bool, operation_id? : String, status_code? : Int, responses? : Array[
    ResponseSpec
    ], name? : String, endpoint? :
    Endpoint
    , security? : Array[
    SecurityRequirement
    ], dependencies? : Array[String], include_in_schema? : Bool, validate? : Bool, openapi_extra? : Json) -> Unit

    Register a POST route on the router.

    Router::put

    fn Router::put(self : Router, path : String, handler : (
    Context
    ) ->
    Response
    raise, summary? : String, description? : String, tags? : Array[String], deprecated? : Bool, operation_id? : String, status_code? : Int, responses? : Array[
    ResponseSpec
    ], name? : String, endpoint? :
    Endpoint
    , security? : Array[
    SecurityRequirement
    ], dependencies? : Array[String], include_in_schema? : Bool, validate? : Bool, openapi_extra? : Json) -> Unit

    Register a PUT route on the router.

    Router::route

    fn Router::route(self : Router, verb :
    Method
    , path : String, handler : (
    Context
    ) ->
    Response
    raise, summary? : String, description? : String, tags? : Array[String], deprecated? : Bool, operation_id? : String, status_code? : Int, responses? : Array[
    ResponseSpec
    ], name? : String, endpoint? :
    Endpoint
    , security? : Array[
    SecurityRequirement
    ], dependencies? : Array[String], include_in_schema? : Bool, validate? : Bool, openapi_extra? : Json) -> Unit

    Register a route on the router for an explicit method. Takes what App::route takes and means the same by it; the route reaches an application when the router is included.

    Router::route_bg

    fn Router::route_bg(self : Router, verb :
    Method
    , path : String, handler : (
    Context
    ,
    BackgroundTasks
    ) ->
    Response
    raise, summary? : String, description? : String, tags? : Array[String], deprecated? : Bool, operation_id? : String, status_code? : Int, responses? : Array[
    ResponseSpec
    ], name? : String, endpoint? :
    Endpoint
    , security? : Array[
    SecurityRequirement
    ], dependencies? : Array[String], include_in_schema? : Bool, validate? : Bool, openapi_extra? : Json) -> Unit

    Register a background-aware route on the router — App::route_bg, deferred to whichever application includes it.

    Router::stream

    fn Router::stream(self : Router, path : String, handler : (
    Context
    ) ->
    StreamingResponse
    raise, summary? : String, description? : String, tags? : Array[String], deprecated? : Bool, operation_id? : String, status_code? : Int, responses? : Array[
    ResponseSpec
    ], name? : String, endpoint? :
    Endpoint
    , security? : Array[
    SecurityRequirement
    ], dependencies? : Array[String], include_in_schema? : Bool, validate? : Bool, openapi_extra? : Json) -> Unit

    Register a streaming GET route on the router — App::stream, deferred to whichever application includes it.

    Router::websocket

    fn Router::websocket(self : Router, path : String, handler : (
    WebSocket
    ) -> Unit) -> Unit

    Register a WebSocket route on the router (← APIRouter.websocket). It takes the including prefix like any other route; the operation attributes do not apply, since a WebSocket route is not an OpenAPI operation.

    segments

    fn segments(path : String) -> Array[String]

    Split a path into its non-empty segments.

    Source Files