Attribute Macro xitca_codegen::route
source · #[route]
Expand description
attribute macro for xitca-web
application.
§Pattern
#[route("path", method = <method>[, attributes])]
§Attributes
"path"
: string literal represent path register to http router."/foo"
for example.method = <method>
: function path of http method register to http router.method = get
for example.enclosed = <type>
: typed middleware applied to route.enclosed_fn = <async function>
: async function as middleware applied to route
§Example
# use xitca_web::{codegen::route, handler::handler_service, service::Service, App, WebContext};
#[route("/", method = get, enclosed_fn = middleware_fn)]
async fn index() -> &'static str {
""
}
async fn middleware_fn<S, C, B, Res, Err>(service: &S, ctx: WebContext<'_, C, B>) -> Result<Res, Err>
where
S: for<'r> Service<WebContext<'r, C, B>, Response = Res, Error = Err>
{
service.call(ctx).await
}
App::new()
// add generated index typed route to application.
.at_typed(index)
# .at("/nah", handler_service(nah));
# async fn nah(_: &WebContext<'_>) -> &'static str {
# // needed to infer the body type of request
# ""
# }