Use rust-embed for serving static files

This commit is contained in:
Simon Bruder 2024-07-14 18:31:08 +02:00
parent aa50166d31
commit 850e576cde
Signed by: simon
GPG key ID: 347FF8699CDA0776
3 changed files with 92 additions and 45 deletions

106
Cargo.lock generated
View file

@ -19,29 +19,6 @@ dependencies = [
"tracing", "tracing",
] ]
[[package]]
name = "actix-files"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0773d59061dedb49a8aed04c67291b9d8cf2fe0b60130a381aab53c6dd86e9be"
dependencies = [
"actix-http",
"actix-service",
"actix-utils",
"actix-web",
"bitflags 2.5.0",
"bytes",
"derive_more",
"futures-core",
"http-range",
"log",
"mime",
"mime_guess",
"percent-encoding",
"pin-project-lite",
"v_htmlescape",
]
[[package]] [[package]]
name = "actix-http" name = "actix-http"
version = "3.8.0" version = "3.8.0"
@ -1083,12 +1060,6 @@ dependencies = [
"itoa", "itoa",
] ]
[[package]]
name = "http-range"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "21dec9db110f5f872ed9699c3ecf50cf16f423502706ba5c72462e28d3157573"
[[package]] [[package]]
name = "httparse" name = "httparse"
version = "1.9.4" version = "1.9.4"
@ -1185,7 +1156,6 @@ dependencies = [
name = "li7y" name = "li7y"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"actix-files",
"actix-identity", "actix-identity",
"actix-session", "actix-session",
"actix-web", "actix-web",
@ -1198,7 +1168,9 @@ dependencies = [
"log", "log",
"maud", "maud",
"mime", "mime",
"mime_guess",
"printpdf", "printpdf",
"rust-embed",
"serde", "serde",
"serde_urlencoded", "serde_urlencoded",
"serde_variant", "serde_variant",
@ -1347,9 +1319,9 @@ checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]] [[package]]
name = "mime_guess" name = "mime_guess"
version = "2.0.4" version = "2.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef" checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e"
dependencies = [ dependencies = [
"mime", "mime",
"unicase", "unicase",
@ -1747,6 +1719,42 @@ dependencies = [
"zeroize", "zeroize",
] ]
[[package]]
name = "rust-embed"
version = "8.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fa66af4a4fdd5e7ebc276f115e895611a34739a9c1c01028383d612d550953c0"
dependencies = [
"actix-web",
"mime_guess",
"rust-embed-impl",
"rust-embed-utils",
"walkdir",
]
[[package]]
name = "rust-embed-impl"
version = "8.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6125dbc8867951125eec87294137f4e9c2c96566e61bf72c45095a7c77761478"
dependencies = [
"proc-macro2",
"quote",
"rust-embed-utils",
"syn 2.0.68",
"walkdir",
]
[[package]]
name = "rust-embed-utils"
version = "8.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e5347777e9aacb56039b0e1f28785929a8a3b709e87482e7442c72e7c12529d"
dependencies = [
"sha2",
"walkdir",
]
[[package]] [[package]]
name = "rustc-demangle" name = "rustc-demangle"
version = "0.1.24" version = "0.1.24"
@ -1781,6 +1789,15 @@ version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f" checksum = "f3cb5ba0dc43242ce17de99c180e96db90b235b8a9fdc9543c96d2209116bd9f"
[[package]]
name = "same-file"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
dependencies = [
"winapi-util",
]
[[package]] [[package]]
name = "scopeguard" name = "scopeguard"
version = "1.2.0" version = "1.2.0"
@ -2435,12 +2452,6 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "v_htmlescape"
version = "0.15.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e8257fbc510f0a46eb602c10215901938b5c2a7d5e70fc11483b1d3c9b5b18c"
[[package]] [[package]]
name = "vcpkg" name = "vcpkg"
version = "0.2.15" version = "0.2.15"
@ -2453,6 +2464,16 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "walkdir"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [
"same-file",
"winapi-util",
]
[[package]] [[package]]
name = "wasi" name = "wasi"
version = "0.11.0+wasi-snapshot-preview1" version = "0.11.0+wasi-snapshot-preview1"
@ -2535,6 +2556,15 @@ dependencies = [
"wasite", "wasite",
] ]
[[package]]
name = "winapi-util"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4d4cc384e1e73b93bafa6fb4f1df8c41695c8a91cf9c4c64358067d15a7b6c6b"
dependencies = [
"windows-sys 0.52.0",
]
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.48.0" version = "0.48.0"

