#[controller]Expand description
Defines a Sword controller.
Route handlers are declared directly inside the impl block using method attributes
such as #[get], #[post], #[put], #[patch], #[delete], #[head], #[options], #[trace], and #[connect].
§Parameters
kind: Controller kind. UseController::Web,Controller::SocketIo,Controller::Grpc, orController::EventHandler.path: Required whenkind = Controller::Web.namespace: Required whenkind = Controller::SocketIo.service: Required whenkind = Controller::Grpc.source: Required whenkind = Controller::EventHandler. UseEventSource::Memory.
§Usage
ⓘ
#[controller(kind = Controller::Web, path = "/base_path")]
struct MyController {}
impl MyController {
#[get("/sub_path")]
async fn my_handler(&self) -> WebResult {
Ok(JsonResponse::Ok().message("Hello from MyController"))
}
}ⓘ
#[controller(kind = Controller::SocketIo, namespace = "/chat")]
struct ChatController;
impl ChatController {
#[on("connection")]
async fn on_connect(&self, _ctx: SocketContext) {}
}