feat: default state

This commit is contained in:
Asger Juul Brunshøj 2025-01-22 23:08:58 +01:00
parent ad67d5997a
commit 993ba8d408
2 changed files with 10 additions and 5 deletions

View File

@ -9,6 +9,7 @@ use state::PersistentState;
use state::State;
use std::collections::BTreeMap;
use std::path::Path;
use std::path::PathBuf;
use tower_http::services::ServeDir;
use tracing::level_filters::LevelFilter;
use type_toppings::ResultExt;
@ -36,7 +37,6 @@ pub mod cli {
ImportMiniMoonboardProblems {
file_path: PathBuf,
},
// TODO: idempotent state initialization to be used in systemd preStart hook
}
}
@ -156,10 +156,16 @@ fn file_service(path: impl AsRef<Path>) -> ServeDir {
#[tracing::instrument]
async fn load_state() -> Result<State, Error> {
tracing::info!("Loading state");
let state = State {
persistent: Persistent::<PersistentState>::load(&Path::new(STATE_FILE)).await?,
let p = PathBuf::from(STATE_FILE);
let persistent = if p.try_exists()? {
tracing::info!("No state found at {STATE_FILE}, creating default state");
Persistent::<PersistentState>::load(&p).await?
} else {
Persistent::<PersistentState>::new(PersistentState::default(), p)
};
Ok(state)
Ok(State { persistent })
}
#[tracing::instrument]

View File

@ -1,4 +1,3 @@
use camino::Utf8PathBuf;
use serde::Serialize;
use serde::de::DeserializeOwned;
use std::path::Path;