Load config from file

This commit is contained in:
Joscha 2023-08-04 17:21:59 +02:00
parent 6f95d58e11
commit 4f7d4f3204
5 changed files with 144 additions and 7 deletions

23
src/config.rs Normal file
View file

@ -0,0 +1,23 @@
//! Configuration from a file.
use std::{fs, io::ErrorKind, path::Path};
use serde::Deserialize;
use tracing::info;
#[derive(Debug, Default, Deserialize)]
pub struct Config {}
impl Config {
pub fn load(path: &Path) -> anyhow::Result<Self> {
info!("Loading config from {}", path.display());
Ok(match fs::read_to_string(path) {
Ok(str) => toml::from_str(&str)?,
Err(e) if e.kind() == ErrorKind::NotFound => {
info!("No config file found, using default config");
Self::default()
}
Err(e) => Err(e)?,
})
}
}

View file

@ -1,3 +1,4 @@
mod config;
mod state;
mod r#static;
@ -7,12 +8,15 @@ use askama::Template;
use askama_axum::{IntoResponse, Response};
use axum::{extract::State, http::StatusCode, routing::get, Router};
use clap::Parser;
use directories::ProjectDirs;
use sqlx::SqlitePool;
use state::AppState;
use tokio::{select, signal::unix::SignalKind};
use tracing::{debug, info};
use tracing_subscriber::filter::LevelFilter;
use crate::config::Config;
const NAME: &str = env!("CARGO_PKG_NAME");
const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), " (", env!("VERGEN_GIT_SHA"), ")");
@ -23,6 +27,9 @@ struct Args {
db: PathBuf,
/// Path to the git repo.
repo: PathBuf,
/// Path to the config file.
#[arg(long, short)]
config: Option<PathBuf>,
/// Enable more verbose output
#[arg(long, short)]
verbose: bool,
@ -42,6 +49,17 @@ fn set_up_logging(verbose: bool) {
}
}
fn load_config(path: Option<PathBuf>) -> anyhow::Result<&'static Config> {
let config_path = path.unwrap_or_else(|| {
ProjectDirs::from("de", "plugh", "tablejohn")
.expect("could not determine home directory")
.config_dir()
.join("config.toml")
});
Ok(Box::leak(Box::new(Config::load(&config_path)?)))
}
async fn wait_for_signal() -> io::Result<()> {
debug!("Listening to signals");
@ -79,7 +97,8 @@ async fn run() -> anyhow::Result<()> {
set_up_logging(args.verbose);
info!("You are running {NAME} {VERSION}");
let state = AppState::new(&args.db, &args.repo).await?;
let config = load_config(args.config)?;
let state = AppState::new(config, &args.db, &args.repo).await?;
let app = Router::new()
.route("/", get(index))

View file

@ -8,7 +8,9 @@ use sqlx::{
sqlite::{SqliteConnectOptions, SqliteJournalMode, SqlitePoolOptions, SqliteSynchronous},
SqlitePool,
};
use tracing::{info, debug};
use tracing::{debug, info};
use crate::config::Config;
// TODO Occasionally run PRAGMA optimize
async fn open_db(db_path: &Path) -> sqlx::Result<SqlitePool> {
@ -43,13 +45,19 @@ fn open_repo(repo_path: &Path) -> anyhow::Result<ThreadSafeRepository> {
#[derive(Clone, FromRef)]
pub struct AppState {
pub config: &'static Config,
pub db: SqlitePool,
pub repo: Arc<ThreadSafeRepository>,
}
impl AppState {
pub async fn new(db_path: &Path, repo_path: &Path) -> anyhow::Result<Self> {
pub async fn new(
config: &'static Config,
db_path: &Path,
repo_path: &Path,
) -> anyhow::Result<Self> {
Ok(Self {
config,
db: open_db(db_path).await?,
repo: Arc::new(open_repo(repo_path)?),
})