81 lines
2.6 KiB
Rust
81 lines
2.6 KiB
Rust
use std::sync::Arc;
|
|
|
|
use axum::{
|
|
routing::{get, post},
|
|
Router,
|
|
};
|
|
use tower_http::trace::TraceLayer;
|
|
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
|
|
|
|
use metamesh_plugin_fast_full_hash::api::{configure, health, manifest, process, ProcessState};
|
|
use metamesh_plugin_fast_full_hash::cache::CacheManager;
|
|
use metamesh_plugin_fast_full_hash::config::create_shared_config;
|
|
|
|
#[tokio::main]
|
|
async fn main() -> anyhow::Result<()> {
|
|
// Initialize logging
|
|
tracing_subscriber::registry()
|
|
.with(
|
|
tracing_subscriber::EnvFilter::try_from_default_env()
|
|
.unwrap_or_else(|_| "info,tower_http=debug".into()),
|
|
)
|
|
.with(tracing_subscriber::fmt::layer())
|
|
.init();
|
|
|
|
// Get configuration from environment
|
|
let port: u16 = std::env::var("PORT")
|
|
.ok()
|
|
.and_then(|p| p.parse().ok())
|
|
.unwrap_or(8080);
|
|
|
|
let cache_path = std::env::var("CACHE_PATH").unwrap_or_else(|_| "/cache".to_string());
|
|
|
|
// Initialize shared state
|
|
let config = create_shared_config();
|
|
let cache = Arc::new(CacheManager::new(&cache_path));
|
|
|
|
// Load existing cache
|
|
if let Err(e) = cache.load().await {
|
|
tracing::warn!("Failed to load cache: {}", e);
|
|
}
|
|
|
|
// Create shared app state
|
|
let app_state = Arc::new(ProcessState {
|
|
config,
|
|
cache,
|
|
});
|
|
|
|
// Build router
|
|
let app = Router::new()
|
|
.route("/health", get(health))
|
|
.route("/manifest", get(manifest))
|
|
.route("/configure", post(configure))
|
|
.route("/process", post(process))
|
|
.with_state(app_state)
|
|
.layer(TraceLayer::new_for_http());
|
|
|
|
// Start server
|
|
let addr = format!("0.0.0.0:{}", port);
|
|
tracing::info!("Starting fast-full-hash plugin on {}", addr);
|
|
|
|
let listener = tokio::net::TcpListener::bind(&addr).await?;
|
|
|
|
// Beacon v2: findable by meta-sort's Plugins page (Scan → Add). Advertises
|
|
// BEACON_ADVERTISE_URL (default http://$HOSTNAME:<port>) and honours
|
|
// BEACON_BINDS, which meta-sort sets on the containers it spawns.
|
|
let _beacon = {
|
|
use meta_feeder_sdk::beacon::{advertise_plugin, advertise_url, binds_from_env, caps, Resource};
|
|
advertise_plugin("metamesh-plugin-full-hash", env!("CARGO_PKG_VERSION"), move || {
|
|
let mut r = Resource::new("full-hash", [caps::enrich("full-hash")])
|
|
.with_endpoint("http", advertise_url(port))
|
|
.with_endpoint("manifest", "/manifest");
|
|
r.binds = binds_from_env();
|
|
r
|
|
})
|
|
};
|
|
|
|
axum::serve(listener, app).await?;
|
|
|
|
Ok(())
|
|
}
|