rdserver: Use web_server! macro

This commit is contained in:
Steffo 2024-12-18 15:59:42 +01:00
parent 6cc87e896d
commit bdd7cfc84e
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
4 changed files with 17 additions and 40 deletions

View file

@ -12,12 +12,12 @@ categories = ["web-programming"]
[dependencies] [dependencies]
acrate_database = { path = "../acrate_database", features = ["connect"] } acrate_database = { path = "../acrate_database", features = ["connect"] }
acrate_rd = { path = "../acrate_rd" } acrate_rd = { path = "../acrate_rd" }
acrate_utils = { path = "../acrate_utils" }
anyhow = "1.0.93" anyhow = "1.0.93"
axum = { version = "0.7.7", features = ["macros"] } axum = { version = "0.7.7", features = ["macros"] }
axum-extra = { version = "0.9.4", features = ["query"] } axum-extra = { version = "0.9.4", features = ["query"] }
log = { version = "0.4.22", features = ["std", "max_level_trace", "release_max_level_debug"] } log = { version = "0.4.22", features = ["std", "max_level_trace", "release_max_level_debug"] }
micronfig = "0.3.0" micronfig = "0.3.0"
minijinja = "2.5.0"
pretty_env_logger = "0.5.0" pretty_env_logger = "0.5.0"
quick-xml = { version = "0.37.0", features = ["serialize"] } quick-xml = { version = "0.37.0", features = ["serialize"] }
serde = { version = "1.0.215", features = ["derive"] } serde = { version = "1.0.215", features = ["derive"] }

View file

@ -1,3 +1,3 @@
micronfig::config!( micronfig::config!(
ACRATE_WEBFINGER_BIND_ADDRESS: String, ACRATE_WEBFINGER_BIND_ADDRESS: String > std::net::SocketAddr,
); );

View file

@ -1,42 +1,19 @@
use std::sync::Arc; use axum::routing::get;
use anyhow::Context; use acrate_utils::web_server;
use axum::Extension;
mod config; mod config;
mod route; mod route;
#[tokio::main] #[tokio::main]
async fn main() -> anyhow::Result<std::convert::Infallible> { async fn main() {
pretty_env_logger::init(); web_server!(
log::debug!("Logging initialized!"); on: *config::ACRATE_WEBFINGER_BIND_ADDRESS(),
templates: [
log::trace!("Creating Minijinja environment..."); "rd.html.j2"
let mut mj = minijinja::Environment::<'static>::new(); ],
routes: {
log::trace!("Adding webfinger page to the Minijinja environment..."); "/*path" => get(route::webfinger_handler),
mj.add_template("rd.html.j2", include_str!("rd.html.j2")) "/.healthcheck" => get(route::healthcheck_handler)
.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");
} }

View file

@ -1,5 +1,4 @@
use std::iter::IntoIterator; use std::iter::IntoIterator;
use std::sync::Arc;
use axum::Extension; use axum::Extension;
use axum::extract::Path; use axum::extract::Path;
use axum::http::{HeaderMap, Response, StatusCode}; 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_database::meta::{MetaAlias, MetaLink, MetaLinkProperty, MetaLinkTitle, MetaProperty, MetaSubject};
use acrate_rd::jrd::ResourceDescriptorLinkJRD; use acrate_rd::jrd::ResourceDescriptorLinkJRD;
use acrate_rd::xrd::{ResourceDescriptorLinkXRD, ResourceDescriptorPropertyXRD, ResourceDescriptorTitleXRD}; use acrate_rd::xrd::{ResourceDescriptorLinkXRD, ResourceDescriptorPropertyXRD, ResourceDescriptorTitleXRD};
use acrate_utils::ext::{minijinja, ExtMj};
pub async fn healthcheck_handler() -> Result<StatusCode, StatusCode> { pub async fn healthcheck_handler() -> Result<StatusCode, StatusCode> {
log::debug!("Handling an healthcheck request!"); log::debug!("Handling an healthcheck request!");
@ -36,7 +36,7 @@ pub async fn webfinger_handler(
Path(path): Path<String>, Path(path): Path<String>,
Query(WebfingerQuery {resource, rel}): Query<WebfingerQuery>, Query(WebfingerQuery {resource, rel}): Query<WebfingerQuery>,
headers: HeaderMap, headers: HeaderMap,
Extension(mj): Extension<Arc<minijinja::Environment<'static>>>, Extension(mj): ExtMj,
) -> Result<Response<String>, StatusCode> { ) -> Result<Response<String>, StatusCode> {
log::debug!("Handling a WebFinger request!"); log::debug!("Handling a WebFinger request!");