use axum::{ extract::{Path, State}, http::{header, StatusCode}, response::IntoResponse, routing::get, Router, }; use maud::Markup; use super::templates; use crate::{ app::App, error::InternalError, repo::{channel, login::Login}, }; async fn index(State(app): State, login: Option) -> Result { match login { None => Ok(templates::unauthenticated()), Some(login) => index_authenticated(app, login).await, } } async fn index_authenticated(app: App, login: Login) -> Result { 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) -> 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, _: Login, Path(channel): Path, ) -> Result { let channel = app.index().channel(channel).await?; Ok(templates::channel(&channel)) } pub fn router() -> Router { Router::new() .route("/", get(index)) .route("/js/*path", get(js)) .route("/:channel", get(channel)) }