TakoPlugin

Trait TakoPlugin 

Source
pub trait TakoPlugin:
    Send
    + Sync
    + 'static {
    // Required methods
    fn name(&self) -> &'static str;
    fn setup(&self, router: &Router) -> Result<()>;
}
Available on crate feature plugins only.
Expand description

Trait for implementing Tako framework plugins.

Plugins extend the framework’s functionality by implementing this trait. They can add middleware, modify routing behavior, register handlers, or integrate external services. All plugins must be thread-safe and have a static lifetime.

Plugins can be applied at both router and route levels:

  • Router-level: Use router.plugin() to apply globally
  • Route-level: Use route.plugin() to apply to specific routes

§Examples

use tako::plugins::TakoPlugin;
use tako::router::Router;
use tako::Method;
use anyhow::Result;

struct CachePlugin {
    ttl_seconds: u64,
}

impl TakoPlugin for CachePlugin {
    fn name(&self) -> &'static str {
        "cache"
    }

    fn setup(&self, router: &Router) -> Result<()> {
        // Add middleware to the router
        router.middleware(|req, next| async move {
            // Cache logic here
            next.run(req).await
        });
        Ok(())
    }
}

async fn handler(_req: tako::types::Request) -> &'static str {
    "Hello"
}

// Router-level usage
let mut router = Router::new();
router.plugin(CachePlugin { ttl_seconds: 300 });

// Route-level usage
let route = router.route(Method::GET, "/api/data", handler);
route.plugin(CachePlugin { ttl_seconds: 600 });

Required Methods§

Source

fn name(&self) -> &'static str

Returns the unique name identifier for this plugin.

Source

fn setup(&self, router: &Router) -> Result<()>

Configures and initializes the plugin with the given router.

Implementors§