Skip to main content

aster/map/server/
server.rs

1//! 可视化 Web 服务器
2//!
3//! 提供代码本体图谱的交互式可视化
4
5use std::path::PathBuf;
6
7use crate::map::server::routes::ApiHandlers;
8
9/// 服务器配置选项
10#[derive(Debug, Clone)]
11pub struct VisualizationServerOptions {
12    pub ontology_path: PathBuf,
13    pub port: u16,
14}
15
16impl Default for VisualizationServerOptions {
17    fn default() -> Self {
18        Self {
19            ontology_path: PathBuf::from("CODE_MAP.json"),
20            port: 3000,
21        }
22    }
23}
24
25/// 可视化服务器
26///
27/// 注意:实际的 HTTP 服务器实现需要依赖 axum/actix-web 等框架
28/// 这里提供核心逻辑和 API 处理器
29pub struct VisualizationServer {
30    options: VisualizationServerOptions,
31    handlers: ApiHandlers,
32}
33
34impl VisualizationServer {
35    /// 创建新的可视化服务器
36    pub fn new(options: VisualizationServerOptions) -> Self {
37        let handlers = ApiHandlers::new(options.ontology_path.clone());
38        Self { options, handlers }
39    }
40
41    /// 获取配置的端口
42    pub fn port(&self) -> u16 {
43        self.options.port
44    }
45
46    /// 获取本体路径
47    pub fn ontology_path(&self) -> &PathBuf {
48        &self.options.ontology_path
49    }
50
51    /// 获取 API 处理器
52    pub fn handlers(&self) -> &ApiHandlers {
53        &self.handlers
54    }
55
56    /// 获取服务器地址
57    pub fn get_address(&self) -> String {
58        format!("http://localhost:{}", self.options.port)
59    }
60}
61
62/// 便捷函数:创建并返回可视化服务器
63pub fn start_visualization_server(ontology_path: PathBuf, port: u16) -> VisualizationServer {
64    let options = VisualizationServerOptions {
65        ontology_path,
66        port,
67    };
68    VisualizationServer::new(options)
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn test_server_creation() {
77        let server = VisualizationServer::new(VisualizationServerOptions {
78            ontology_path: PathBuf::from("test.json"),
79            port: 8080,
80        });
81
82        assert_eq!(server.port(), 8080);
83        assert_eq!(server.get_address(), "http://localhost:8080");
84    }
85
86    #[test]
87    fn test_default_options() {
88        let options = VisualizationServerOptions::default();
89        assert_eq!(options.port, 3000);
90    }
91}