Extract modules declaration from main.rs to lib.rs in "hagrid" crate.

This is to make "hagrid" code library alike instead of having it as a binary
which improves reusability and testability of the code.

So, the main.rs binary will be just a launcher for library code.

Changes:
- Extract modules declaration from main.rs to lib.rs in "hagrid" crate.
- Introduce "app" module which has to accumulate infrustructure code,
  but for the time being it delegates run() and handle errors.
- Rename hagrid::web::serve() to hagrid::web::run() to standardize on
  module entry function names.
- Accumulate rocket framework launching code in hagrid::web::run()
  function.
  To be able to move code around I have to replace usage of
  macros like #[rocket::launch] or #[rocket::main] with direct usage of
  :🚀:async_main, ignite() and launch() functions.
This commit is contained in:
Zeke Fast
2025-08-20 22:24:40 +02:00
parent 9adeb4d544
commit 9b6b495f56
4 changed files with 38 additions and 31 deletions

3
src/app/mod.rs Normal file
View File

@@ -0,0 +1,3 @@
pub fn run() {
crate::web::run().unwrap_or_else(|e| eprintln!("Hagrid Error: {e}"))
}

25
src/lib.rs Normal file
View File

@@ -0,0 +1,25 @@
#![recursion_limit = "1024"]
use gettext_macros::init_i18n;
mod anonymize_utils;
pub mod app;
mod counters;
mod dump;
mod gettext_strings;
mod i18n;
mod i18n_helpers;
mod mail;
mod rate_limiter;
mod sealed_state;
mod template_helpers;
mod tokens;
mod web;
#[cfg(debug_assertions)]
init_i18n!("hagrid", en, de, ja);
#[cfg(not(debug_assertions))]
init_i18n!(
"hagrid", en, de, fr, it, ja, nb, pl, tr, zh_Hans, ko, nl, ru, ar, sv, es, ro
);

View File

@@ -1,30 +1,3 @@
#![recursion_limit = "1024"]
use gettext_macros::init_i18n;
use rocket::launch;
#[cfg(debug_assertions)]
init_i18n!("hagrid", en, de, ja);
#[cfg(not(debug_assertions))]
init_i18n!(
"hagrid", en, de, fr, it, ja, nb, pl, tr, zh_Hans, ko, nl, ru, ar, sv, es, ro
);
mod anonymize_utils;
mod counters;
mod dump;
mod gettext_strings;
mod i18n;
mod i18n_helpers;
mod mail;
mod rate_limiter;
mod sealed_state;
mod template_helpers;
mod tokens;
mod web;
#[launch]
fn rocket() -> _ {
web::serve().expect("Rocket config must succeed")
fn main() {
hagrid::app::run()
}

View File

@@ -391,8 +391,14 @@ fn errors(
Ok(Custom(status_code, response_body))
}
pub fn serve() -> anyhow::Result<rocket::Rocket<rocket::Build>> {
rocket_factory(rocket::build())
pub fn run() -> Result<(), rocket::Error> {
let rocket = rocket_factory(rocket::build()).expect("Rocket config must succeed");
::rocket::async_main(async move {
let _rocket = rocket.ignite().await?.launch().await?;
Ok(())
})
}
compile_i18n!();