rush_sync_server/server/
types.rs1use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use std::sync::{Arc, RwLock};
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct ServerInfo {
8 pub id: String,
9 pub name: String,
10 pub port: u16,
11 pub status: ServerStatus,
12 pub created_at: String,
13 #[serde(default = "default_timestamp")]
14 pub created_timestamp: u64,
15}
16
17fn default_timestamp() -> u64 {
18 std::time::SystemTime::now()
19 .duration_since(std::time::UNIX_EPOCH)
20 .unwrap_or_default()
21 .as_secs()
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
25pub enum ServerStatus {
26 Stopped,
27 Running,
28 Failed,
29}
30
31impl std::fmt::Display for ServerStatus {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 match self {
34 ServerStatus::Stopped => write!(f, "STOPPED"),
35 ServerStatus::Running => write!(f, "RUNNING"),
36 ServerStatus::Failed => write!(f, "FAILED"),
37 }
38 }
39}
40
41#[derive(Debug, Clone)]
42pub struct ServerData {
43 pub id: String,
44 pub port: u16,
45 pub name: String,
46}
47
48pub type ServerMap = Arc<RwLock<HashMap<String, ServerInfo>>>;
50pub type ServerHandles = Arc<RwLock<HashMap<String, actix_web::dev::ServerHandle>>>;
51
52#[derive(Debug, Clone)]
54pub struct ServerContext {
55 pub servers: ServerMap,
56 pub handles: ServerHandles,
57 pub port_range_start: u16,
58}
59
60impl ServerContext {
61 pub fn new() -> Self {
62 Self {
63 servers: Arc::new(RwLock::new(HashMap::new())),
64 handles: Arc::new(RwLock::new(HashMap::new())),
65 port_range_start: 8080,
66 }
67 }
68}