pub trait Plugin {
// Required method
fn install(self: Box<Self>, app: &mut AppBuilder);
}Expand description
A reusable bundle of behavior that installs itself into an AppBuilder
at build time — Churust’s analogue of Ktor’s install(Plugin).
A plugin typically registers one or more Middleware (and may add state)
inside install. Pass a plugin to
AppBuilder::install; the builder boxes it and calls install.
use churust_core::{App, AppBuilder, Churust, Call, Middleware, Next, Plugin, Response, TestClient};
use async_trait::async_trait;
use std::sync::Arc;
use http::{header::HeaderName, HeaderValue};
struct Mark;
#[async_trait]
impl Middleware for Mark {
async fn handle(&self, call: Call, next: Next) -> Response {
let mut res = next.run(call).await;
res.headers.insert(HeaderName::from_static("x-plugin"), HeaderValue::from_static("on"));
res
}
}
struct MarkPlugin;
impl Plugin for MarkPlugin {
fn install(self: Box<Self>, app: &mut AppBuilder) {
app.add_middleware(Arc::new(Mark));
}
}
let app = Churust::server()
.install(MarkPlugin)
.routing(|r| { r.get("/", |_c: Call| async { "ok" }); })
.build();
let res = TestClient::new(app).get("/").send().await;
assert_eq!(res.header("x-plugin"), Some("on"));Required Methods§
Sourcefn install(self: Box<Self>, app: &mut AppBuilder)
fn install(self: Box<Self>, app: &mut AppBuilder)
Install this plugin into the builder (register middleware, state, etc.). Consumes the boxed plugin.
Dyn Compatibility§
This trait is dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".