Compare commits

...

3 commits

4 changed files with 21 additions and 40 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");
} }

View file

@ -7,10 +7,10 @@ pub mod run;
#[macro_export] #[macro_export]
macro_rules! add_minijinja_template { macro_rules! add_minijinja_template {
($mj:ident, $path:literal) => { ($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)) $mj.add_template($path, include_str!($path))
.expect(concat!("Invalid Minijinja template: ", $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_export]
macro_rules! add_axum_route { macro_rules! add_axum_route {
($router:ident, $path:literal, $route:expr) => { ($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); 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, on: $socket_addr:expr,
templates: [ templates: [
$( $template_path:literal ),+ $( $template_path:literal ),*
], ],
routes: { routes: {
$( $path:literal => $route:expr ),+ $( $path:literal => $route:expr ),*
} }
) => { ) => {
$crate::init::init_logging(); $crate::init::init_logging();
@ -40,11 +40,11 @@ macro_rules! web_server {
let mut mj = $crate::init::init_minijinja(); let mut mj = $crate::init::init_minijinja();
$( $(
$crate::add_minijinja_template!(mj, $template_path); $crate::add_minijinja_template!(mj, $template_path);
)+ )*
let router = $crate::init::init_router(mj); let router = $crate::init::init_router(mj);
$( $(
$crate::add_axum_route!(router, $path, $route); $crate::add_axum_route!(router, $path, $route);
)+ )*
$crate::run::run_server(listener, router).await $crate::run::run_server(listener, router).await
}; };
} }