rush_sync_server/server/
types.rs1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use std::sync::{Arc, RwLock};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
6pub struct ServerInfo {
7 pub id: String,
8 pub name: String,
9 pub port: u16,
10 pub status: ServerStatus,
11 pub created_at: String,
12 pub created_timestamp: u64,
13}
14
15impl Default for ServerInfo {
16 fn default() -> Self {
17 let now = std::time::SystemTime::now()
18 .duration_since(std::time::UNIX_EPOCH)
19 .unwrap_or_default()
20 .as_secs();
21
22 Self {
23 id: String::new(),
24 name: String::new(),
25 port: 0,
26 status: ServerStatus::Stopped,
27 created_at: chrono::Local::now().format("%Y-%m-%d %H:%M:%S").to_string(),
28 created_timestamp: now,
29 }
30 }
31}
32
33#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
34pub enum ServerStatus {
35 Stopped,
36 Running,
37 Failed,
38}
39
40impl std::fmt::Display for ServerStatus {
41 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
42 match self {
43 ServerStatus::Stopped => write!(f, "STOPPED"),
44 ServerStatus::Running => write!(f, "RUNNING"),
45 ServerStatus::Failed => write!(f, "FAILED"),
46 }
47 }
48}
49
50#[derive(Debug, Clone)]
51pub struct ServerData {
52 pub id: String,
53 pub port: u16,
54 pub name: String,
55}
56
57pub type ServerMap = Arc<RwLock<HashMap<String, ServerInfo>>>;
58pub type ServerHandles = Arc<RwLock<HashMap<String, actix_web::dev::ServerHandle>>>;
59
60#[derive(Debug, Clone)]
61pub struct ServerContext {
62 pub servers: ServerMap,
63 pub handles: ServerHandles,
64}
65
66impl Default for ServerContext {
67 fn default() -> Self {
68 Self {
69 servers: Arc::new(RwLock::new(HashMap::new())),
70 handles: Arc::new(RwLock::new(HashMap::new())),
71 }
72 }
73}
74use crate::core::config::Config;
79
80pub fn get_server_version() -> &'static str {
81 env!("CARGO_PKG_VERSION")
82}
83
84pub fn get_server_name() -> &'static str {
85 env!("CARGO_PKG_NAME")
86}
87
88pub const SERVER_VERSION: &str = env!("CARGO_PKG_VERSION");
89pub const SERVER_NAME: &str = env!("CARGO_PKG_NAME");
90
91pub fn get_server_config_from_main(config: &Config) -> &crate::core::config::ServerConfig {
92 &config.server
93}
94
95pub fn get_logging_config_from_main(config: &Config) -> &crate::core::config::LoggingConfig {
96 &config.logging
97}