Take config path as input

This commit is contained in:
Andras Schmelczer 2025-03-24 21:57:56 +00:00
parent 958af89116
commit baba8f82bf
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
6 changed files with 56 additions and 9 deletions

View file

@ -1,6 +1,8 @@
use std::ffi::OsString;
use anyhow::Result;
use crate::{config::Config, consts::CONFIG_PATH, database::Database};
use crate::{config::Config, consts::DEFAULT_CONFIG_PATH, database::Database};
#[derive(Clone, Debug)]
pub struct AppState {
@ -9,10 +11,11 @@ pub struct AppState {
}
impl AppState {
pub async fn try_new() -> Result<Self> {
let path = std::path::Path::new(CONFIG_PATH);
pub async fn try_new(config_path: Option<OsString>) -> Result<Self> {
let config_path = config_path.unwrap_or_else(|| OsString::from(DEFAULT_CONFIG_PATH));
let path = std::path::PathBuf::from(config_path);
let config = Config::read_or_create(path).await?;
let config = Config::read_or_create(&path).await?;
let database = Database::try_new(&config.database).await?;
Ok(Self { config, database })