use crate::config::Config;
use crate::router::Router;
pub trait Plugin {
fn name(&self) -> &'static str;
fn install(self: Box<Self>, router: &mut Router, config: &Config);
}
#[cfg(test)]
mod tests {
use super::*;
use crate::method::Method;
use crate::request::Request;
use crate::response::Response;
struct Health {
path: &'static str,
}
impl Plugin for Health {
fn name(&self) -> &'static str {
"health"
}
fn install(self: Box<Self>, router: &mut Router, config: &Config) {
let label = config.get_or("service.name", "unknown").to_string();
router.get(self.path, move |_| Response::text(format!("ok {label}")));
}
}
#[test]
fn a_plugin_registers_its_routes() {
let mut router = Router::new();
let mut config = Config::new();
config.set("service.name", "billing");
Box::new(Health { path: "/healthz" }).install(&mut router, &config);
assert_eq!(router.len(), 1);
let mut req = Request::new(Method::Get, "/healthz");
assert_eq!(
router.dispatch(&mut req).body().as_bytes(),
Some(&b"ok billing"[..])
);
}
}