lyrs/src/router.rs
2024-05-20 11:35:39 -05:00

89 lines
2.2 KiB
Rust

use crate::partials::page;
use crate::{
file_watcher::FileWatcher,
prelude::*,
service::{auth, static_files},
state::State as AppState,
};
use axum::{
http::StatusCode,
response::{Html, IntoResponse},
routing::get,
Router,
};
use maud::html;
use thiserror::Error;
use tower_http::trace::TraceLayer;
use tower_livereload::LiveReloadLayer;
#[derive(Error, Debug)]
pub enum NewRouterError {
#[error("watcher error: {0}")]
Watcher(#[from] notify::Error),
}
#[derive(Error, Debug)]
pub enum ReqError {
#[error("argon2 error: {0}")]
Argon2(#[from] argon2::password_hash::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(
state: AppState,
with_watchers: bool,
) -> Result<(Router, Vec<Option<FileWatcher>>), NewRouterError> {
let live_reload_layer: Option<LiveReloadLayer> = if with_watchers {
Some(LiveReloadLayer::new())
} else {
None
};
let orl = || {
if let Some(lr) = &live_reload_layer {
Some(lr.reloader())
} else {
None
}
};
let (static_file_service, static_file_watcher) = static_files::router(orl())?;
let auth_service = auth::router().unwrap();
let mut result = Router::new()
.route("/", get(index))
.route("/about", get(about))
.nest_service("/auth", auth_service)
.nest_service("/static", static_file_service)
.layer(TraceLayer::new_for_http())
.with_state(state.clone());
if let Some(lr) = live_reload_layer {
result = result.clone().layer(lr);
}
let watchers = vec![static_file_watcher];
Ok((result, watchers))
}
async fn index() -> ReqResult<Html<String>> {
page("index", html! { "Index" })
}
async fn about() -> ReqResult<Html<String>> {
page("index", html! { "About" })
}