justapi — /docs

JustAPI's plugin system supports Python plugins (for flexibility) and Rust plugins (for maximum performance).

Python Plugin Interface

A Python plugin is any class implementing one or more lifecycle hooks:

class MyPlugin:
    def build(self, app):
        """Called when plugin is registered with app.use()."""
        pass

    async def on_startup(self):
        """Called before server starts accepting requests."""
        pass

    async def on_shutdown(self):
        """Called during server shutdown."""
        pass

Hook Details

Method Timing Purpose
build(self, app) On app.use() Add routes, middleware, modify config
on_startup(self) Before server starts Connect to services, warm caches
on_shutdown(self) During graceful shutdown Close connections, save state

Registration

from justapi import JustAPIApp

class MyPlugin:
    def build(self, app: JustAPIApp):
        print("Plugin registered!")

    async def on_startup(self):
        print("Server starting...")

    async def on_shutdown(self):
        print("Server stopping...")

app = JustAPIApp()
app.use(MyPlugin())

Rust Plugin Interface

For performance-critical extensions, Rust plugins implement the Plugin trait:

use justapi_core::plugin::Plugin;
use async_trait::async_trait;

pub struct MyRustPlugin;

#[async_trait]
impl Plugin for MyRustPlugin {
    fn name(&self) -> &'static str {
        "MyRustPlugin"
    }

    async fn on_startup(&self) -> anyhow::Result<()> {
        println!("Rust plugin started");
        Ok(())
    }

    async fn on_shutdown(&self) -> anyhow::Result<()> {
        println!("Rust plugin stopped");
        Ok(())
    }
}

Registration

use justapi_core::plugin::PluginRegistration;
use std::sync::Arc;

inventory::submit! {
    PluginRegistration::new(|| Arc::new(MyRustPlugin))
}

Plugin Marketplace

To publish a JustAPI Python plugin to the community:

  1. Publish to PyPI with the justapi-plugin keyword
  2. Users install via pip install justapi-plugin-yourname
  3. Usage: from justapi_plugin_yourname import YourPlugin; app.use(YourPlugin())

See Also

JustAPI v2.0.9 — Open Source MIT License · Built with WebTUI · GitHub

NORMAL master justapi/docs
utf-8 Top 1:1