use axum::{
extract::Path,
response::{Html, Json},
routing::{get, post},
Router,
};
use oauth_provider_rs::{GitHubOAuthConfig, GitHubOAuthProvider, OAuthProvider};
use remote_mcp_kernel::microkernel::{
core::CustomRouterConfig, GitHubMicrokernelServer, MicrokernelServer,
};
use serde_json::json;
use std::net::SocketAddr;
async fn health_check() -> Json<serde_json::Value> {
Json(json!({
"status": "healthy",
"timestamp": std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs(),
"service": "custom-router-example"
}))
}
async fn metrics() -> Json<serde_json::Value> {
Json(json!({
"uptime": "5 minutes",
"requests": 42,
"memory_usage": "128MB"
}))
}
async fn api_status(Path(version): Path<String>) -> Json<serde_json::Value> {
Json(json!({
"api_version": version,
"status": "operational",
"endpoints": [
"/health",
"/metrics",
"/api/v1/status"
]
}))
}
async fn admin_dashboard() -> Html<&'static str> {
Html(r#"
<!DOCTYPE html>
<html>
<head>
<title>Admin Dashboard</title>
<style>
body { font-family: Arial, sans-serif; margin: 40px; }
.card { border: 1px solid #ddd; padding: 20px; margin: 10px 0; border-radius: 5px; }
.header { color: #333; }
</style>
</head>
<body>
<h1 class="header">Microkernel Admin Dashboard</h1>
<div class="card">
<h3>Server Status</h3>
<p>✅ Microkernel Server: Running</p>
<p>✅ OAuth Provider: Connected</p>
<p>✅ Custom Routers: 3 attached</p>
</div>
<div class="card">
<h3>Available Endpoints</h3>
<ul>
<li><a href="/health">Health Check</a></li>
<li><a href="/monitoring/metrics">Metrics</a></li>
<li><a href="/api/v1/status">API Status</a></li>
<li><a href="/admin">Admin Dashboard</a></li>
</ul>
</div>
</body>
</html>
"#)
}
async fn webhook_handler() -> Json<serde_json::Value> {
Json(json!({
"message": "Webhook received",
"processed": true,
"timestamp": std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_secs()
}))
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
tracing_subscriber::fmt::init();
let github_config = GitHubOAuthConfig {
client_id: std::env::var("GITHUB_CLIENT_ID").unwrap_or_else(|_| "demo_client_id".to_string()),
client_secret: std::env::var("GITHUB_CLIENT_SECRET").unwrap_or_else(|_| "demo_client_secret".to_string()),
redirect_uri: "http://localhost:8080/oauth/callback".to_string(),
scope: "read:user".to_string(),
provider_name: "github".to_string(),
};
let github_oauth_provider = GitHubOAuthProvider::new_github(github_config);
let oauth_provider = OAuthProvider::new(github_oauth_provider);
let health_router = Router::new()
.route("/health", get(health_check))
.route("/webhooks", post(webhook_handler));
let monitoring_router = Router::new()
.route("/metrics", get(metrics));
let api_router = Router::new()
.route("/v1/status", get(api_status))
.route("/{version}/status", get(api_status));
let admin_router = Router::new()
.route("/", get(admin_dashboard));
let custom_routers = vec![
(health_router, CustomRouterConfig::default()),
(
monitoring_router,
CustomRouterConfig::with_name_and_prefix("Monitoring", "/monitoring"),
),
(
api_router,
CustomRouterConfig::with_name_and_prefix("API", "/api"),
),
(
admin_router,
CustomRouterConfig::with_name_and_prefix("Admin Dashboard", "/admin"),
),
];
let microkernel: GitHubMicrokernelServer = MicrokernelServer::new()
.with_oauth_provider(oauth_provider)
.with_custom_routers(custom_routers);
let bind_address: SocketAddr = "127.0.0.1:8080".parse()?;
println!("🚀 Starting microkernel server with custom routers...");
println!("📍 Server listening on: http://{}", bind_address);
println!("\n📋 Available endpoints:");
println!(" OAuth:");
println!(" GET /oauth/login");
println!(" GET /oauth/callback");
println!(" POST /oauth/token");
println!(" Custom Endpoints:");
println!(" GET /health - Health check");
println!(" POST /webhooks - Webhook handler");
println!(" GET /monitoring/metrics - System metrics");
println!(" GET /api/v1/status - API status");
println!(" GET /api/{{version}}/status - Versioned API status");
println!(" GET /admin - Admin dashboard");
println!("\n🔧 Try these commands:");
println!(" curl http://localhost:8080/health");
println!(" curl http://localhost:8080/monitoring/metrics");
println!(" curl http://localhost:8080/api/v1/status");
println!(" open http://localhost:8080/admin");
microkernel.serve(bind_address).await?;
Ok(())
}