Compare commits
3 commits
72f12b6de7
...
c972fbff31
Author | SHA1 | Date | |
---|---|---|---|
c972fbff31 | |||
5b62ff6ff7 | |||
e2687011eb |
4 changed files with 21 additions and 40 deletions
|
@ -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"] }
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
micronfig::config!(
|
||||
ACRATE_APUB_INBOX_BIND_ADDRESS: String,
|
||||
ACRATE_APUB_INBOX_BIND_ADDRESS: String > std::net::SocketAddr,
|
||||
);
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -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
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue