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
|
use std::fs::File;
use std::io::Read;
use serde_derive::Deserialize;
use tracing::error;
pub const DEFAULT_LOG_LEVEL: &str = "DEBUG";
#[derive(Deserialize)]
pub struct Config {
pub bot_token: String,
#[serde(default = "Config::default_log_file")]
pub log_file: String,
#[serde(default = "Config::default_db_file")]
pub db_file: String,
#[serde(default = "Config::default_listen")]
pub listen: String,
#[serde(default = "Config::default_log_level")]
pub log_level: String,
#[serde(default = "Config::default_max_body_size")]
pub max_body_size: u64,
#[serde(default = "Config::default_sqlite_thread_pool_size")]
pub sqlite_thread_pool_size: u32,
}
impl Config {
fn default_log_level() -> String {
String::from(DEFAULT_LOG_LEVEL)
}
fn default_log_file() -> String {
String::new() // empty string means not logging to file
}
fn default_max_body_size() -> u64 {
1024 * 16
}
fn default_listen() -> String {
String::from("localhost:8080")
}
fn default_db_file() -> String {
String::from("kimikuri.db")
}
fn default_sqlite_thread_pool_size() -> u32 {
16
}
}
impl Config {
// Read config file. Panic if any error occurs.
pub fn from_file(file_path: &str) -> Config {
let mut file = File::open(file_path)
.unwrap_or_else(|err| {
error!("Cannot open config file {}: {:?}", file_path, err);
panic!("Cannot open config file {}: {:?}", file_path, err);
});
let mut config = String::new();
file.read_to_string(&mut config)
.unwrap_or_else(|err| {
error!("Cannot read config file {}: {:?}.", file_path, err);
panic!("Cannot read config file {}: {:?}.", file_path, err);
});
return serde_json::from_str(config.as_str()).unwrap_or_else(|err| {
error!("Cannot decode config file: {:?}.", err);
panic!("Cannot decode config file: {:?}.", err);
});
}
}
|