split config and fileconfig
Some checks are pending
ci/woodpecker/push/publish Pipeline is pending

This commit is contained in:
Mylloon 2023-04-09 15:19:23 +02:00
parent 307cb96b4b
commit 6a592f179a
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 25 additions and 12 deletions

View file

@ -2,7 +2,7 @@ use serde::Deserialize;
use std::fs;
#[derive(Deserialize, Clone, Default)]
pub struct Config {
pub struct FileConfig {
pub scheme: Option<String>,
pub port: Option<u16>,
pub mail: Option<String>,
@ -10,18 +10,23 @@ pub struct Config {
pub onion: Option<String>,
}
impl Config {
#[derive(Clone)]
pub struct Config {
pub fc: FileConfig,
}
impl FileConfig {
fn new() -> Self {
Self {
scheme: Some("http".to_string()),
port: Some(8080),
..Config::default()
..FileConfig::default()
}
}
fn complete(a: Self) -> Self {
// Default config
let d = Config::new();
let d = FileConfig::new();
/// Return the default value if nothing is value is none
fn test<T>(val: Option<T>, default: Option<T>) -> Option<T> {
@ -42,10 +47,10 @@ impl Config {
}
}
pub fn get_config(file_path: &str) -> Config {
fn get_file_config(file_path: &str) -> FileConfig {
match fs::read_to_string(file_path) {
Ok(file) => match toml::from_str(&file) {
Ok(stored_config) => Config::complete(stored_config),
Ok(stored_config) => FileConfig::complete(stored_config),
Err(file_error) => {
panic!("Error in config file: {file_error}");
}
@ -53,7 +58,15 @@ pub fn get_config(file_path: &str) -> Config {
Err(_) =>
// No config file
{
Config::new()
FileConfig::new()
}
}
}
pub fn get_config(file_path: &str) -> Config {
let internal_config = get_file_config(file_path);
Config {
fc: internal_config,
}
}

View file

@ -33,11 +33,11 @@ mod contrib;
async fn main() -> io::Result<()> {
let config = config::get_config("./config/config.toml");
let addr = ("0.0.0.0", config.port.unwrap());
let addr = ("0.0.0.0", config.fc.port.unwrap());
println!(
"Listening to {}://{}:{}",
config.clone().scheme.unwrap(),
config.clone().fc.scheme.unwrap(),
addr.0,
addr.1
);
@ -93,7 +93,7 @@ async fn main() -> io::Result<()> {
.app_data(web::Data::new(config.clone()))
.wrap(DefaultHeaders::new().add((
"Onion-Location",
config.onion.as_deref().unwrap_or_default(),
config.fc.onion.as_deref().unwrap_or_default(),
)))
.service(index::page)
.service(agreements::security)

View file

@ -23,8 +23,8 @@ struct SecurityTemplate {
fn get_security(config: Config, info: ConnectionInfo) -> String {
let data = SecurityTemplate {
contact: config.mail.unwrap_or_default(),
pref_lang: config.lang.unwrap_or_default(),
contact: config.fc.mail.unwrap_or_default(),
pref_lang: config.fc.lang.unwrap_or_default(),
url: format!(
"{}://{}/.well-known/security.txt",
info.scheme(),