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)]
11#[derive(Debug, Deserialize)]
12pub struct AppConfig {
13 pub server: ServerConfig,
14 pub log: LogConfig,
15}
16#[allow(dead_code)]
17#[derive(Debug, Deserialize)]
18pub struct ServerConfig {
19 pub port: u16,
21}
22#[allow(dead_code)]
23#[derive(Debug, Deserialize)]
24pub struct LogConfig {
25 pub level: String,
27 pub path: String,
29 #[serde(default = "default_retain_days")]
31 pub retain_days: u32,
32}
33
34fn default_retain_days() -> u32 {
36 5
37}
38
39#[allow(dead_code)]
40impl AppConfig {
41 pub fn load_config() -> Result<Self> {
46 let ret = match (
47 File::open("/app/config.yml"),
48 File::open("config.yml"),
49 env::var("BOT_SERVER_CONFIG"),
50 ) {
51 (Ok(file), _, _) => serde_yaml::from_reader(file),
52 (_, Ok(file), _) => serde_yaml::from_reader(file),
53 (_, _, Ok(file_path)) => serde_yaml::from_reader(File::open(file_path)?),
54 _ => {
55 serde_yaml::from_str::<AppConfig>(DEFAULT_CONFIG_YAML)
57 }
58 };
59
60 Ok(ret?)
61 }
62
63 pub fn log_path_init(&self) -> Result<()> {
64 let log_path = &self.log.path;
65
66 if let Some(parent) = Path::new(log_path).parent() {
68 if !parent.exists() {
69 std::fs::create_dir_all(parent)?;
70 }
71 }
72 Ok(())
73 }
74}