#[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(_) => {}
}
}
}