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
2021-03-24 19:48:59 +01:00

88 lines
3 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 = 2;
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";
pub const USER_AGENT: &str = "Actix-web";
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();
}
// Open LOC_FILE and store it in memory (LOC)
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")
}
// Open a file from its path
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 cookie_key: String,
pub config_version: u8,
}
// totally not copypasted from rs-short
impl Config {
// open and parse CONFIG_FILE
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. Please update at https://git.42l.fr/neil/sncf/wiki/Upgrade-from-a-previous-version --- Error")
}
// if config.config_version doesn't match the hardcoded version,
// ask the admin to manually upgrade its config file
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!();
}
}
}
pub fn get_csrf_key() -> [u8; 32] {
let mut key: [u8; 32] = Default::default();
key.copy_from_slice(&CONFIG.cookie_key.clone().into_bytes()[..32]);
key
}