2020-11-29 18:37:33 +00:00
|
|
|
// False positive with itertools::process_results
|
|
|
|
#![allow (clippy::redundant_closure)]
|
|
|
|
|
2020-11-26 23:30:33 +00:00
|
|
|
use std::{
|
2020-11-29 18:37:33 +00:00
|
|
|
collections::HashMap,
|
2020-12-12 17:11:22 +00:00
|
|
|
convert::{TryFrom},
|
2020-11-26 23:30:33 +00:00
|
|
|
iter::FromIterator,
|
|
|
|
path::Path,
|
|
|
|
};
|
|
|
|
|
2020-12-16 14:46:03 +00:00
|
|
|
use crate::{
|
|
|
|
errors::ConfigError,
|
|
|
|
key_validity::*,
|
|
|
|
};
|
2020-11-26 23:30:33 +00:00
|
|
|
|
|
|
|
// Stuff we need to load from the config file and use to
|
|
|
|
// set up the HTTP server
|
|
|
|
|
|
|
|
pub mod file {
|
2020-11-29 18:37:33 +00:00
|
|
|
use serde::Deserialize;
|
2020-11-26 23:30:33 +00:00
|
|
|
|
2020-12-12 17:11:22 +00:00
|
|
|
use crate::key_validity::*;
|
2020-12-12 15:35:33 +00:00
|
|
|
|
2020-11-26 23:30:33 +00:00
|
|
|
#[derive (Deserialize)]
|
|
|
|
pub struct Server {
|
2020-12-12 15:57:22 +00:00
|
|
|
// This is duplicated in the hashmap, but it's not a problem
|
2020-11-30 15:52:15 +00:00
|
|
|
pub name: String,
|
2020-12-12 15:50:38 +00:00
|
|
|
pub tripcode: BlakeHashWrapper,
|
2020-11-26 23:30:33 +00:00
|
|
|
pub display_name: Option <String>,
|
|
|
|
}
|
|
|
|
|
2020-12-12 17:11:22 +00:00
|
|
|
#[derive (Deserialize)]
|
|
|
|
pub struct DevMode {
|
2020-12-16 14:46:03 +00:00
|
|
|
|
2020-12-12 17:11:22 +00:00
|
|
|
}
|
|
|
|
|
2020-12-12 01:26:58 +00:00
|
|
|
// Stuff that's identical between the file and the runtime structures
|
|
|
|
|
|
|
|
#[derive (Default, Deserialize)]
|
|
|
|
pub struct Isomorphic {
|
|
|
|
#[serde (default)]
|
2020-12-14 07:07:13 +00:00
|
|
|
pub enable_scraper_api: bool,
|
2020-12-12 15:35:33 +00:00
|
|
|
|
2020-12-12 17:11:22 +00:00
|
|
|
// If any of these fields are used, we are in dev mode and have to
|
|
|
|
// show extra warnings, since some auth may be weakened
|
|
|
|
pub dev_mode: Option <DevMode>,
|
2020-12-12 01:26:58 +00:00
|
|
|
}
|
|
|
|
|
2020-11-26 23:30:33 +00:00
|
|
|
#[derive (Deserialize)]
|
|
|
|
pub struct Config {
|
2020-12-12 01:26:58 +00:00
|
|
|
#[serde (flatten)]
|
|
|
|
pub iso: Isomorphic,
|
2020-12-16 14:46:03 +00:00
|
|
|
|
|
|
|
pub port: Option <u16>,
|
|
|
|
pub servers: Vec <Server>,
|
|
|
|
|
|
|
|
// Adding a DB will take a while, so I'm moving these out of dev mode.
|
|
|
|
pub scraper_keys: Vec <ScraperKey <Valid30Days>>,
|
2020-11-26 23:30:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stuff we actually need at runtime
|
|
|
|
|
|
|
|
pub struct Config {
|
2020-12-16 14:46:03 +00:00
|
|
|
pub iso: file::Isomorphic,
|
|
|
|
|
2020-12-13 01:54:54 +00:00
|
|
|
pub port: Option <u16>,
|
2020-12-12 15:57:22 +00:00
|
|
|
pub servers: HashMap <String, file::Server>,
|
2020-12-16 14:46:03 +00:00
|
|
|
pub scraper_keys: HashMap <String, ScraperKey <Valid30Days>>,
|
2020-11-26 23:30:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TryFrom <file::Config> for Config {
|
|
|
|
type Error = ConfigError;
|
|
|
|
|
|
|
|
fn try_from (f: file::Config) -> Result <Self, Self::Error> {
|
|
|
|
let servers = f.servers.into_iter ()
|
2020-12-12 15:57:22 +00:00
|
|
|
.map (|server| Ok::<_, ConfigError> ((server.name.clone (), server)));
|
2020-11-26 23:30:33 +00:00
|
|
|
|
|
|
|
let servers = itertools::process_results (servers, |i| HashMap::from_iter (i))?;
|
|
|
|
|
2020-12-16 14:46:03 +00:00
|
|
|
let scraper_keys = if f.iso.enable_scraper_api {
|
|
|
|
HashMap::from_iter (f.scraper_keys.into_iter ().map (|key| (key.hash.encode_base64 (), key)))
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Default::default ()
|
|
|
|
};
|
|
|
|
|
2020-11-26 23:30:33 +00:00
|
|
|
Ok (Self {
|
2020-12-16 14:46:03 +00:00
|
|
|
iso: f.iso,
|
2020-12-13 01:54:54 +00:00
|
|
|
port: f.port,
|
2020-11-26 23:30:33 +00:00
|
|
|
servers,
|
2020-12-16 14:46:03 +00:00
|
|
|
scraper_keys,
|
2020-11-26 23:30:33 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Config {
|
|
|
|
pub async fn from_file (path: &Path) -> Result <Self, ConfigError> {
|
|
|
|
use tokio::prelude::*;
|
|
|
|
|
|
|
|
let mut f = tokio::fs::File::open (path).await?;
|
|
|
|
|
2020-11-29 18:37:33 +00:00
|
|
|
let mut buffer = vec! [0_u8; 4096];
|
2020-11-26 23:30:33 +00:00
|
|
|
let bytes_read = f.read (&mut buffer).await?;
|
|
|
|
buffer.truncate (bytes_read);
|
|
|
|
|
|
|
|
let config_s = String::from_utf8 (buffer)?;
|
|
|
|
let new_config: file::Config = toml::from_str (&config_s)?;
|
|
|
|
|
|
|
|
Self::try_from (new_config)
|
|
|
|
}
|
|
|
|
}
|