2024-05-15 16:48:23 -05:00
|
|
|
use crate::{
|
|
|
|
file_watcher::FileWatcher,
|
|
|
|
prelude::*,
|
|
|
|
state::{NewStateError, State as AppState},
|
|
|
|
static_files,
|
|
|
|
};
|
|
|
|
use axum::{
|
|
|
|
extract::State,
|
|
|
|
http::StatusCode,
|
|
|
|
response::{Html, IntoResponse},
|
|
|
|
routing::get,
|
|
|
|
Router,
|
|
|
|
};
|
2024-05-14 14:30:03 -05:00
|
|
|
use minijinja::context;
|
|
|
|
use tower_livereload::LiveReloadLayer;
|
|
|
|
|
2024-05-15 16:48:23 -05:00
|
|
|
use thiserror::Error;
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum NewRouterError {
|
|
|
|
#[error("new state error: {0}")]
|
|
|
|
State(#[from] NewStateError),
|
|
|
|
|
|
|
|
#[error("watcher error: {0}")]
|
|
|
|
Watcher(#[from] notify::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Error, Debug)]
|
|
|
|
pub enum ReqError {
|
|
|
|
#[error("template error: {0}")]
|
|
|
|
Template(#[from] minijinja::Error),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl IntoResponse for ReqError {
|
|
|
|
fn into_response(self) -> axum::http::Response<axum::body::Body> {
|
|
|
|
error!("webserver error: {:?}", self);
|
|
|
|
(
|
|
|
|
StatusCode::INTERNAL_SERVER_ERROR,
|
|
|
|
// TODO: don't expose raw errors over the internet?
|
|
|
|
format!("internal server error: {}", self),
|
|
|
|
)
|
|
|
|
.into_response()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type ReqResult<T> = Result<T, ReqError>;
|
|
|
|
|
|
|
|
pub async fn router(
|
|
|
|
with_watchers: bool,
|
|
|
|
) -> Result<(Router, Vec<Option<FileWatcher>>), NewRouterError> {
|
2024-05-14 14:30:03 -05:00
|
|
|
let state = AppState::try_new().await?;
|
|
|
|
|
2024-05-14 15:33:49 -05:00
|
|
|
let live_reload_layer: Option<LiveReloadLayer> = if with_watchers {
|
|
|
|
Some(LiveReloadLayer::new())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
2024-05-14 14:30:03 -05:00
|
|
|
|
2024-05-14 15:33:49 -05:00
|
|
|
let orl = || {
|
|
|
|
if let Some(lr) = &live_reload_layer {
|
|
|
|
Some(lr.reloader())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let template_file_watcher = state.clone().templates.start_watcher(orl()).await?;
|
|
|
|
let (static_file_service, static_file_watcher) = static_files::router(orl())?;
|
|
|
|
|
|
|
|
let mut result = Router::new()
|
2024-05-14 14:30:03 -05:00
|
|
|
.route("/", get(index))
|
2024-05-14 16:56:22 -05:00
|
|
|
.route("/about", get(about))
|
2024-05-14 14:30:03 -05:00
|
|
|
.nest_service("/static", static_file_service)
|
2024-05-14 15:33:49 -05:00
|
|
|
.with_state(state.clone());
|
|
|
|
|
|
|
|
if let Some(lr) = live_reload_layer {
|
|
|
|
result = result.clone().layer(lr);
|
|
|
|
}
|
|
|
|
|
|
|
|
let watchers = vec![template_file_watcher, static_file_watcher];
|
|
|
|
|
|
|
|
Ok((result, watchers))
|
2024-05-14 14:30:03 -05:00
|
|
|
}
|
|
|
|
|
2024-05-15 16:48:23 -05:00
|
|
|
async fn index(State(state): State<AppState>) -> ReqResult<Html<String>> {
|
2024-05-14 14:30:03 -05:00
|
|
|
Ok(Html(
|
|
|
|
state
|
|
|
|
.templates
|
2024-05-14 17:14:37 -05:00
|
|
|
.render("pages/index.jinja.html", context!())
|
2024-05-14 14:30:03 -05:00
|
|
|
.await?,
|
|
|
|
))
|
|
|
|
}
|
2024-05-15 16:48:23 -05:00
|
|
|
async fn about(State(state): State<AppState>) -> ReqResult<Html<String>> {
|
2024-05-14 16:56:22 -05:00
|
|
|
Ok(Html(
|
|
|
|
state
|
|
|
|
.templates
|
2024-05-14 17:14:37 -05:00
|
|
|
.render("pages/about.jinja.html", context!())
|
2024-05-14 16:56:22 -05:00
|
|
|
.await?,
|
|
|
|
))
|
|
|
|
}
|