Compare commits
No commits in common. "c972fbff315f1b3d17a74c6d56bdda07cd9b815f" and "72f12b6de73ede08d495b30d2347ebac1459bc3c" have entirely different histories.
c972fbff31
...
72f12b6de7
4 changed files with 40 additions and 21 deletions
|
@ -11,7 +11,6 @@ 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"] }
|
||||||
|
|
|
@ -1,3 +1,3 @@
|
||||||
micronfig::config!(
|
micronfig::config!(
|
||||||
ACRATE_APUB_INBOX_BIND_ADDRESS: String > std::net::SocketAddr,
|
ACRATE_APUB_INBOX_BIND_ADDRESS: String,
|
||||||
);
|
);
|
||||||
|
|
|
@ -1,18 +1,38 @@
|
||||||
use axum::routing::{get, post};
|
use std::sync::Arc;
|
||||||
use acrate_utils::web_server;
|
use anyhow::Context;
|
||||||
|
use axum::Extension;
|
||||||
|
|
||||||
mod config;
|
mod config;
|
||||||
mod route;
|
mod route;
|
||||||
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() -> anyhow::Result<std::convert::Infallible> {
|
||||||
web_server!(
|
pretty_env_logger::init();
|
||||||
on: *config::ACRATE_APUB_INBOX_BIND_ADDRESS(),
|
log::debug!("Logging initialized!");
|
||||||
templates: [ ],
|
|
||||||
routes: {
|
log::trace!("Creating Minijinja environment...");
|
||||||
"/inbox" => post(route::inbox_handler),
|
let mut mj = minijinja::Environment::<'static>::new();
|
||||||
"/.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");
|
||||||
}
|
}
|
||||||
|
|
|
@ -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: {:#?}", $path);
|
log::trace!("Adding template to minijinja Environment: {:?} ← {:#?}", $mj, $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: {:#?}", $path);
|
log::trace!("Added template to minijinja Environment: {:?} ← {:#?}", $mj, $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: {:#?}", $path);
|
log::trace!("Adding route to axum Router: {:?} ← {:#?}", $router, $path);
|
||||||
let $router = $router.route($path, $route);
|
let $router = $router.route($path, $route);
|
||||||
log::trace!("Added route to axum Router: {:#?}", $path);
|
log::trace!("Added route to axum Router: {:?} ← {:#?}", $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
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue