aster/map/server/
server.rs1use std::path::PathBuf;
6
7use crate::map::server::routes::ApiHandlers;
8
9#[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
25pub struct VisualizationServer {
30 options: VisualizationServerOptions,
31 handlers: ApiHandlers,
32}
33
34impl VisualizationServer {
35 pub fn new(options: VisualizationServerOptions) -> Self {
37 let handlers = ApiHandlers::new(options.ontology_path.clone());
38 Self { options, handlers }
39 }
40
41 pub fn port(&self) -> u16 {
43 self.options.port
44 }
45
46 pub fn ontology_path(&self) -> &PathBuf {
48 &self.options.ontology_path
49 }
50
51 pub fn handlers(&self) -> &ApiHandlers {
53 &self.handlers
54 }
55
56 pub fn get_address(&self) -> String {
58 format!("http://localhost:{}", self.options.port)
59 }
60}
61
62pub 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}