From 9338c968aa5079957260597aa2b42f31ccb48128 Mon Sep 17 00:00:00 2001 From: Paul Maruhn <paulmaruhn@posteo.de> Date: Fri, 14 May 2021 00:51:08 +0200 Subject: [PATCH] add config --- ffddns-web/src/config.rs | 24 ++++++++++++++ ffddns-web/src/main.rs | 70 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 90 insertions(+), 4 deletions(-) create mode 100644 ffddns-web/src/config.rs diff --git a/ffddns-web/src/config.rs b/ffddns-web/src/config.rs new file mode 100644 index 0000000..7f30eec --- /dev/null +++ b/ffddns-web/src/config.rs @@ -0,0 +1,24 @@ +use serde::{Serialize, Deserialize}; +use serde; + + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Config { + pub domain: Vec<Domain>, + pub master: String, + pub rname: String, +} + +#[derive(Clone, Debug, Serialize, Deserialize)] +pub struct Domain { + /// the domain suffix. eg. for a dynamic domain + /// mydomain.ddns.org the name here is ddns.org + pub name: String, + /// a short description for a domain + pub description: String, + /// a list of networks, which a subdomain from this + /// domain is allowed to updated to + pub nets: Vec<String>, + /// duration in days before a subdomain gets 'released' wgen not updated + pub registration_time: usize, +} diff --git a/ffddns-web/src/main.rs b/ffddns-web/src/main.rs index 4435386..afac3f5 100644 --- a/ffddns-web/src/main.rs +++ b/ffddns-web/src/main.rs @@ -2,6 +2,7 @@ mod db; mod web; +mod config; use chrono::DateTime; use chrono::Utc; @@ -18,17 +19,78 @@ use rocket::request::Outcome; use std::fmt::{self, Display}; use std::net::IpAddr; use rand; +use toml; +use lazy_static::lazy_static; +use config::Config; +use std::fs; +use std::path; +use std::io::Read; +use log::{debug, info, error}; +use std::process::exit; + + +const CONFIG_DIRS: &[&str] = &[ + "./ffdyndns.toml", + "/etc/ffdyndns.toml", + "/var/lib/ffdyndns/ffdyndns.toml", +]; + +lazy_static! { + pub static ref CONFIG: Config = { + let file = CONFIG_DIRS.iter() + .map(|x| path::Path::new(x)) + .find(|p| p.exists() && p.is_file()); + + match file { + Some(f) => { + debug!("loading config: {}", f.to_str().unwrap()); + let mut f = fs::File::open(f).unwrap(); + let mut toml_str = String::new(); + f.read_to_string(&mut toml_str).expect("can't read config file"); + + match toml::from_str::<Config>(&toml_str) { + Err(e) => { + eprintln!("configuration error: {}", e); + exit(1); + } + Ok(r) => r + } + } + None => { + eprintln!("could not find config file"); + exit(1); + }, + } + }; +} + + +// for p in CONFIG_DIRS.iter().map(|x| path::Path::new(x)) { +// if !p.exists() || !p.is_file() { +// continue; +// } +// debug!("loading config: {}", p.to_str().unwrap()); +// let mut f = fs::File::open(p).unwrap(); +// let mut toml_str = String::new(); +// f.read_to_string(&mut toml_str).expect("can't read config file"); + +// config = toml::from_str::<Config>(&toml_str).unwrap() +// }; + +// return config; #[derive(Debug, Clone)] pub struct DomainUpdate { - domain: String, - ip: IpAddr + domain: String, + ip: IpAddr } fn main() { - let db = db::Database::new("./ffddns.sqlite".into()); - web::start_web(db); + println!("{:?}", CONFIG.domain); + + let db = db::Database::new("./ffddns.sqlite".into()); + web::start_web(db); } -- GitLab