use askama_actix responders so we get the correct content types/other headers for free

This commit is contained in:
xenofem 2022-06-16 19:48:10 -04:00
parent 5fdbe8dfee
commit 78cd45d68a
3 changed files with 37 additions and 34 deletions

View file

@ -6,12 +6,13 @@ mod zip;
use std::{fmt::Debug, path::PathBuf, str::FromStr};
use actix_http::StatusCode;
use actix_web::{
error::InternalError, get, middleware::Logger, post, web, App, HttpRequest, HttpResponse,
HttpServer, Responder,
};
use actix_web_actors::ws;
use askama::Template;
use askama_actix::{Template, TemplateToResponse};
use bytesize::ByteSize;
use log::{error, warn};
use serde::{Deserialize, Serialize};
@ -55,19 +56,15 @@ pub fn log_auth_failure(ip_addr: &str) {
#[derive(Template)]
#[template(path = "index.html")]
struct IndexPage<'a> {
cachebuster: &'a str,
struct IndexPage {
cachebuster: String,
}
#[get("/")]
async fn index(data: web::Data<AppState>) -> impl Responder {
HttpResponse::Ok().body(
IndexPage {
cachebuster: &data.config.cachebuster,
}
.render()
.unwrap(),
)
IndexPage {
cachebuster: data.config.cachebuster.clone(),
}
}
#[derive(Deserialize)]
@ -129,19 +126,16 @@ async fn handle_download(
.into_response(&req))
} else {
let offsets = info.contents.as_deref().map(zip::file_data_offsets);
Ok(HttpResponse::Ok().body(
DownloadPage {
info: DownloadInfo {
file: info,
code: code.clone(),
available: file.metadata().await?.len(),
offsets,
},
cachebuster: &data.config.cachebuster,
}
.render()
.unwrap(),
))
Ok(DownloadPage {
info: DownloadInfo {
file: info,
code: code.clone(),
available: file.metadata().await?.len(),
offsets,
},
cachebuster: &data.config.cachebuster,
}
.to_response())
}
}
@ -188,17 +182,13 @@ fn not_found<T>(req: HttpRequest, data: web::Data<AppState>, report: bool) -> ac
let ip_addr = get_ip_addr(&req, data.config.reverse_proxy);
log_auth_failure(&ip_addr);
}
Err(InternalError::from_response(
"Download not found",
HttpResponse::NotFound().body(
NotFoundPage {
cachebuster: &data.config.cachebuster,
}
.render()
.unwrap(),
),
)
.into())
let mut resp = NotFoundPage {
cachebuster: &data.config.cachebuster,
}
.to_response();
*resp.status_mut() = StatusCode::NOT_FOUND;
Err(InternalError::from_response("Download not found", resp).into())
}
#[get("/upload")]