mcp_stdio_proxy/
config.rs1use anyhow::Result;
2use serde::Deserialize;
3use std::env;
4use std::fs::File;
5use std::path::Path;
6
7const DEFAULT_CONFIG_YAML: &str = include_str!("../config.yml");
9
10#[allow(dead_code)]
12#[derive(Debug, Deserialize, Clone, Default)]
13pub struct MirrorYamlConfig {
14 #[serde(default)]
15 pub npm_registry: String,
16 #[serde(default)]
17 pub pypi_index_url: String,
18}
19
20#[allow(dead_code)]
21#[derive(Debug, Deserialize, Clone)]
22pub struct AppConfig {
23 pub server: ServerConfig,
24 pub log: LogConfig,
25 #[serde(default)]
26 pub mirror: MirrorYamlConfig,
27}
28#[allow(dead_code)]
29#[derive(Debug, Deserialize, Clone)]
30pub struct ServerConfig {
31 pub port: u16,
33}
34#[allow(dead_code)]
35#[derive(Debug, Deserialize, Clone)]
36pub struct LogConfig {
37 pub level: String,
39 pub path: String,
41 #[serde(default = "default_retain_days")]
43 pub retain_days: u32,
44}
45
46fn default_retain_days() -> u32 {
48 5
49}
50
51#[allow(dead_code)]
52impl AppConfig {
53 pub fn load_config() -> Result<Self> {
63 let mut config = match (
64 File::open("/app/config.yml"),
65 File::open("config.yml"),
66 env::var("BOT_SERVER_CONFIG"),
67 ) {
68 (Ok(file), _, _) => serde_yaml::from_reader(file),
69 (_, Ok(file), _) => serde_yaml::from_reader(file),
70 (_, _, Ok(file_path)) => serde_yaml::from_reader(File::open(file_path)?),
71 _ => {
72 serde_yaml::from_str::<AppConfig>(DEFAULT_CONFIG_YAML)
74 }
75 }?;
76
77 if let Ok(port) = env::var("MCP_PROXY_PORT")
79 && let Ok(port_num) = port.parse::<u16>()
80 {
81 config.server.port = port_num;
82 }
83
84 if let Ok(log_dir) = env::var("MCP_PROXY_LOG_DIR") {
85 config.log.path = log_dir;
86 }
87
88 if let Ok(log_level) = env::var("MCP_PROXY_LOG_LEVEL") {
89 config.log.level = log_level;
90 }
91
92 Ok(config)
93 }
94
95 pub fn log_path_init(&self) -> Result<()> {
96 let log_path = &self.log.path;
97
98 if let Some(parent) = Path::new(log_path).parent()
100 && !parent.exists()
101 {
102 std::fs::create_dir_all(parent)?;
103 }
104 Ok(())
105 }
106}