From caa3ce71ecc34cdbb65470d3f34a805aea2354bb Mon Sep 17 00:00:00 2001 From: Florian Briand Date: Mon, 16 Sep 2024 22:54:02 +0200 Subject: [PATCH] feat: add a function to init properly app library --- crates/app/Cargo.toml | 2 ++ crates/app/src/lib.rs | 17 +++++++++++++++++ crates/app/src/main.rs | 6 +++++- crates/desktop/src/lib.rs | 4 ++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/crates/app/Cargo.toml b/crates/app/Cargo.toml index 175b803..73aa4ff 100644 --- a/crates/app/Cargo.toml +++ b/crates/app/Cargo.toml @@ -16,6 +16,8 @@ tokio = { version = "1.39.1", features = ["macros", "rt-multi-thread"] } tower-http = { version = "0.5.2", features = ["fs"] } tower-livereload = "0.9.3" +utils = { path = "../utils" } + [dev-dependencies] cargo-watch = "8.5.1" systemfd = "0.4.0" diff --git a/crates/app/src/lib.rs b/crates/app/src/lib.rs index 818e6ea..d7daf4c 100644 --- a/crates/app/src/lib.rs +++ b/crates/app/src/lib.rs @@ -2,8 +2,13 @@ use std::path::PathBuf; use axum::http::{StatusCode, Uri}; use axum_htmx::AutoVaryLayer; +use thiserror::Error; use tower_http::services::ServeDir; +use ::utils::config::{load_config, ConfigError}; + +pub mod db; + mod menu; mod pages; @@ -11,6 +16,18 @@ async fn fallback(uri: Uri) -> (StatusCode, String) { (StatusCode::NOT_FOUND, format!("No route for {uri}")) } +#[derive(Error, Debug)] +pub enum InitError { + #[error(transparent)] + ConfigError(#[from] ConfigError), +} + +pub fn init() -> Result<(), InitError> { + load_config(None)?; + Ok(()) +} + + pub async fn get_router(assets_path: PathBuf) -> axum::Router<()> { axum::Router::new() .nest_service("/assets", ServeDir::new(assets_path)) diff --git a/crates/app/src/main.rs b/crates/app/src/main.rs index ea1bc46..e60819d 100644 --- a/crates/app/src/main.rs +++ b/crates/app/src/main.rs @@ -10,7 +10,7 @@ use tokio::net::TcpListener; use tower_livereload::predicate::Predicate; use tower_livereload::LiveReloadLayer; -use ::app::get_router; +use ::app::{get_router, init, InitError}; #[derive(Error, Debug)] pub enum AppError { @@ -20,6 +20,8 @@ pub enum AppError { NotifyWatcher(#[from] notify::Error), #[error("Missing environment variable {var}")] MissingEnvVar { var: &'static str }, + #[error("Error while initialising the app")] + Initialisation(#[from] InitError), } /// Nous filtrons les requêtes de `htmx` pour ne pas inclure le script _JS_ qui gère le rechargement @@ -61,6 +63,8 @@ fn get_livereload_layer( #[tokio::main] async fn main() -> Result<(), AppError> { + init()?; + let manifest_dir = env::var("CARGO_MANIFEST_DIR").map_err(|_| AppError::MissingEnvVar { var: "CARGO_MANIFEST_DIR", })?; diff --git a/crates/desktop/src/lib.rs b/crates/desktop/src/lib.rs index 3905361..c5b678c 100644 --- a/crates/desktop/src/lib.rs +++ b/crates/desktop/src/lib.rs @@ -10,6 +10,8 @@ use thiserror::Error; use tokio::sync::{Mutex, MutexGuard}; use tower::{Service, ServiceExt}; +use ::app::init; + #[derive(Error, Debug)] pub enum DesktopError { #[error("Axum error:\n{0}")] @@ -46,6 +48,8 @@ async fn process_tauri_request( #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { + init().expect("Failed to initialize the application"); + tauri::Builder::default() .setup(|app| { let assets_path: PathBuf = app