Skip to content

Plugin System API

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

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
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
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())

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(())
}
}
use justapi_core::plugin::PluginRegistration;
use std::sync::Arc;
inventory::submit! {
PluginRegistration::new(|| Arc::new(MyRustPlugin))
}

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())