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
#[allow(unused_imports)]
use log::{debug, error, info, trace, warn};
use rocket::config::{Config, Environment, Limits, LoggingLevel, Value};
use std::collections::HashMap;
use df_st_core::config::{get_database_url, RootConfig};
pub fn set_server_config(server_config: &RootConfig) -> Config {
let mut df_st_database: HashMap<String, Value> = HashMap::new();
if let Some(url) = get_database_url(&server_config) {
df_st_database.insert("url".to_string(), Value::String(url));
}
if let Some(pool_size) = &server_config.database.pool_size {
df_st_database.insert("pool_size".to_string(), Value::Integer(*pool_size));
}
let mut database: HashMap<String, HashMap<String, Value>> = HashMap::new();
database.insert("df_st_database".to_string(), df_st_database);
let server_conf = server_config.server.clone();
let mut config = Config::build(Environment::Staging)
.address(
server_conf
.address
.unwrap_or_else(|| "127.0.0.1".to_string()),
)
.port(server_conf.port.unwrap_or(20350));
if server_conf.workers.is_some() {
config = config.workers(server_conf.workers.unwrap_or(16));
}
config = config.keep_alive(server_conf.keep_alive.unwrap_or(5));
config = config.log_level(match server_conf.log_level.unwrap_or_default().as_ref() {
"Critical" => LoggingLevel::Critical,
"Normal" => LoggingLevel::Normal,
"Debug" => LoggingLevel::Debug,
"Off" => LoggingLevel::Off,
_ => LoggingLevel::Normal,
});
if server_conf.secret_key.is_some() {
config = config.secret_key(server_conf.secret_key.unwrap());
}
if let Some(tls_conf) = server_conf.tls {
config = config.tls(tls_conf.certs, tls_conf.private_key);
}
if let Some(limits) = server_conf.limits {
let mut limit = Limits::new();
for (key, value) in limits.values {
limit = limit.limit(key, value);
}
config = config.limits(limit);
}
config = config.extra("databases", database);
config.finalize().unwrap()
}
#[derive(Clone, Debug, Default)]
pub struct ServerInfo {
pub page_max_limit: u32,
pub default_max_page_size: u32,
pub base_url: String,
pub world_id: i32,
}
impl ServerInfo {
pub fn new(server_config: &RootConfig, api_path: &str, world_id: u32) -> Self {
let mut server_info = ServerInfo::default();
let server_conf = server_config.server.clone();
let protocol = match server_conf.tls {
Some(_) => "https".to_owned(),
None => "http".to_owned(),
};
server_info.base_url = format!(
"{}://{}:{}{}",
protocol,
server_config.local_address.trim_matches('/'),
server_conf.port.unwrap_or(20350),
api_path.trim_end_matches('/'),
);
server_info.page_max_limit = 500;
server_info.default_max_page_size = 100;
server_info.world_id = world_id as i32;
server_info
}
}