1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#[cfg(any(feature = "wss", feature = "http"))]
use std::{fs};
#[cfg(any(feature = "wss", feature = "http"))]
use json::{JsonValue};
#[cfg(any(feature = "http"))]
use crate::http::http::Http;
#[cfg(any(feature = "http"))]
use crate::http::header::Header;
#[cfg(any(feature = "http"))]
use crate::http::body::Body;
#[cfg(any(feature = "http"))]
use crate::http::WebConfig;

#[cfg(any(feature = "wss"))]
use crate::wss::Wss;
#[cfg(any(feature = "wss"))]
use crate::wss::WssConfig;

#[cfg(any(feature = "http"))]
pub mod http;
#[cfg(any(feature = "wss"))]
pub mod wss;

#[derive(Clone, Debug)]
pub enum Tcp {
    #[cfg(any(feature = "http"))]
    HTTP(Http),
    #[cfg(any(feature = "wss"))]
    WSS(Wss),
}

impl Tcp {
    #[cfg(any(feature = "http"))]
    pub fn http(path: &str) -> Tcp {
        let config = match fs::read_to_string(path) {
            Ok(content) => {
                match json::parse(content.as_str()) {
                    Ok(e) => e,
                    Err(_) => {
                        let _ = fs::create_dir(path);
                        let conf = WebConfig::default();
                        let _ = fs::write(&*path, &*conf.clone().to_json().to_string());
                        conf.to_json()
                    }
                }
            }
            Err(_) => {
                let _ = fs::create_dir(path);
                let conf = WebConfig::default();
                let _ = fs::write(&*path, &*conf.clone().to_json().to_string());
                conf.to_json()
            }
        };
        let config = WebConfig::load(config);
        return Tcp::HTTP(Http::new(config));
    }
    #[cfg(any(feature = "wss"))]
    pub fn wss(path: &str) -> Tcp {
        let config = match fs::read_to_string(path) {
            Ok(content) => {
                match json::parse(content.as_str()) {
                    Ok(e) => e,
                    Err(_) => {
                        let _ = fs::create_dir(path);
                        let conf = WssConfig::default();
                        let _ = fs::write(&*path, &*conf.clone().to_json().to_string());
                        conf.to_json()
                    }
                }
            }
            Err(_) => {
                let _ = fs::create_dir(path);
                let conf = WssConfig::default();
                let _ = fs::write(&*path, &*conf.clone().to_json().to_string());
                conf.to_json()
            }
        };
        let config = WssConfig::load(config);
        return Tcp::WSS(Wss::new(config));
    }
    #[cfg(any(feature = "wss", feature = "http"))]
    pub fn start(self, handle: fn(header: Header, body: Body) -> (i32, &'static str, JsonValue)) {
        match self {
            #[cfg(any(feature = "http"))]
            Tcp::HTTP(e) => e.start(handle),
            #[cfg(any(feature = "wss"))]
            Tcp::WSS(_) => {}
        }
    }
}