config.rs
1.22 KB
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
use std::fs::File;
use std::io::Read;
use once_cell::sync::Lazy;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct Database { url :Option<String> }
#[derive(Debug, Deserialize)]
struct Locations { upload :String
, images :String }
#[derive(Debug, Deserialize)]
pub(crate) struct Config { namespace :String
, database :Database
, locations :Locations }
pub(crate) static CONFIG :Lazy<Config> = Lazy::new(|| Config::load());
impl Config {
pub fn load() -> Config {
let filename = std::env::var("CONFIG").unwrap();
let mut buffer = vec![];
let mut file = File::open(filename).unwrap();
file.read_to_end(&mut buffer).unwrap();
let mut config :Config = toml::from_slice(&buffer).unwrap();
config.database.url = match config.database.url {
Some(url) => Some(url),
None => std::env::var("DATABASE_URL").ok()
};
config
}
pub fn namespace(&self) -> &str {
self.namespace.as_str()
}
pub fn upload_dir(&self) -> &str {
self.locations.upload.as_str()
}
pub fn images_dir(&self) -> &str {
self.locations.images.as_str()
}
}