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

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