1
0
Fork 0
mirror of https://git.42l.fr/neil/sncf.git synced 2024-04-28 12:12:44 +02:00
sncf/src/config.rs
2020-08-31 16:02:10 +02:00

73 lines
2.4 KiB
Rust

use serde_json::Value;
use std::fs::File;
use std::io::Read;
use std::io::{self, BufRead, BufReader};
use std::path::Path;
// payload limit set to 5MiB
pub const PAYLOAD_LIMIT: usize = 10_000_000;
pub const PROXY_TIMEOUT: u64 = 15;
pub const CONFIG_FILE: &str = "./config.toml";
pub const CONFIG_VERSION: u8 = 1;
pub const ADJ_LIST_FILE: &str = "./adj-list.txt";
pub const NAME_LIST_FILE: &str = "./name-list.txt";
pub const LOC_FILE: &str = "./lang.json";
lazy_static! {
pub static ref CONFIG: Config = Config::init();
pub static ref ADJ_LIST: Vec<String> =
lines_from_file(ADJ_LIST_FILE).expect("Failed to load adjectives list");
pub static ref NAME_LIST: Vec<String> =
lines_from_file(NAME_LIST_FILE).expect("Failed to load names list");
pub static ref LOC: Value = init_lang();
}
fn init_lang() -> Value {
let mut file = File::open(LOC_FILE).expect("init_lang: Can't open translations file");
let mut data = String::new();
file.read_to_string(&mut data)
.expect("init_lang: Can't read translations file");
serde_json::from_str(&data).expect("init_lang(): Can't parse translations file")
}
fn lines_from_file(filename: impl AsRef<Path>) -> io::Result<Vec<String>> {
BufReader::new(File::open(filename)?).lines().collect()
}
#[derive(Deserialize)]
pub struct Config {
pub listening_address: String,
pub listening_port: u16,
pub sncf_url: String,
pub database_path: String,
pub nextcloud_url: String,
pub admin_username: String,
pub admin_password: String,
pub prune_days: u16,
pub debug_mode: bool,
pub config_version: u8,
}
// totally not copypasted from rs-short
impl Config {
pub fn init() -> Self {
let mut conffile = File::open(CONFIG_FILE).expect(
r#"Config file config.toml not found.
Please create it using config.toml.sample."#,
);
let mut confstr = String::new();
conffile
.read_to_string(&mut confstr)
.expect("Couldn't read config to string");
toml::from_str(&confstr).expect("Couldn't deserialize the config")
}
pub fn check_version(&self) {
if self.config_version != CONFIG_VERSION {
eprintln!("Your configuration file is obsolete!\nPlease update it following the instructions in https://git.42l.fr/neil/sncf/wiki/Upgrade-from-a-previous-version and update its version to {}.", CONFIG_VERSION);
panic!();
}
}
}