bingus-blog/src/main.rs

183 lines
5.3 KiB
Rust
Raw Normal View History

2024-04-30 11:44:40 +03:00
#![feature(let_chains)]
2024-04-18 04:05:38 +03:00
2024-05-08 23:03:10 +03:00
mod app;
2024-04-18 04:05:38 +03:00
mod config;
mod error;
mod hash_arc_store;
2024-08-13 15:53:18 +03:00
mod helpers;
2024-04-18 04:05:38 +03:00
mod markdown_render;
2024-06-13 23:43:03 +03:00
mod platform;
2024-04-18 04:05:38 +03:00
mod post;
mod ranged_i128_visitor;
mod systemtime_as_secs;
2024-08-13 15:53:18 +03:00
mod templates;
2024-04-18 04:05:38 +03:00
use std::future::IntoFuture;
use std::net::SocketAddr;
use std::process::exit;
use std::sync::Arc;
use std::time::Duration;
use color_eyre::eyre::{self, Context};
use tokio::net::TcpListener;
2024-08-13 15:53:18 +03:00
use tokio::sync::RwLock;
2024-04-18 04:05:38 +03:00
use tokio::task::JoinSet;
2024-08-13 15:53:18 +03:00
use tokio::time::Instant;
use tokio::{select, signal};
2024-04-18 04:05:38 +03:00
use tokio_util::sync::CancellationToken;
use tracing::level_filters::LevelFilter;
2024-08-13 15:53:18 +03:00
use tracing::{debug, error, info, info_span, warn, Instrument};
2024-05-08 23:03:10 +03:00
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::{util::SubscriberInitExt, EnvFilter};
2024-04-18 04:05:38 +03:00
2024-05-08 23:03:10 +03:00
use crate::app::AppState;
use crate::post::{MarkdownPosts, PostManager};
2024-08-13 15:53:18 +03:00
use crate::templates::new_registry;
use crate::templates::watcher::watch_templates;
2024-04-18 04:05:38 +03:00
#[tokio::main]
async fn main() -> eyre::Result<()> {
color_eyre::install()?;
2024-08-13 15:53:18 +03:00
let reg = tracing_subscriber::registry();
#[cfg(feature = "tokio-console")]
let reg = reg
2024-04-18 04:05:38 +03:00
.with(
EnvFilter::builder()
2024-08-13 15:53:18 +03:00
.with_default_directive(LevelFilter::TRACE.into())
2024-04-18 04:05:38 +03:00
.from_env_lossy(),
)
2024-08-13 15:53:18 +03:00
.with(console_subscriber::spawn());
#[cfg(not(feature = "tokio-console"))]
let reg = reg.with(
EnvFilter::builder()
.with_default_directive(LevelFilter::INFO.into())
.from_env_lossy(),
);
reg.with(tracing_subscriber::fmt::layer()).init();
2024-04-18 04:05:38 +03:00
2024-05-08 23:03:10 +03:00
let config = Arc::new(
config::load()
.await
.context("couldn't load configuration")?,
);
2024-04-18 04:05:38 +03:00
2024-05-01 18:25:01 +03:00
let socket_addr = SocketAddr::new(config.http.host, config.http.port);
2024-04-18 04:05:38 +03:00
let mut tasks = JoinSet::new();
let cancellation_token = CancellationToken::new();
2024-04-18 04:05:38 +03:00
2024-08-13 15:53:18 +03:00
let start = Instant::now();
// NOTE: use tokio::task::spawn_blocking if this ever turns into a concurrent task
let mut reg =
new_registry("custom/templates").context("failed to create handlebars registry")?;
reg.register_helper("date", Box::new(helpers::date));
reg.register_helper("duration", Box::new(helpers::duration));
debug!(duration = ?start.elapsed(), "registered all templates");
let reg = Arc::new(RwLock::new(reg));
let watcher_token = cancellation_token.child_token();
let posts = Arc::new(MarkdownPosts::new(Arc::clone(&config)).await?);
2024-05-08 23:03:10 +03:00
let state = AppState {
config: Arc::clone(&config),
2024-05-09 11:30:18 +03:00
posts: Arc::clone(&posts),
2024-08-13 15:53:18 +03:00
reg: Arc::clone(&reg),
2024-04-18 04:05:38 +03:00
};
2024-08-13 15:53:18 +03:00
debug!("setting up watcher");
tasks.spawn(
watch_templates("custom/templates", watcher_token.clone(), reg)
.instrument(info_span!("custom_template_watcher")),
);
2024-05-08 23:03:10 +03:00
if config.cache.enable && config.cache.cleanup {
2024-08-13 15:53:18 +03:00
if let Some(millis) = config.cache.cleanup_interval {
2024-05-09 11:30:18 +03:00
let posts = Arc::clone(&posts);
let token = cancellation_token.child_token();
debug!("setting up cleanup task");
tasks.spawn(async move {
2024-08-13 15:53:18 +03:00
let mut interval = tokio::time::interval(Duration::from_millis(millis));
loop {
select! {
2024-08-13 15:53:18 +03:00
_ = token.cancelled() => break Ok(()),
_ = interval.tick() => {
2024-05-09 11:30:18 +03:00
posts.cleanup().await
}
}
}
});
} else {
2024-05-09 11:30:18 +03:00
posts.cleanup().await;
}
}
let app = app::new(&config).with_state(state.clone());
2024-04-18 04:05:38 +03:00
2024-05-01 18:25:01 +03:00
let listener = TcpListener::bind(socket_addr)
2024-04-18 04:05:38 +03:00
.await
2024-05-01 18:25:01 +03:00
.with_context(|| format!("couldn't listen on {}", socket_addr))?;
2024-04-18 04:05:38 +03:00
let local_addr = listener
.local_addr()
2024-04-18 19:17:33 +03:00
.context("couldn't get socket address")?;
2024-04-18 04:05:38 +03:00
info!("listening on http://{}", local_addr);
let sigint = signal::ctrl_c();
2024-06-13 23:43:03 +03:00
let sigterm = platform::sigterm();
2024-04-18 04:05:38 +03:00
let axum_token = cancellation_token.child_token();
2024-04-18 04:05:38 +03:00
let mut server = axum::serve(
listener,
app.into_make_service_with_connect_info::<SocketAddr>(),
)
.with_graceful_shutdown(async move { axum_token.cancelled().await })
.into_future();
tokio::select! {
result = &mut server => {
2024-04-18 19:17:33 +03:00
result.context("failed to serve app")?;
2024-04-18 04:05:38 +03:00
},
_ = sigint => {
info!("received SIGINT, exiting gracefully");
},
_ = sigterm => {
info!("received SIGTERM, exiting gracefully");
}
};
let cleanup = async move {
// stop tasks
cancellation_token.cancel();
2024-04-18 19:17:33 +03:00
server.await.context("failed to serve app")?;
2024-04-18 04:05:38 +03:00
while let Some(task) = tasks.join_next().await {
2024-08-13 15:53:18 +03:00
let res = task.context("failed to join task")?;
if let Err(err) = res {
error!("task failed with error: {err}");
}
2024-04-18 04:05:38 +03:00
}
2024-05-08 23:03:10 +03:00
drop(state);
2024-04-18 04:05:38 +03:00
Ok::<(), color_eyre::Report>(())
};
let sigint = signal::ctrl_c();
2024-06-13 23:43:03 +03:00
let sigterm = platform::sigterm();
2024-04-18 04:05:38 +03:00
tokio::select! {
result = cleanup => {
2024-04-18 19:17:33 +03:00
result.context("cleanup failed, oh well")?;
2024-04-18 04:05:38 +03:00
},
_ = sigint => {
warn!("received second signal, exiting");
exit(1);
},
_ = sigterm => {
warn!("received second signal, exiting");
exit(1);
}
}
Ok(())
}