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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
use std::collections::HashMap;
use serde::{Serialize, Deserialize};
//use serde_json::value::{to_value, Value};
//use log::error;
use std::io::{Error, ErrorKind, Result};

use pasetoken_lib::ConfigPaSeToken;
use pasetors::footer::Footer;

use crate::{
    CFG_FILE_EXTENSION,
    FILE_SCHEME,
    defs::{
        Local, TotpAlgorithm, TotpMode,
        deserialize_totp_algorithm,
        deserialize_totp_mode,
    },
}; 

use std::path::Path;

// use crate::tools::generate_uuid;

// fn default_config_empty() -> String {
//   "".to_string()
// }
// fn default_config_items() -> HashMap<String,String> {
//   HashMap::new()
// }
fn default_config_resource() -> String {
  "/".to_string()
}
fn default_config_array_resource() -> Vec<String> {
  Vec::new()
}
fn default_config_serv_paths()-> Vec<ServPath> {
  Vec::new()
}
fn default_config_dflt_lang() -> String {
  "en".to_string()
}
// fn default_server_uid() -> String {
//     generate_uuid(String::from("abcdef0123456789"))
// }
fn default_config_org() -> String {
  "".to_string()
}
fn default_config_empty() -> String {
  "".to_string()
}
fn default_config_locales() -> HashMap<String,Local> {
  HashMap::new()
}
fn default_config_tpls() -> HashMap<String,String> {
  HashMap::new()
}
fn default_is_restricted() -> bool {
  false
}
fn default_config_use_mail() -> bool {
  false
}
fn default_config_use_random() -> bool {
   false
}
fn default_config_invite_expire() -> u64 {
  300
}
fn default_config_web_menu_items() -> Vec<WebMenuItem> {
    Vec::new()
}
fn default_web_menu_item_roles() -> Vec<String> {
    Vec::new()
}
fn default_sub_menu_items() -> Vec<SubMenuItem> {
    Vec::new()
}
fn default_config_totp_digits() -> usize {
    6
}
fn default_config_totp_algorithm() -> TotpAlgorithm {
    TotpAlgorithm::default()
}
fn default_config_totp_mode() -> TotpMode {
    TotpMode::default()
}
fn default_config_password_score() -> u8 {
    0
}
#[derive(Debug, Clone, Serialize, Deserialize,Default)]
pub struct SubMenuItem {
        #[serde(default = "default_config_empty")]
    pub typ: String,
        #[serde(default = "default_config_empty")]
    pub srctyp: String,
        #[serde(default = "default_config_empty")]
    pub text: String,
        #[serde(default = "default_config_empty")]
    pub url: String,
        #[serde(default = "default_web_menu_item_roles")]
    pub roles: Vec<String>,
}

#[derive(Debug, Clone, Serialize, Deserialize,Default)]
pub struct WebMenuItem {
        #[serde(default = "default_config_empty")]
    pub typ: String,
        #[serde(default = "default_config_empty")]
    pub srctyp: String,
        #[serde(default = "default_config_empty")]
    pub text: String,
        #[serde(default = "default_config_empty")]
    pub url: String,
        #[serde(default = "default_web_menu_item_roles")]
    pub roles: Vec<String>,
        #[serde(default = "default_sub_menu_items")]
    pub items: Vec<SubMenuItem>,
}

#[derive(Debug, Clone, Serialize, Deserialize,Default)]
pub struct UiConfig {
        #[serde(default = "default_config_empty")]
    pub css_link: String,
        #[serde(default = "default_config_empty")]
    pub js_link: String,
        #[serde(default = "default_config_empty")]
    pub other_css_link: String,
        #[serde(default = "default_config_empty")]
    pub other_js_link: String,
        #[serde(default = "default_config_empty")]
    pub main_js_link: String,
        #[serde(default = "default_config_empty")]
    pub utils_js_link: String,
        #[serde(default = "default_config_web_menu_items")]
    pub web_menu_items: Vec<WebMenuItem>,
} 

