admin_config/
database_config.rs

1use serde::{Deserialize, Serialize};
2
3pub trait ToConnectionUrl {
4    fn to_connection_url(&self) -> String;
5}
6
7#[derive(Debug, Clone, Serialize, Deserialize, Default)]
8pub struct DatabaseConfig {
9    /// MongoDB 配置
10    #[serde(default)]
11    pub mongodb: MongoDbConfig,
12    /// MySQL 配置
13    #[serde(default)]
14    pub mysql: MySqlConfig,
15    /// PostgreSQL 配置
16    #[serde(default)]
17    pub postgresql: PostgreSqlConfig,
18    /// SQLite 配置
19    #[serde(default)]
20    pub sqlite: SqliteConfig,
21    /// Qdrant 配置
22    #[serde(default)]
23    pub qdrant: QdrantConfig,
24    /// SeekDB 配置
25    #[serde(default)]
26    pub seekdb: SeekDbConfig,
27    /// SurrealDB 配置
28    #[serde(default)]
29    pub surrealdb: SurrealDbConfig,
30}
31
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct MongoDbConfig {
34    /// 主机地址
35    pub host: String,
36    /// 端口
37    pub port: u16,
38    /// 数据库名
39    pub database: String,
40    /// 用户名
41    pub username: String,
42    /// 密码
43    pub password: String,
44    /// 最大连接池大小
45    pub max_pool_size: u32,
46    /// 连接超时时间(秒)
47    pub connect_timeout: u64,
48}
49
50impl Default for MongoDbConfig {
51    fn default() -> Self {
52        Self {
53            host: "localhost".to_string(),
54            port: 27017,
55            database: "".to_string(),
56            username: "".to_string(),
57            password: "".to_string(),
58            max_pool_size: 10,
59            connect_timeout: 10,
60        }
61    }
62}
63
64impl ToConnectionUrl for MongoDbConfig {
65    fn to_connection_url(&self) -> String {
66        if self.username.is_empty() {
67            format!("mongodb://{}:{}/{}", self.host, self.port, self.database)
68        } else {
69            format!(
70                "mongodb://{}:{}@{}:{}/{}",
71                self.username, self.password, self.host, self.port, self.database
72            )
73        }
74    }
75}
76
77#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct MySqlConfig {
79    /// 主机地址
80    pub host: String,
81    /// 端口
82    pub port: u16,
83    /// 数据库名
84    pub database: String,
85    /// 用户名
86    pub username: String,
87    /// 密码
88    pub password: String,
89    /// 最大连接池大小
90    pub max_pool_size: u32,
91    /// 连接超时时间(秒)
92    pub connect_timeout: u64,
93}
94
95impl Default for MySqlConfig {
96    fn default() -> Self {
97        Self {
98            host: "localhost".to_string(),
99            port: 3306,
100            database: "".to_string(),
101            username: "".to_string(),
102            password: "".to_string(),
103            max_pool_size: 10,
104            connect_timeout: 10,
105        }
106    }
107}
108
109impl ToConnectionUrl for MySqlConfig {
110    fn to_connection_url(&self) -> String {
111        format!(
112            "mysql://{}:{}@{}:{}/{}",
113            self.username, self.password, self.host, self.port, self.database
114        )
115    }
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
119pub struct PostgreSqlConfig {
120    /// 主机地址
121    pub host: String,
122    /// 端口
123    pub port: u16,
124    /// 数据库名
125    pub database: String,
126    /// 用户名
127    pub username: String,
128    /// 密码
129    pub password: String,
130    /// 最大连接池大小
131    pub max_pool_size: u32,
132    /// 连接超时时间(秒)
133    pub connect_timeout: u64,
134}
135
136impl Default for PostgreSqlConfig {
137    fn default() -> Self {
138        Self {
139            host: "localhost".to_string(),
140            port: 5432,
141            database: "".to_string(),
142            username: "".to_string(),
143            password: "".to_string(),
144            max_pool_size: 10,
145            connect_timeout: 10,
146        }
147    }
148}
149
150impl ToConnectionUrl for PostgreSqlConfig {
151    fn to_connection_url(&self) -> String {
152        format!(
153            "postgresql://{}:{}@{}:{}/{}",
154            self.username, self.password, self.host, self.port, self.database
155        )
156    }
157}
158
159#[derive(Debug, Clone, Serialize, Deserialize)]
160pub struct SqliteConfig {
161    /// 数据库文件路径
162    pub path: String,
163    /// 最大连接池大小
164    pub max_pool_size: u32,
165}
166
167impl Default for SqliteConfig {
168    fn default() -> Self {
169        Self {
170            path: "./data.db".to_string(),
171            max_pool_size: 10,
172        }
173    }
174}
175
176impl ToConnectionUrl for SqliteConfig {
177    fn to_connection_url(&self) -> String {
178        format!("sqlite://{}", self.path)
179    }
180}
181
182#[derive(Debug, Clone, Serialize, Deserialize)]
183pub struct QdrantConfig {
184    /// 主机地址
185    pub host: String,
186    /// 端口
187    pub port: u16,
188    /// API Key
189    pub api_key: Option<String>,
190    /// 是否使用 HTTPS
191    pub use_https: bool,
192}
193
194impl Default for QdrantConfig {
195    fn default() -> Self {
196        Self {
197            host: "localhost".to_string(),
198            port: 6333,
199            api_key: None,
200            use_https: false,
201        }
202    }
203}
204
205impl ToConnectionUrl for QdrantConfig {
206    fn to_connection_url(&self) -> String {
207        let protocol = if self.use_https { "https" } else { "http" };
208        format!("{}://{}:{}", protocol, self.host, self.port)
209    }
210}
211
212#[derive(Debug, Clone, Serialize, Deserialize)]
213pub struct SeekDbConfig {
214    /// 主机地址
215    pub host: String,
216    /// 端口
217    pub port: u16,
218    /// 数据库名
219    pub database: String,
220    /// 用户名
221    pub username: String,
222    /// 密码
223    pub password: String,
224}
225
226impl Default for SeekDbConfig {
227    fn default() -> Self {
228        Self {
229            host: "localhost".to_string(),
230            port: 2881,
231            database: "".to_string(),
232            username: "".to_string(),
233            password: "".to_string(),
234        }
235    }
236}
237
238impl ToConnectionUrl for SeekDbConfig {
239    fn to_connection_url(&self) -> String {
240        if self.username.is_empty() {
241            format!("seekdb://{}:{}/{}", self.host, self.port, self.database)
242        } else {
243            format!(
244                "seekdb://{}:{}@{}:{}/{}",
245                self.username, self.password, self.host, self.port, self.database
246            )
247        }
248    }
249}
250
251#[derive(Debug, Clone, Serialize, Deserialize)]
252pub struct SurrealDbConfig {
253    /// 主机地址
254    pub host: String,
255    /// 端口
256    pub port: u16,
257    /// 命名空间
258    pub namespace: String,
259    /// 数据库名
260    pub database: String,
261    /// 用户名
262    pub username: String,
263    /// 密码
264    pub password: String,
265    /// 是否使用 HTTPS
266    pub use_https: bool,
267}
268
269impl Default for SurrealDbConfig {
270    fn default() -> Self {
271        Self {
272            host: "localhost".to_string(),
273            port: 9090,
274            namespace: "".to_string(),
275            database: "".to_string(),
276            username: "".to_string(),
277            password: "".to_string(),
278            use_https: false,
279        }
280    }
281}
282
283impl ToConnectionUrl for SurrealDbConfig {
284    fn to_connection_url(&self) -> String {
285        let protocol = if self.use_https { "wss" } else { "ws" };
286        if self.username.is_empty() {
287            format!("{}://{}:{}", protocol, self.host, self.port)
288        } else {
289            format!(
290                "{}://{}:{}@{}:{}",
291                protocol, self.username, self.password, self.host, self.port
292            )
293        }
294    }
295}