Lines
78.95 %
Functions
12.31 %
Branches
100 %
use std::sync::Arc;
use crate::AppState;
use crate::files::{S3File, list_s3};
use askama::Template;
use axum::{
extract::State,
http::StatusCode,
response::{Html, IntoResponse, Response},
};
use axum_extra::extract::CookieJar;
pub mod account;
pub mod commodity;
pub mod report;
pub mod script;
pub mod tag;
pub mod template;
pub mod transaction;
pub mod validation;
pub async fn index(cookie_jar: CookieJar) -> impl IntoResponse {
let is_logged_in = match cookie_jar.get("access_token") {
Some(cookie) => crate::auth_keys::verify(cookie.value(), crate::token::TokenType::Access)
.await
.is_some(),
None => false,
let template = IndexTemplate {
is_logged_in,
scripting_enabled: true,
HtmlTemplate(template)
}
#[derive(Template)]
#[template(path = "pages/index.html")]
struct IndexTemplate {
is_logged_in: bool,
scripting_enabled: bool,
pub async fn file_table(
State(data): State<Arc<AppState>>,
) -> Result<impl IntoResponse, StatusCode> {
let frac = State(&data).frac;
let template = FileTableTemplate {
files: list_s3(State(data)).await?,
frac,
Ok(HtmlTemplate(template))
#[template(path = "components/file-table.html")]
struct FileTableTemplate {
files: Vec<S3File>,
frac: i64,
pub async fn register() -> impl IntoResponse {
let template = RegisterTemplate {};
#[template(path = "pages/register.html")]
struct RegisterTemplate;
pub async fn login() -> impl IntoResponse {
let template = LoginTemplate {};
#[template(path = "pages/login.html")]
struct LoginTemplate;
/// A wrapper type that we'll use to encapsulate HTML parsed by askama into valid HTML for axum to serve.
struct HtmlTemplate<T>(T);
/// Allows us to convert Askama HTML templates into valid HTML for axum to serve in the response.
impl<T> IntoResponse for HtmlTemplate<T>
where
T: Template,
{
fn into_response(self) -> Response {
// Attempt to render the template with askama
match self.0.render() {
// If we're able to successfully parse and aggregate the template, serve it
Ok(html) => Html(html).into_response(),
// If we're not, return an error or some bit of fallback HTML
Err(err) => (
StatusCode::INTERNAL_SERVER_ERROR,
format!("Failed to render template. Error: {err}"),
)
.into_response(),