diff options
Diffstat (limited to 'src/index/routes.rs')
| -rw-r--r-- | src/index/routes.rs | 82 |
1 files changed, 0 insertions, 82 deletions
diff --git a/src/index/routes.rs b/src/index/routes.rs deleted file mode 100644 index 37f6dc9..0000000 --- a/src/index/routes.rs +++ /dev/null @@ -1,82 +0,0 @@ -use axum::{ - extract::{Path, State}, - http::{header, StatusCode}, - response::{IntoResponse, Response}, - routing::get, - Router, -}; -use maud::Markup; - -use super::{app, templates}; -use crate::{ - app::App, - error::InternalError, - repo::{channel, login::Login}, -}; - -async fn index(State(app): State<App>, login: Option<Login>) -> Result<Markup, InternalError> { - match login { - None => Ok(templates::unauthenticated()), - Some(login) => index_authenticated(app, login).await, - } -} - -async fn index_authenticated(app: App, login: Login) -> Result<Markup, InternalError> { - let channels = app.channels().all().await?; - - Ok(templates::authenticated(login, &channels)) -} - -#[derive(rust_embed::Embed)] -#[folder = "js"] -struct Js; - -async fn js(Path(path): Path<String>) -> impl IntoResponse { - let mime = mime_guess::from_path(&path).first_or_octet_stream(); - - match Js::get(&path) { - Some(file) => ( - StatusCode::OK, - [(header::CONTENT_TYPE, mime.as_ref())], - file.data, - ) - .into_response(), - None => (StatusCode::NOT_FOUND, "").into_response(), - } -} - -async fn channel( - State(app): State<App>, - _: Login, - Path(channel): Path<channel::Id>, -) -> Result<Markup, ChannelError> { - let channel = app - .index() - .channel(&channel) - .await - // impl From would work here, but it'd take more code. - .map_err(ChannelError)?; - Ok(templates::channel(&channel)) -} - -#[derive(Debug)] -struct ChannelError(app::Error); - -impl IntoResponse for ChannelError { - fn into_response(self) -> Response { - let Self(error) = self; - match error { - not_found @ app::Error::ChannelNotFound(_) => { - (StatusCode::NOT_FOUND, not_found.to_string()).into_response() - } - app::Error::DatabaseError(error) => InternalError::from(error).into_response(), - } - } -} - -pub fn router() -> Router<App> { - Router::new() - .route("/", get(index)) - .route("/js/*path", get(js)) - .route("/:channel", get(channel)) -} |
