-
-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathconfig.rs
127 lines (111 loc) · 3.62 KB
/
config.rs
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
use std::fs::File;
use std::io::{Read, Write};
use rocket::config::{Config, Environment, Limits};
use serde::{Deserialize, Serialize};
use crate::dirs;
/* Far from an optimal way to solve it, but works and is simple */
static mut TESTING: bool = true;
pub fn set_testing(testing: bool) {
unsafe {
TESTING = testing;
}
}
pub fn is_testing() -> bool {
unsafe { TESTING }
}
#[derive(Serialize, Deserialize)]
pub struct AWConfig {
#[serde(default = "default_address")]
pub address: String,
#[serde(default = "default_port")]
pub port: u16,
#[serde(skip, default = "default_testing")]
pub testing: bool, // This is not written to the config file (serde(skip))
#[serde(default = "default_apikey")]
pub apikey: Option<String>,
#[serde(default = "default_cors")]
pub cors: Vec<String>,
}
impl Default for AWConfig {
fn default() -> AWConfig {
AWConfig {
address: default_address(),
port: default_port(),
testing: default_testing(),
apikey: default_apikey(),
cors: default_cors(),
}
}
}
impl AWConfig {
pub fn to_rocket_config(&self) -> rocket::Config {
let env = if self.testing {
Environment::Production
} else {
Environment::Development
};
// Needed for bucket imports
let limits = Limits::new().limit("json", 1_000_000_000);
Config::build(env)
.address(self.address.clone())
.port(self.port)
.keep_alive(0)
.limits(limits)
.finalize()
.unwrap()
}
}
fn default_address() -> String {
"127.0.0.1".to_string()
}
fn default_cors() -> Vec<String> {
Vec::<String>::new()
}
fn default_testing() -> bool {
is_testing()
}
fn default_apikey() -> Option<String> {
None
}
fn default_port() -> u16 {
if is_testing() {
5666
} else {
5600
}
}
pub fn create_config(testing: bool) -> AWConfig {
set_testing(testing);
let mut config_path = dirs::get_config_dir().unwrap();
if !testing {
config_path.push("config.toml")
} else {
config_path.push("config-testing.toml")
}
/* If there is no config file, create a new config file with default values but every value is
* commented out by default in case we would change a default value at some point in the future */
if !config_path.is_file() {
debug!("Writing default commented out config at {:?}", config_path);
let mut wfile = File::create(config_path.clone()).expect("Unable to create config file");
let default_config = AWConfig::default();
let default_config_str =
toml::to_string(&default_config).expect("Failed to convert default config to string");
let mut default_config_str_commented = String::new();
default_config_str_commented.push_str(&"### DEFAULT SETTINGS ###\n");
for line in default_config_str.lines() {
default_config_str_commented.push_str(&format!("#{}\n", line));
}
wfile
.write_all(&default_config_str_commented.into_bytes())
.expect("Failed to write config to file");
wfile.sync_all().expect("Unable to sync config file");
}
debug!("Reading config at {:?}", config_path);
let mut rfile = File::open(config_path).expect("Failed to open config file for reading");
let mut content = String::new();
rfile
.read_to_string(&mut content)
.expect("Failed to read config as a string");
let aw_config: AWConfig = toml::from_str(&content).expect("Failed to parse config file");
aw_config
}