From e4fcbe92cf1dea7ac4343966d213a568fa0deabc Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sun, 12 Jul 2020 20:11:26 -0400 Subject: [PATCH] rewrite config to be in dhall --- config.dhall | 27 +++++++++++++++++++++++++++ src/app.rs | 36 +++++++----------------------------- src/main.rs | 7 ++++++- 3 files changed, 40 insertions(+), 30 deletions(-) create mode 100644 config.dhall diff --git a/config.dhall b/config.dhall new file mode 100644 index 0000000..f461134 --- /dev/null +++ b/config.dhall @@ -0,0 +1,27 @@ +let Person = + { Type = { name : Text, tags : List Text, gitLink : Text, twitter : Text } + , default = + { name = "", tags = [] : List Text, gitLink = "", twitter = "" } + } + +let defaultPort = env:PORT ? 3030 + +let Config = + { Type = + { signalboost : List Person.Type + , port : Natural + , clackSet : List Text + , resumeFname : Text + } + , default = + { signalboost = [] : List Person.Type + , port = defaultPort + , clackSet = [ "Ashlynn" ] + , resumeFname = "./static/resume/resume.md" + } + } + +in Config::{ + , signalboost = ./signalboost.dhall + , clackSet = [ "Ashlynn", "Terry Davis", "Dennis Ritchie" ] + } diff --git a/src/app.rs b/src/app.rs index d281d58..124ae96 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,38 +1,16 @@ use crate::signalboost::Person; use anyhow::Result; +use comrak::{markdown_to_html, ComrakOptions}; use serde::Deserialize; use std::{fs, path::PathBuf}; -use comrak::{markdown_to_html, ComrakOptions}; - -mod defaults { - use std::path::PathBuf; - - pub fn clacks() -> Vec { - vec!["Ashlynn".to_string()] - } - - pub fn signalboost_fname() -> PathBuf { - "./signalboost.dhall".into() - } - - pub fn port() -> u16 { - 34252 - } - - pub fn resume_fname() -> PathBuf { - "./static/resume/resume.md".into() - } -} #[derive(Clone, Deserialize)] pub struct Config { - #[serde(default = "defaults::clacks")] + #[serde(rename = "clackSet")] clack_set: Vec, - #[serde(default = "defaults::signalboost_fname")] - signalboost_fname: PathBuf, - #[serde(default = "defaults::port")] + signalboost: Vec, port: u16, - #[serde(default = "defaults::resume_fname")] + #[serde(rename = "resumeFname")] resume_fname: PathBuf, } @@ -57,9 +35,9 @@ pub struct State { pub resume: String, } -pub fn init<'a>() -> Result { - let cfg: Config = envy::from_env()?; - let sb = serde_dhall::from_file(cfg.signalboost_fname.clone()).parse()?; +pub fn init(cfg: PathBuf) -> Result { + let cfg: Config = serde_dhall::from_file(cfg).parse()?; + let sb = cfg.signalboost.clone(); let resume = fs::read_to_string(cfg.resume_fname.clone())?; let resume: String = markdown(&resume); diff --git a/src/main.rs b/src/main.rs index d965924..dd2e83b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,12 @@ fn with_state( async fn main() -> Result<()> { pretty_env_logger::init(); - let state = Arc::new(app::init()?); + let state = Arc::new(app::init( + std::env::var("CONFIG_FNAME") + .unwrap_or("./config.dhall".into()) + .as_str() + .into(), + )?); let routes = warp::get() .and(path::end().and_then(handlers::index))