Compare commits

...

2 commits

4 changed files with 46 additions and 43 deletions

12
Cargo.lock generated
View file

@ -320,6 +320,17 @@ dependencies = [
"askama_shared", "askama_shared",
] ]
[[package]]
name = "askama_actix"
version = "0.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c52f74f8382a142ecfc052100b21abc33f2c069e20fe345808e7ed914b179449"
dependencies = [
"actix-web",
"askama",
"askama_shared",
]
[[package]] [[package]]
name = "askama_derive" name = "askama_derive"
version = "0.11.2" version = "0.11.2"
@ -1582,6 +1593,7 @@ dependencies = [
"actix-web", "actix-web",
"actix-web-actors", "actix-web-actors",
"askama", "askama",
"askama_actix",
"bytes", "bytes",
"bytesize", "bytesize",
"crc32fast", "crc32fast",

View file

@ -14,6 +14,7 @@ actix-http = "3.0.4"
actix-web = "4.0.1" actix-web = "4.0.1"
actix-web-actors = "4.1.0" actix-web-actors = "4.1.0"
askama = "0.11.1" askama = "0.11.1"
askama_actix = "0.13"
bytes = "1.1.0" bytes = "1.1.0"
bytesize = "1.1.0" bytesize = "1.1.0"
crc32fast = "1.3.2" crc32fast = "1.3.2"

View file

@ -33,11 +33,11 @@
}, },
"nixpkgs": { "nixpkgs": {
"locked": { "locked": {
"lastModified": 1653060744, "lastModified": 1655306633,
"narHash": "sha256-kfRusllRumpt33J1hPV+CeCCylCXEU7e0gn2/cIM7cY=", "narHash": "sha256-nv4FfWWV/dEelByjXJtJkoDPOHIsKfLq50RN3Hqq5Yk=",
"owner": "NixOS", "owner": "NixOS",
"repo": "nixpkgs", "repo": "nixpkgs",
"rev": "dfd82985c273aac6eced03625f454b334daae2e8", "rev": "b1957596ff1c7aa8c55c4512b7ad1c9672502e8e",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -77,11 +77,11 @@
"nixpkgs": "nixpkgs_2" "nixpkgs": "nixpkgs_2"
}, },
"locked": { "locked": {
"lastModified": 1653273659, "lastModified": 1655347556,
"narHash": "sha256-dHXYaNL1axhZZyiZXxt1WKhvZrYXq7bjCs3y5VjgyGI=", "narHash": "sha256-JZ06EaeHi9sbbO3n8qYZ8KzDfSbDlPVRHI6Pw4sAxRE=",
"owner": "oxalica", "owner": "oxalica",
"repo": "rust-overlay", "repo": "rust-overlay",
"rev": "0fa3e01da1ce98e3b40063b8e2678095943402b1", "rev": "1cc3dc5aec863c2f724a46f7086fb011004a4e6e",
"type": "github" "type": "github"
}, },
"original": { "original": {
@ -92,11 +92,11 @@
}, },
"utils": { "utils": {
"locked": { "locked": {
"lastModified": 1652776076, "lastModified": 1653893745,
"narHash": "sha256-gzTw/v1vj4dOVbpBSJX4J0DwUR6LIyXo7/SuuTJp1kM=", "narHash": "sha256-0jntwV3Z8//YwuOjzhV2sgJJPt+HY6KhU7VZUL0fKZQ=",
"owner": "numtide", "owner": "numtide",
"repo": "flake-utils", "repo": "flake-utils",
"rev": "04c1b180862888302ddfb2e3ad9eaa63afc60cf8", "rev": "1ed9fb1935d260de5fe1c2f7ee0ebaae17ed2fa1",
"type": "github" "type": "github"
}, },
"original": { "original": {

View file

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