Compare commits

...

3 commits

4 changed files with 21 additions and 40 deletions

View file

@ -11,6 +11,7 @@ categories = ["web-programming"]
[dependencies]
acrate_database = { path = "../acrate_database", features = ["connect"] }
acrate_utils = { path = "../acrate_utils" }
anyhow = "1.0.93"
axum = { version = "0.7.7", features = ["macros"] }
log = { version = "0.4.22", features = ["std", "max_level_trace", "release_max_level_debug"] }

View file

@ -1,3 +1,3 @@
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 anyhow::Context;
use axum::Extension;
use axum::routing::{get, post};
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!("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");
async fn main() {
web_server!(
on: *config::ACRATE_APUB_INBOX_BIND_ADDRESS(),
templates: [ ],
routes: {
"/inbox" => post(route::inbox_handler),
"/.healthcheck" => get(route::healthcheck_handler)
}
);
}

View file

@ -7,10 +7,10 @@ pub mod run;
#[macro_export]
macro_rules! add_minijinja_template {
($mj:ident, $path:literal) => {
log::trace!("Adding template to minijinja Environment: {:?} ← {:#?}", $mj, $path);
log::trace!("Adding template to minijinja Environment: {:#?}", $path);
$mj.add_template($path, include_str!($path))
.expect(concat!("Invalid Minijinja template: ", $path));
log::trace!("Added template to minijinja Environment: {:?} ← {:#?}", $mj, $path);
log::trace!("Added template to minijinja Environment: {:#?}", $path);
};
}
@ -18,9 +18,9 @@ macro_rules! add_minijinja_template {
#[macro_export]
macro_rules! add_axum_route {
($router:ident, $path:literal, $route:expr) => {
log::trace!("Adding route to axum Router: {:?} ← {:#?}", $router, $path);
log::trace!("Adding route to axum Router: {:#?}", $path);
let $router = $router.route($path, $route);
log::trace!("Added route to axum Router: {:?} ← {:#?}", $router, $path);
log::trace!("Added route to axum Router: {:#?}", $path);
};
}
@ -29,10 +29,10 @@ macro_rules! web_server {
(
on: $socket_addr:expr,
templates: [
$( $template_path:literal ),+
$( $template_path:literal ),*
],
routes: {
$( $path:literal => $route:expr ),+
$( $path:literal => $route:expr ),*
}
) => {
$crate::init::init_logging();
@ -40,11 +40,11 @@ macro_rules! web_server {
let mut mj = $crate::init::init_minijinja();
$(
$crate::add_minijinja_template!(mj, $template_path);
)+
)*
let router = $crate::init::init_router(mj);
$(
$crate::add_axum_route!(router, $path, $route);
)+
)*
$crate::run::run_server(listener, router).await
};
}