add permission support
refactoring
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use own_assist_common::config_from_file;
|
||||
use own_assist_common::config_loader::ConfigLoadingError;
|
||||
use crate::McpServiceType;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub(crate) struct Config {
|
||||
#[serde(rename = "bind-address")]
|
||||
pub(crate) bind_address: Option<url::Url>,
|
||||
pub(crate) servers: Vec<ServerConfig>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
pub(crate) struct ServerConfig {
|
||||
pub(crate) path: String,
|
||||
pub(crate) r#type: McpServiceType
|
||||
@@ -16,6 +18,6 @@ pub(crate) struct ServerConfig {
|
||||
|
||||
impl Config {
|
||||
pub fn from_file() -> Result<Self, ConfigLoadingError> {
|
||||
config_from_file("servers.toml")
|
||||
config_from_file("server.toml")
|
||||
}
|
||||
}
|
||||
@@ -2,13 +2,16 @@
|
||||
pub mod datetime;
|
||||
mod config;
|
||||
|
||||
use axum::response::Json;
|
||||
use axum::Router;
|
||||
use rmcp::transport::{
|
||||
StreamableHttpServerConfig,
|
||||
streamable_http_server::{session::local::LocalSessionManager, tower::StreamableHttpService},
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::main;
|
||||
use tracing_subscriber::layer::SubscriberExt;
|
||||
use tracing_subscriber::util::SubscriberInitExt;
|
||||
use crate::config::{Config, ServerConfig};
|
||||
#[cfg(feature = "datetime")]
|
||||
use crate::datetime::DateTimeHandler;
|
||||
@@ -21,16 +24,16 @@ pub trait McpServerHandler: rmcp::ServerHandler {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
#[derive(Debug, Clone, Deserialize, Serialize)]
|
||||
#[non_exhaustive]
|
||||
pub enum McpServiceType {
|
||||
DateTime
|
||||
}
|
||||
|
||||
pub(crate) fn mcp_router(server_configs: Vec<ServerConfig>) -> Router {
|
||||
pub(crate) fn mcp_router(server_configs: &Vec<ServerConfig>) -> Router {
|
||||
let mut router = Router::new();
|
||||
|
||||
for config in &server_configs {
|
||||
for config in server_configs {
|
||||
router = match config.r#type {
|
||||
McpServiceType::DateTime => {
|
||||
router.route_service(config.path.as_str(), DateTimeHandler::mcp_service())
|
||||
@@ -41,16 +44,36 @@ pub(crate) fn mcp_router(server_configs: Vec<ServerConfig>) -> Router {
|
||||
router
|
||||
}
|
||||
|
||||
fn bind_address_format(url: url::Url) -> String {
|
||||
format!("{}:{}", url.host_str().expect("No host specified"), url.port().unwrap_or(8000))
|
||||
}
|
||||
|
||||
#[main]
|
||||
async fn main() {
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env()
|
||||
.unwrap_or_else(|_| "debug".to_string().into()),
|
||||
)
|
||||
.with(tracing_subscriber::fmt::layer())
|
||||
.init();
|
||||
|
||||
let config = Config::from_file().inspect_err(|e|{
|
||||
eprintln!("Error loading config: {}", e); // FIXME: use actual logger instead
|
||||
eprintln!("Error loading config: {e}");
|
||||
std::process::exit(1);
|
||||
}).unwrap();
|
||||
|
||||
let router = mcp_router(config.servers);
|
||||
println!("config: {config:#?}");
|
||||
|
||||
let tcp_listener = tokio::net::TcpListener::bind("127.0.0.1:8000").await.unwrap(); // use toml config
|
||||
let router = mcp_router(&config.servers).route("/", axum::routing::get(|| async { Json(config.servers) }));
|
||||
|
||||
let bind_address = config.bind_address.unwrap_or(url::Url::parse("localhost:8000").unwrap());
|
||||
println!("binding address at {bind_address}");
|
||||
|
||||
let tcp_listener = tokio::net::TcpListener::bind(bind_address_format(bind_address)).await.inspect_err(|e|{
|
||||
eprintln!("Error bind tcp listener: {e:#?}");
|
||||
std::process::exit(1);
|
||||
}).unwrap();
|
||||
|
||||
let ct = tokio_util::sync::CancellationToken::new();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user