rewrite config to be in dhall

This commit is contained in:
Cadey Ratio 2020-07-12 20:11:26 -04:00
parent a59c848618
commit e4fcbe92cf
3 changed files with 40 additions and 30 deletions

27
config.dhall Normal file
View File

@ -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" ]
}

View File

@ -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<String> {
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<String>,
#[serde(default = "defaults::signalboost_fname")]
signalboost_fname: PathBuf,
#[serde(default = "defaults::port")]
signalboost: Vec<Person>,
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<State> {
let cfg: Config = envy::from_env()?;
let sb = serde_dhall::from_file(cfg.signalboost_fname.clone()).parse()?;
pub fn init(cfg: PathBuf) -> Result<State> {
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);

View File

@ -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))