From bdd7cfc84e84096f1c18d254c49c31638c7626d9 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Wed, 18 Dec 2024 15:59:42 +0100 Subject: [PATCH] `rdserver`: Use `web_server!` macro --- acrate_rdserver/Cargo.toml | 2 +- acrate_rdserver/src/config.rs | 2 +- acrate_rdserver/src/main.rs | 49 ++++++++++------------------------- acrate_rdserver/src/route.rs | 4 +-- 4 files changed, 17 insertions(+), 40 deletions(-) diff --git a/acrate_rdserver/Cargo.toml b/acrate_rdserver/Cargo.toml index ac749de..fe2421e 100644 --- a/acrate_rdserver/Cargo.toml +++ b/acrate_rdserver/Cargo.toml @@ -12,12 +12,12 @@ categories = ["web-programming"] [dependencies] acrate_database = { path = "../acrate_database", features = ["connect"] } acrate_rd = { path = "../acrate_rd" } +acrate_utils = { path = "../acrate_utils" } anyhow = "1.0.93" axum = { version = "0.7.7", features = ["macros"] } axum-extra = { version = "0.9.4", features = ["query"] } log = { version = "0.4.22", features = ["std", "max_level_trace", "release_max_level_debug"] } micronfig = "0.3.0" -minijinja = "2.5.0" pretty_env_logger = "0.5.0" quick-xml = { version = "0.37.0", features = ["serialize"] } serde = { version = "1.0.215", features = ["derive"] } diff --git a/acrate_rdserver/src/config.rs b/acrate_rdserver/src/config.rs index 535a5c4..786b401 100644 --- a/acrate_rdserver/src/config.rs +++ b/acrate_rdserver/src/config.rs @@ -1,3 +1,3 @@ micronfig::config!( - ACRATE_WEBFINGER_BIND_ADDRESS: String, + ACRATE_WEBFINGER_BIND_ADDRESS: String > std::net::SocketAddr, ); diff --git a/acrate_rdserver/src/main.rs b/acrate_rdserver/src/main.rs index 60dffed..f926ac2 100644 --- a/acrate_rdserver/src/main.rs +++ b/acrate_rdserver/src/main.rs @@ -1,42 +1,19 @@ -use std::sync::Arc; -use anyhow::Context; -use axum::Extension; +use axum::routing::get; +use acrate_utils::web_server; mod config; mod route; - #[tokio::main] -async fn main() -> anyhow::Result { - pretty_env_logger::init(); - log::debug!("Logging initialized!"); - - log::trace!("Creating Minijinja environment..."); - let mut mj = minijinja::Environment::<'static>::new(); - - log::trace!("Adding webfinger page to the Minijinja environment..."); - mj.add_template("rd.html.j2", include_str!("rd.html.j2")) - .expect("rd.html.j2 to be a valid Minijinja template"); - - log::trace!("Creating Axum router..."); - let app = axum::Router::new() - .route("/*path", axum::routing::get(route::webfinger_handler)) - .route("/.healthcheck", axum::routing::get(route::healthcheck_handler)) - .layer(Extension(Arc::new(mj))); - log::trace!("Axum router created successfully!"); - - log::trace!("Creating Tokio listener..."); - let bind_address = config::ACRATE_WEBFINGER_BIND_ADDRESS(); - let listener = tokio::net::TcpListener::bind(bind_address) - .await - .context("failed to bind listener to address")?; - log::trace!("Tokio listener bound to: {bind_address}"); - - log::info!("Starting server..."); - axum::serve(listener, app) - .await - .context("server exited with error")?; - - log::error!("Server exited with no error, panicking."); - panic!("server exited with no error"); +async fn main() { + web_server!( + on: *config::ACRATE_WEBFINGER_BIND_ADDRESS(), + templates: [ + "rd.html.j2" + ], + routes: { + "/*path" => get(route::webfinger_handler), + "/.healthcheck" => get(route::healthcheck_handler) + } + ); } diff --git a/acrate_rdserver/src/route.rs b/acrate_rdserver/src/route.rs index a601283..34fa201 100644 --- a/acrate_rdserver/src/route.rs +++ b/acrate_rdserver/src/route.rs @@ -1,5 +1,4 @@ use std::iter::IntoIterator; -use std::sync::Arc; use axum::Extension; use axum::extract::Path; use axum::http::{HeaderMap, Response, StatusCode}; @@ -11,6 +10,7 @@ use acrate_database::diesel::GroupedBy; use acrate_database::meta::{MetaAlias, MetaLink, MetaLinkProperty, MetaLinkTitle, MetaProperty, MetaSubject}; use acrate_rd::jrd::ResourceDescriptorLinkJRD; use acrate_rd::xrd::{ResourceDescriptorLinkXRD, ResourceDescriptorPropertyXRD, ResourceDescriptorTitleXRD}; +use acrate_utils::ext::{minijinja, ExtMj}; pub async fn healthcheck_handler() -> Result { log::debug!("Handling an healthcheck request!"); @@ -36,7 +36,7 @@ pub async fn webfinger_handler( Path(path): Path, Query(WebfingerQuery {resource, rel}): Query, headers: HeaderMap, - Extension(mj): Extension>>, + Extension(mj): ExtMj, ) -> Result, StatusCode> { log::debug!("Handling a WebFinger request!");