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]
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"] }

View file

@ -1,3 +1,3 @@
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 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<std::convert::Infallible> {
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)
}
);
}

View file

@ -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<StatusCode, StatusCode> {
log::debug!("Handling an healthcheck request!");
@ -36,7 +36,7 @@ pub async fn webfinger_handler(
Path(path): Path<String>,
Query(WebfingerQuery {resource, rel}): Query<WebfingerQuery>,
headers: HeaderMap,
Extension(mj): Extension<Arc<minijinja::Environment<'static>>>,
Extension(mj): ExtMj,
) -> Result<Response<String>, StatusCode> {
log::debug!("Handling a WebFinger request!");