apub_inbox: Use web_server! macro

This commit is contained in:
Steffo 2024-12-18 16:57:24 +01:00
parent 5b62ff6ff7
commit c972fbff31
Signed by: steffo
GPG key ID: 5ADA3868646C3FC0
3 changed files with 13 additions and 32 deletions

View file

@ -11,6 +11,7 @@ categories = ["web-programming"]
[dependencies] [dependencies]
acrate_database = { path = "../acrate_database", features = ["connect"] } acrate_database = { path = "../acrate_database", features = ["connect"] }
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"] }
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"] }

View file

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

View file

@ -1,38 +1,18 @@
use std::sync::Arc; use axum::routing::{get, post};
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_APUB_INBOX_BIND_ADDRESS(),
templates: [ ],
log::trace!("Creating Minijinja environment..."); routes: {
let mut mj = minijinja::Environment::<'static>::new(); "/inbox" => post(route::inbox_handler),
"/.healthcheck" => get(route::healthcheck_handler)
log::trace!("Creating Axum router..."); }
let app = axum::Router::new() );
.route("/inbox", axum::routing::post(route::inbox_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_APUB_INBOX_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 apub_inbox web 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");
} }