1use json::{object, JsonValue};
2use serde::{Deserialize, Serialize};
3use std::fs;
4use std::path::{Path, PathBuf};
5
6#[derive(Debug, Clone, Deserialize, Serialize)]
7pub struct Config {
8 pub debug: bool,
10 pub host: String,
12 pub port: u16,
14 pub server: String,
16 pub https: bool,
18 pub tls: TlsConfig,
19 pub log: bool,
21 pub root_path: PathBuf,
23 pub public: String,
25 pub webpage: String,
27 pub runtime: String,
29 pub charset: String,
31 pub token: String,
33 pub pub_key_path: String,
35 #[cfg(feature = "pools")]
36 pub pools_max: usize,
38}
39impl Config {
40 #[must_use]
41 pub fn default(path: PathBuf) -> Self {
42 Self {
43 debug: true,
44 host: "0.0.0.0".to_string(),
45 port: 3535,
46 server: "BR Rust WebServer".to_string(),
47 https: false,
48 tls: TlsConfig::new(),
49 log: true,
50 root_path: path,
51 public: "public".to_string(),
52 webpage: "spa".to_string(),
53 runtime: "runtime".to_string(),
54 charset: "utf-8".to_string(),
55 token: String::new(),
56 pub_key_path: String::new(),
57 #[cfg(feature = "pools")]
58 pools_max: 0,
59 }
60 }
61
62 #[must_use]
63 pub fn create(config_file: &Path, pkg_name: bool) -> Config {
64 let dir = config_file.parent().unwrap().to_path_buf();
65 #[derive(Clone, Debug, Deserialize, Serialize)]
66 pub struct ConfigPkg {
67 pub br_web_server: Config,
68 }
69 impl ConfigPkg {
70 pub fn new(dir: PathBuf) -> ConfigPkg {
71 Self {
72 br_web_server: Config::default(dir)
73 }
74 }
75 }
76 let mut data = Config::default(dir.clone());
77 data.from();
78 match fs::read_to_string(config_file) {
79 Ok(e) => {
80 if pkg_name {
81 toml::from_str::<ConfigPkg>(&e).unwrap().br_web_server
82 } else {
83 data
84 }
85 }
86 Err(_) => {
87 if pkg_name {
88 let data = ConfigPkg::new(dir);
89 fs::create_dir_all(config_file.parent().unwrap()).unwrap();
90 let toml = toml::to_string(&data).unwrap();
91 let _ = fs::write(config_file.to_str().unwrap(), toml);
92 data.br_web_server
93 } else {
94 let data = Config::default(dir);
95 fs::create_dir_all(config_file.parent().unwrap()).unwrap();
96 let toml = toml::to_string(&data).unwrap();
97 let _ = fs::write(config_file.to_str().unwrap(), toml);
98 data
99 }
100 }
101 }
102 }
103 fn from(&mut self) -> &mut Config {
104 let root = self.root_path.clone();
105 let runtime = root.join(self.runtime.clone()).to_str().unwrap().to_string();
106 let public = root.join(self.public.clone()).to_str().unwrap().to_string();
107 let webpage = root.join("webpage").to_str().unwrap().to_string();
108 let ssl = root.join("ssl").to_str().unwrap().to_string();
109
110 fs::create_dir_all(ssl.clone()).unwrap();
111 fs::create_dir_all(runtime.clone()).unwrap();
112 fs::create_dir_all(public.clone()).unwrap();
113 fs::create_dir_all(webpage.clone()).unwrap();
114
115 self
116 }
117 #[must_use]
118 pub fn json(self) -> JsonValue {
119 object! {
120 debug: self.debug,
121 log: self.log,
122 server: self.server,
123 host: self.host,
124 port: self.port,
125 https: self.https,
126 root_path:self.root_path.to_str().unwrap(),
127 public: self.public,
128 webpage: self.webpage,
129 runtime: self.runtime,
130 charset: self.charset,
131 token: self.token,
132 pub_key_path: self.pub_key_path,
133 }
134 }
135}
136
137
138#[derive(Clone, Debug, Deserialize, Serialize)]
139pub struct TlsConfig {
140 pub certs: PathBuf,
141 pub key: PathBuf,
142}
143impl TlsConfig {
144 fn new() -> Self {
145 Self {
146 certs: PathBuf::default(),
147 key: PathBuf::default(),
148 }
149 }
150 pub fn json(&mut self) -> JsonValue {
151 object! {
152 certs:self.certs.to_str().unwrap_or(""),
153 key: self.key.to_str().unwrap_or(""),
154 }
155 }
156 #[must_use]
157 pub fn from(value: &JsonValue, root: &Path) -> Self {
158 Self {
159 certs: root.join(value["certs"].as_str().unwrap_or("")),
160 key: root.join(value["key"].as_str().unwrap_or("")),
161 }
162 }
163}