mirror of
https://git.42l.fr/neil/sncf.git
synced 2024-06-17 06:55:05 +02:00
72 lines
2.3 KiB
Rust
72 lines
2.3 KiB
Rust
use std::fs::File;
|
|
use std::io::Read;
|
|
use std::io::{self, BufRead, BufReader};
|
|
use std::path::Path;
|
|
use serde_json::Value;
|
|
|
|
// payload limit set to 5MiB
|
|
pub const PAYLOAD_LIMIT: usize = 50_000_000;
|
|
|
|
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! Please update it with config.toml.sample and update its version to {}.", CONFIG_VERSION);
|
|
panic!();
|
|
}
|
|
}
|
|
}
|