View file

@ -10,7 +10,6 @@ edition = "2021"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
[dependencies] [dependencies]
actix-files = "0.6.6"
actix-identity = "0.7.1" actix-identity = "0.7.1"
actix-session = { version = "0.9.0", features = ["cookie-session"] } actix-session = { version = "0.9.0", features = ["cookie-session"] }
actix-web = { version = "4.8.0", features = ["cookies"] } actix-web = { version = "4.8.0", features = ["cookies"] }
@ -23,7 +22,9 @@ futures-util = "0.3.30"
log = "0.4.21" log = "0.4.21"
maud = { version = "0.26.0", features = ["actix-web"] } maud = { version = "0.26.0", features = ["actix-web"] }
mime = "0.3.17" mime = "0.3.17"
mime_guess = "2.0.5"
printpdf = "0.7.0" printpdf = "0.7.0"
rust-embed = { version = "8.5.0", features = ["actix"] }
serde = { version = "1.0.203", features = ["serde_derive"] } serde = { version = "1.0.203", features = ["serde_derive"] }
serde_urlencoded = "0.7.1" serde_urlencoded = "0.7.1"
serde_variant = "0.1.3" serde_variant = "0.1.3"

View file

@ -7,9 +7,15 @@ use std::env;
use actix_identity::IdentityMiddleware; use actix_identity::IdentityMiddleware;
use actix_session::{storage::CookieSessionStore, SessionMiddleware}; use actix_session::{storage::CookieSessionStore, SessionMiddleware};
use actix_web::middleware::ErrorHandlers; use actix_web::middleware::ErrorHandlers;
use actix_web::{cookie::Key, http::StatusCode, web, App, HttpServer}; use actix_web::{cookie::Key, http::StatusCode, web, App, HttpResponse, HttpServer};
use base64::prelude::{Engine as _, BASE64_STANDARD}; use base64::prelude::{Engine as _, BASE64_STANDARD};
use log::{debug, info, warn}; use log::{info, warn};
use mime_guess::from_path;
use rust_embed::Embed;
#[derive(Embed)]
#[folder = "static"]
struct Static;
#[actix_web::main] #[actix_web::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
@ -43,10 +49,8 @@ async fn main() -> std::io::Result<()> {
let port = env::var("LISTEN_PORT").map_or(8080, |s| { let port = env::var("LISTEN_PORT").map_or(8080, |s| {
s.parse::<u16>().expect("failed to parse LISTEN_PORT") s.parse::<u16>().expect("failed to parse LISTEN_PORT")
}); });
let static_root = env::var("STATIC_ROOT").unwrap_or("static".to_string());
info!("Starting on {address}:{port} with static files from {static_root}"); info!("Starting on {address}:{port}");
debug!("Serving static files from {static_root}");
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
@ -56,7 +60,19 @@ async fn main() -> std::io::Result<()> {
.wrap(li7y::middleware::ForceIdentity) .wrap(li7y::middleware::ForceIdentity)
.configure(li7y::api::config), .configure(li7y::api::config),
) )
.service(actix_files::Files::new("/static", &static_root)) .service(web::scope("/static").route(
"/{_:.*}",
web::get().to(|path: web::Path<String>| async {
Static::get(&path)
.map(|embedded_file| match from_path(path.into_inner()).first() {
Some(mime_type) => HttpResponse::Ok()
.content_type(mime_type)
.body(embedded_file.data),
None => HttpResponse::Ok().body(embedded_file.data),
})
.unwrap_or(HttpResponse::NotFound().body(()))
}),
))
.configure(li7y::frontend::config) .configure(li7y::frontend::config)
.wrap(ErrorHandlers::new().handler( .wrap(ErrorHandlers::new().handler(
StatusCode::UNAUTHORIZED, StatusCode::UNAUTHORIZED,