sapphire_framework_server/
config.rs1use std::net::SocketAddr;
25use std::path::{Path, PathBuf};
26
27use sapphire_workspace::AppContext;
28use serde::{Deserialize, Serialize};
29
30use crate::error::{Error, Result};
31
32pub const SERVER_CONFIG_FILE: &str = "server.toml";
34
35#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
37#[serde(deny_unknown_fields)]
38pub struct ServerConfig {
39 #[serde(default)]
41 pub http: HttpConfig,
42 #[serde(default)]
44 pub mcp: McpConfig,
45 #[serde(default)]
47 pub update: UpdateConfig,
48}
49
50#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
55#[serde(deny_unknown_fields)]
56pub struct HttpConfig {
57 #[serde(default, skip_serializing_if = "Option::is_none")]
59 pub listen: Option<SocketAddr>,
60 #[serde(default, skip_serializing_if = "Vec::is_empty")]
62 pub allowed_hosts: Vec<String>,
63}
64
65#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
67#[serde(deny_unknown_fields)]
68pub struct McpConfig {
69 #[serde(default)]
71 pub http: bool,
72}
73
74#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
76#[serde(deny_unknown_fields)]
77pub struct UpdateConfig {
78 #[serde(default = "UpdateConfig::default_check")]
81 pub check: bool,
82}
83
84impl UpdateConfig {
85 fn default_check() -> bool {
86 true
87 }
88}
89
90impl Default for UpdateConfig {
91 fn default() -> Self {
92 UpdateConfig { check: true }
93 }
94}
95
96impl ServerConfig {
97 pub fn path_for_app(ctx: &AppContext) -> Option<PathBuf> {
99 ctx.try_config_dir().map(|dir| dir.join(SERVER_CONFIG_FILE))
100 }
101
102 pub fn load_for_app(ctx: &AppContext) -> Result<ServerConfig> {
105 match ServerConfig::path_for_app(ctx) {
106 Some(path) => ServerConfig::load(&path),
107 None => Ok(ServerConfig::default()),
108 }
109 }
110
111 pub fn load(path: &Path) -> Result<ServerConfig> {
116 match std::fs::read_to_string(path) {
117 Ok(text) => {
118 toml::from_str(&text).map_err(|e| Error::Config(format!("{}: {e}", path.display())))
119 }
120 Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(ServerConfig::default()),
121 Err(e) => Err(Error::Io(e)),
122 }
123 }
124}
125
126#[cfg(test)]
127mod tests {
128 use super::*;
129
130 #[test]
131 fn a_missing_file_is_all_defaults_and_mcp_is_off() {
132 let tmp = tempfile::TempDir::new().unwrap();
133 let config = ServerConfig::load(&tmp.path().join(SERVER_CONFIG_FILE)).unwrap();
134 assert_eq!(config, ServerConfig::default());
135 assert!(!config.mcp.http);
136 assert_eq!(config.http.listen, None);
137 assert!(config.update.check);
138 }
139
140 #[test]
141 fn the_file_turns_the_update_check_off() {
142 let tmp = tempfile::TempDir::new().unwrap();
143 let path = tmp.path().join(SERVER_CONFIG_FILE);
144 std::fs::write(&path, "[update]\ncheck = false\n").unwrap();
145 assert!(!ServerConfig::load(&path).unwrap().update.check);
146 std::fs::write(&path, "[update]\n").unwrap();
147 assert!(ServerConfig::load(&path).unwrap().update.check);
148 }
149
150 #[test]
151 fn the_file_turns_mcp_on_and_names_the_listener() {
152 let tmp = tempfile::TempDir::new().unwrap();
153 let path = tmp.path().join(SERVER_CONFIG_FILE);
154 std::fs::write(
155 &path,
156 "[http]\nlisten = \"127.0.0.1:3173\"\nallowed_hosts = [\"box.example\"]\n\n[mcp]\nhttp = true\n",
157 )
158 .unwrap();
159 let config = ServerConfig::load(&path).unwrap();
160 assert!(config.mcp.http);
161 assert_eq!(config.http.listen, Some("127.0.0.1:3173".parse().unwrap()));
162 assert_eq!(config.http.allowed_hosts, vec!["box.example".to_owned()]);
163 }
164
165 #[test]
166 fn a_misspelt_setting_is_an_error_not_a_default() {
167 let tmp = tempfile::TempDir::new().unwrap();
168 let path = tmp.path().join(SERVER_CONFIG_FILE);
169 std::fs::write(&path, "[mcp]\nhtpp = true\n").unwrap();
170 assert!(ServerConfig::load(&path).is_err());
171 }
172}