+fn get_exe_name() -> String {
+ let first_arg = std::env::args().next().unwrap();
+ let sep: &[_] = &['\\', '/'];
+ first_arg[first_arg.rfind(sep).unwrap()+1..].to_string()
+}
+
+fn load_config() -> Config {
+ // unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF));
+ match File::open(consts::FILE_CONF) {
+ Ok(file) => from_reader(file).unwrap_or_else(|_| panic!("Failed to open configuration file {}", consts::FILE_CONF)),
+ Err(_) => {
+ let mut file = File::create(consts::FILE_CONF) .unwrap();
+ file.write_all(to_string_pretty(&DEFAULT_CONFIG, PrettyConfig::new()).unwrap().as_bytes()).unwrap(); // We do not use 'to_writer' because it can't pretty format the output.
+ DEFAULT_CONFIG
+ }
+ }
+}
+
+fn read_key() -> String {
+ let mut key = String::new();
+ File::open(consts::FILE_KEY)
+ .unwrap_or_else(|_| panic!("Failed to open key file: {}", consts::FILE_KEY))
+ .read_to_string(&mut key)
+ .unwrap_or_else(|_| panic!("Failed to read key file: {}", consts::FILE_KEY));
+
+ String::from(
+ percent_encoding::percent_decode(key.replace('\n', "").as_bytes())
+ .decode_utf8()
+ .unwrap_or_else(|_| panic!("Failed to decode key file: {}", consts::FILE_KEY))
+ )
+}
+
+fn write_key(key : &str) {
+ let percent_encoded = percent_encoding::utf8_percent_encode(key, percent_encoding::NON_ALPHANUMERIC).to_string();
+ let mut file = File::create(consts::FILE_KEY).unwrap();
+ file.write_all(percent_encoded.as_bytes()).unwrap();
+}
+
+#[actix_rt::main]
+async fn main() -> std::io::Result<()> {
+ let key = {
+ // If the key file doesn't exist then create a new one with a random key in it.
+ if !Path::new(consts::FILE_KEY).exists() {
+ let new_key = crypto::generate_key();
+ write_key(&new_key);
+ println!("A key has been generated here: {}", consts::FILE_KEY);
+ new_key
+ } else {
+ read_key()