/// ServPath collects dir path to server as static content
#[derive(Debug, Clone, Serialize, Deserialize,Default)]
pub struct ServPath {
        #[serde(default = "default_config_empty")]
    pub src_path: String,
        #[serde(default = "default_config_empty")]
    pub url_path: String,
        #[serde(default = "default_config_empty")]
    pub not_found: String,
        #[serde(default = "default_config_empty")]
    pub not_auth: String,
        #[serde(default = "default_is_restricted")]
    pub is_restricted: bool,
        #[serde(default = "default_config_empty")]
    pub redirect_to: String,
}
/// Config collects config values.
#[derive(Debug, Clone, Serialize, Deserialize,Default)]
pub struct Config {
    pub hostport: String,
    pub bind: String,
    pub port: u16,
    pub protocol: String,
        #[serde(default = "default_config_org")]
    pub org: String,
    pub name: String,
    pub verbose: u8,
    pub prefix: String,
    pub resources_path: String,
    pub cert_file: String,
    pub key_file: String,
    //pub certs_store_path: String,
    //pub cert_file_sufix: String,
        #[serde(default = "default_config_array_resource")]
    pub allow_origin: Vec<String>,
        #[serde(default = "default_config_array_resource")]
    pub langs: Vec<String>,
        #[serde(default = "default_config_dflt_lang")]
    pub dflt_lang: String,
        #[serde(default = "default_config_empty")]
    pub path_locales_config: String,
        #[serde(default = "default_config_locales")]
    pub locales: HashMap<String,Local>,

    // Some paths 
        #[serde(default = "default_config_resource")]
    pub root_path: String,
        #[serde(default = "default_config_resource")]
    pub defaults_path: String,
        #[serde(default = "default_config_serv_paths")]
    pub serv_paths: Vec<ServPath>,
        #[serde(default = "default_config_resource")]
    pub docs_index: String,

        #[serde(default = "default_config_resource")]
    pub templates_path: String,
        #[serde(default = "default_config_resource")]
    pub html_url: String,
        #[serde(default = "default_config_resource")]
    pub assets_url: String,

        #[serde(default = "default_config_resource")]
    pub users_store_uri: String,
        #[serde(default = "default_config_empty")]
    pub user_store_access: String,

        #[serde(default = "default_config_empty")]
    pub signup_mode: String,
        #[serde(default = "default_config_invite_expire")]
    pub invite_expire: u64,
        #[serde(default = "default_config_use_random")]
    pub use_random: bool,
        #[serde(default = "default_config_totp_digits")]
    pub totp_digits: usize,
        #[serde(default = "default_config_totp_mode", deserialize_with = "deserialize_totp_mode")]
    pub totp_mode: TotpMode,
        #[serde(default = "default_config_totp_algorithm", deserialize_with = "deserialize_totp_algorithm")]
    pub totp_algorithm: TotpAlgorithm,

        #[serde(default = "default_config_password_score")]
    pub password_score: u8,
        #[serde(default = "default_config_empty")]
    pub admin_fields: String,
        #[serde(default = "default_config_empty")]
    pub mail_from: String,
        #[serde(default = "default_config_empty")]
    pub mail_reply_to: String,

        #[serde(default = "default_config_use_mail")]
    pub use_mail: bool,
        #[serde(default = "default_config_empty")]
    pub smtp: String,
        #[serde(default = "default_config_empty")]
    pub smtp_auth: String,

    #[cfg(feature = "authstore")]
        #[serde(default = "default_config_resource")]
    pub authz_store_uri: String,
    #[cfg(feature = "casbin")]
        #[serde(default = "default_config_empty")]
    pub authz_model_path: String,
    #[cfg(feature = "casbin")]
        #[serde(default = "default_config_empty")]
    pub authz_policy_path: String,
        #[serde(default = "default_config_empty")]

    pub session_store_uri: String,
        #[serde(default = "default_config_empty")]
    pub session_store_file: String,
    pub session_expire: u64,

    pub paseto: ConfigPaSeToken,

    pub ui: UiConfig,

        #[serde(default = "default_config_tpls")]
    pub tpls: HashMap<String,String>,
}

