Expand description
§Plugin-Based Axum Server
This module provides functionality for dynamically loading and handling plugins in an Axum-based server. It allows plugins to define routes and functionality without the need to recompile the application whenever a plugin is activated or deactivated. Plugin management is handled through a configuration file, providing flexibility for dynamic behavior.
The system is designed to facilitate extensibility by loading plugins that implement specific HTTP routes and handling request and response transformations, such as header manipulation and JSON response parsing.
§Key Features:
- Load shared libraries as plugins.
- Routes and functions from plugins are integrated into the Axum router.
- Plugins can be enabled or disabled via a configuration file (
plugin.json). - No need to recompile the main application to activate or deactivate a plugin.
- Note: After adding, enabling, or disabling one or more plugins, it is necessary to restart the server for the changes to take effect.
§Plugin Configuration:
Each plugin inside the plugins directory must include a plugins.json file. This file specifies the library path, version, and whether the plugin is enabled.
Example plugin.json entry:
{
"name": "plugin_name",
"description": "Axum Router Plugin Example",
"lib_path": "./path/to/plugin.so",
"version": "0.1.0",
"license": "MIT",
"enabled": true
}§Example Usage
use axum_router_plugin::Plugins;
use axum::{
routing::get,
Router,
};
#[tokio::main]
async fn main() {
// Load plugins from the plugins directory.
// Each plugin must have its own directory containing a plugin.json file
// that provides information about the plugin, such as the library path,
// version, and whether it's enabled.
//
// You can change the location of the plugins directory by setting
// the environment variable PLUGINS_DIR, for example:
// export PLUGINS_DIR=path/to/plugins
//
// Set the argument to true if you want to add the plugin name to the routes.
let axum_plugins = Plugins::new(Some(true));
// Load the plugins and create a router with the loaded plugins.
// If loading fails, the program will panic with an error message.
let plugins_router = match axum_plugins.load() {
Ok(router) => router,
Err(err) => panic!("Error loading plugins: {}", err),
};
// Build our application with a route.
// The plugins are nested under the "/plugin" path.
let _app = Router::new()
.route("/", get(|| async {
"Hello world!"
}))
.nest("/plugin", plugins_router);
}This example demonstrates how to load plugins dynamically at runtime, configure routes, and nest plugin routes under a specified path.
Structs§
- Plugins
- Struct for managing plugin loading, routing, and naming behavior.