Skip to main content

mcp_stdio_proxy/
config.rs

1use anyhow::Result;
2use serde::Deserialize;
3use std::env;
4use std::fs::File;
5use std::path::Path;
6
7/// The default config file
8const DEFAULT_CONFIG_YAML: &str = include_str!("../config.yml");
9
10/// config.yml 中 mirror 段的结构
11#[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    /// The port to listen on for incoming connections
32    pub port: u16,
33}
34#[allow(dead_code)]
35#[derive(Debug, Deserialize, Clone)]
36pub struct LogConfig {
37    /// The log level to use
38    pub level: String,
39    /// The path to the log file
40    pub path: String,
41    /// The number of log files to retain (default: 20)
42    #[serde(default = "default_retain_days")]
43    pub retain_days: u32,
44}
45
46/// Default log files to retain
47fn default_retain_days() -> u32 {
48    5
49}
50
51#[allow(dead_code)]
52impl AppConfig {
53    /// Load the config file from the following sources:
54    /// 1. /app/config.yml
55    /// 2. config.yml
56    /// 3. BOT_SERVER_CONFIG environment variable
57    ///
58    /// Environment variables can override config values:
59    /// - MCP_PROXY_PORT: Override server port
60    /// - MCP_PROXY_LOG_DIR: Override log directory path
61    /// - MCP_PROXY_LOG_LEVEL: Override log level
62    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                // 如果都没有,则使用默认配置
73                serde_yaml::from_str::<AppConfig>(DEFAULT_CONFIG_YAML)
74            }
75        }?;
76
77        // 环境变量覆盖配置(优先级最高)
78        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        // 获取日志文件的父目录
99        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}