jae-blog/src/config.rs

175 lines
4.7 KiB
Rust
Raw Normal View History

2024-05-01 18:25:01 +03:00
use std::env;
use std::net::{IpAddr, Ipv4Addr};
use std::path::PathBuf;
2024-04-18 04:05:38 +03:00
use color_eyre::eyre::{bail, Context, Result};
use serde::{Deserialize, Serialize};
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tracing::{error, info};
use crate::ranged_i128_visitor::RangedI128Visitor;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
2024-04-20 20:59:00 +03:00
#[serde(default)]
pub struct SyntectConfig {
pub load_defaults: bool,
pub themes_dir: Option<PathBuf>,
pub theme: Option<String>,
}
2024-04-20 20:59:00 +03:00
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash, Default)]
2024-04-18 04:05:38 +03:00
#[serde(default)]
pub struct RenderConfig {
pub syntect: SyntectConfig,
2024-04-18 04:05:38 +03:00
}
2024-04-20 20:59:00 +03:00
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(default)]
pub struct CacheConfig {
pub enable: bool,
pub cleanup: bool,
pub cleanup_interval: Option<u64>,
pub persistence: bool,
pub file: PathBuf,
pub compress: bool,
#[serde(deserialize_with = "check_zstd_level_bounds")]
pub compression_level: i32,
2024-04-20 20:59:00 +03:00
}
2024-04-18 04:05:38 +03:00
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(default)]
2024-05-01 18:25:01 +03:00
pub struct HttpConfig {
2024-04-18 04:05:38 +03:00
pub host: IpAddr,
pub port: u16,
2024-05-01 18:25:01 +03:00
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(default)]
pub struct DirsConfig {
pub posts: PathBuf,
pub media: PathBuf,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(default)]
pub struct Config {
2024-04-18 04:05:38 +03:00
pub title: String,
pub description: String,
2024-05-01 18:25:01 +03:00
pub raw_access: bool,
pub num_posts: usize,
pub dirs: DirsConfig,
pub http: HttpConfig,
2024-04-18 04:05:38 +03:00
pub render: RenderConfig,
2024-04-20 20:59:00 +03:00
pub cache: CacheConfig,
2024-04-18 04:05:38 +03:00
}
impl Default for Config {
fn default() -> Self {
Self {
2024-04-18 04:16:12 +03:00
title: "bingus-blog".into(),
2024-04-18 04:05:38 +03:00
description: "blazingly fast markdown blog software written in rust memory safe".into(),
2024-05-01 18:25:01 +03:00
raw_access: true,
num_posts: 5,
dirs: Default::default(),
http: Default::default(),
2024-04-18 04:05:38 +03:00
render: Default::default(),
2024-04-20 20:59:00 +03:00
cache: Default::default(),
2024-05-01 18:25:01 +03:00
}
}
}
impl Default for DirsConfig {
fn default() -> Self {
Self {
posts: "posts".into(),
media: "media".into(),
}
}
}
impl Default for HttpConfig {
fn default() -> Self {
Self {
host: IpAddr::V4(Ipv4Addr::UNSPECIFIED),
port: 3000,
2024-04-18 04:05:38 +03:00
}
}
}
2024-04-20 20:59:00 +03:00
impl Default for SyntectConfig {
2024-04-18 04:05:38 +03:00
fn default() -> Self {
Self {
2024-04-20 20:59:00 +03:00
load_defaults: false,
themes_dir: Some("themes".into()),
theme: Some("Catppuccin Mocha".into()),
2024-04-18 04:05:38 +03:00
}
}
}
2024-04-20 20:59:00 +03:00
impl Default for CacheConfig {
fn default() -> Self {
Self {
enable: true,
cleanup: true,
cleanup_interval: None,
persistence: true,
file: "cache".into(),
compress: true,
compression_level: 3,
2024-04-20 20:59:00 +03:00
}
}
}
2024-04-18 04:05:38 +03:00
pub async fn load() -> Result<Config> {
let config_file = env::var(format!("{}_CONFIG", env!("CARGO_BIN_NAME")))
.unwrap_or(String::from("config.toml"));
match tokio::fs::OpenOptions::new()
.read(true)
.open(&config_file)
.await
{
Ok(mut file) => {
let mut buf = String::new();
file.read_to_string(&mut buf)
.await
2024-04-18 19:17:33 +03:00
.context("couldn't read configuration file")?;
toml::from_str(&buf).context("couldn't parse configuration")
2024-04-18 04:05:38 +03:00
}
Err(err) => match err.kind() {
std::io::ErrorKind::NotFound => {
let config = Config::default();
info!("configuration file doesn't exist, creating");
match tokio::fs::OpenOptions::new()
.write(true)
.create(true)
.truncate(true)
.open(&config_file)
.await
{
Ok(mut file) => file
.write_all(
toml::to_string_pretty(&config)
2024-04-18 19:17:33 +03:00
.context("couldn't serialize configuration")?
2024-04-18 04:05:38 +03:00
.as_bytes(),
)
.await
.unwrap_or_else(|err| error!("couldn't write configuration: {}", err)),
Err(err) => {
error!("couldn't open file {:?} for writing: {}", &config_file, err)
}
}
Ok(config)
}
_ => bail!("couldn't open config file: {}", err),
},
}
}
fn check_zstd_level_bounds<'de, D>(d: D) -> Result<i32, D::Error>
where
D: serde::Deserializer<'de>,
{
d.deserialize_i32(RangedI128Visitor::<1, 22>)
.map(|x| x as i32)
}