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:
- Publish to PyPI with the
justapi-pluginkeyword - Users install via
pip install justapi-plugin-yourname - Usage:
from justapi_plugin_yourname import YourPlugin; app.use(YourPlugin())
See Also
- Plugins Guide — Detailed plugin development guide
- JustAPIApp —
app.use()method - Rust Core Deep Dive — Plugin trait internals