Skip to main content

Plugin

Trait Plugin 

Source
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§

Source

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".

Implementations on Foreign Types§

Source§

impl<C> Plugin for Jwt<C>
where C: DeserializeOwned + Clone + Send + Sync + 'static,

Source§

fn install(self: Box<Jwt<C>>, app: &mut AppBuilder)

Source§

impl<P, F, Fut> Plugin for Basic<P, F>
where F: Fn(String, String) -> Fut + Send + Sync + 'static, Fut: Future<Output = Option<P>> + Send + 'static, P: Clone + Send + Sync + 'static,

Source§

fn install(self: Box<Basic<P, F>>, app: &mut AppBuilder)

Source§

impl<P, F, Fut> Plugin for Bearer<P, F>
where F: Fn(String) -> Fut + Send + Sync + 'static, Fut: Future<Output = Option<P>> + Send + 'static, P: Clone + Send + Sync + 'static,

Source§

fn install(self: Box<Bearer<P, F>>, app: &mut AppBuilder)

Implementors§