impl Config {
    fn fix_root_path<Config>(&mut self, root_path:  String ) { 
        if root_path != self.root_path {
            self.root_path = root_path.to_owned();
        }
        if self.root_path.is_empty() || ! Path::new(&self.root_path).exists() {
            return;
        }
        if self.users_store_uri.starts_with(FILE_SCHEME) {
            self.users_store_uri = format!("{}{}",
                FILE_SCHEME, self.fix_item_path(self.users_store_uri.replace(FILE_SCHEME,"")));
        } 
        if self.session_store_uri.starts_with(FILE_SCHEME) {
            self.session_store_uri = format!("{}{}",
                FILE_SCHEME, self.fix_item_path(self.session_store_uri.replace(FILE_SCHEME,"")));
        }
        self.cert_file = self.fix_item_path(self.cert_file.to_owned());
        self.key_file = self.fix_item_path(self.key_file.to_owned());
        self.templates_path = self.fix_item_path(self.templates_path.to_owned());
        self.defaults_path = self.fix_item_path(self.defaults_path.to_owned());
        self.paseto.public_path = self.fix_item_path(self.paseto.public_path.to_owned());
        self.paseto.secret_path = self.fix_item_path(self.paseto.secret_path.to_owned());
        #[cfg(feature = "authstore")]
        if self.authz_store_uri.starts_with(FILE_SCHEME) {
            self.authz_store_uri = format!("{}{}", 
                FILE_SCHEME, self.fix_item_path(self.authz_store_uri.replace(FILE_SCHEME,"")));
        }
        #[cfg(feature = "casbin")]
        {
            self.authz_model_path = self.fix_item_path(self.authz_model_path.to_owned());
            self.authz_policy_path = self.fix_item_path(self.authz_policy_path.to_owned());
        }
        // Loading paseto 
        (self.paseto.public_data, self.paseto.secret_data) = self.paseto.load_data();
        self.paseto.footer = ConfigPaSeToken::make_footer(
            self.paseto.map_footer.to_owned()
        ).unwrap_or(Footer::new());
    }
    fn fix_item_path(&mut self, item: String ) -> String { 
        if !item.is_empty() && ! Path::new(&item).exists() {
            format!("{}/{}",&self.root_path,&item)
        } else {
            item
        }
    }
    pub fn load_items(&mut self ) { 
        self.fix_root_path::<Config>(self.root_path.to_owned());
    } 
    #[allow(dead_code)] 
    pub fn to_json(&self) -> String {
        serde_json::to_string(self).unwrap_or_else(|e|{
            println!("Error to convert Config to json: {}",e);
            String::from("")
        })
    }
    // #[allow(dead_code)] 
    // pub fn st_html_path(&self) -> &'static str  {
    //     Box::leak(self.html_path.to_owned().into_boxed_str())
    // }
    #[allow(dead_code)] 
    pub fn full_html_url(&self, url: &str) -> String {
        format!("{}://{}:{}/{}",
            &self.protocol,&self.bind,&self.port,
            url
        )
    }
    pub fn load_from_file<'a>(file_cfg: &str, name: &str) -> Result<Self> { 
        let item_cfg: Self;
        let file_path: String;
        if file_cfg.contains(CFG_FILE_EXTENSION) {
            file_path = file_cfg.to_string();
        } else { 
            file_path = format!("{}{}",file_cfg.to_string(),CFG_FILE_EXTENSION);
        }
        let config_content:  &'a str;
        match std::fs::read_to_string(&file_path) {
            Ok(cfgcontent) => config_content = Box::leak(cfgcontent.into_boxed_str()),
            Err(e) => 
                return Err(Error::new(
                    ErrorKind::InvalidInput,
                    format!("Error read {}: {}",&file_path,e)
                )),
        };
        // match toml::from_str::<T>(&config_content) {
        match toml::from_str::<Self>(&config_content) {
            Ok(cfg) => item_cfg = cfg, 
            Err(e) => return Err(Error::new(
                ErrorKind::InvalidInput,
                format!("Error loading config {}: {}",&file_path,e)
            )),
        };
        log::info!("Loaded {} config from: {}", &name, &file_path);
        Ok(item_cfg)
    } 

}