Add static file serving

This commit is contained in:
Simon Bruder 2024-06-25 13:30:51 +02:00
parent f4ba4e9733
commit 83eaf1a5f5
Signed by: simon
GPG key ID: 347FF8699CDA0776
4 changed files with 61 additions and 3 deletions

55
Cargo.lock generated
View file

@ -19,6 +19,29 @@ 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",
"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"
@ -734,6 +757,12 @@ 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"
@ -809,6 +838,7 @@ checksum = "d4345964bb142484797b161f473a503a434de77149dd8c7427788c6e13379388"
name = "li7y" name = "li7y"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"actix-files",
"actix-web", "actix-web",
"diesel", "diesel",
"diesel_migrations", "diesel_migrations",
@ -890,6 +920,16 @@ version = "0.3.17"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a"
[[package]]
name = "mime_guess"
version = "2.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4192263c238a5f0d0c6bfd21f336a313a4ce1c450542449ca191bb657b4642ef"
dependencies = [
"mime",
"unicase",
]
[[package]] [[package]]
name = "miniz_oxide" name = "miniz_oxide"
version = "0.7.4" version = "0.7.4"
@ -1401,6 +1441,15 @@ version = "1.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
[[package]]
name = "unicase"
version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7d2d4dafb69621809a81864c9c1b864479e1235c0dd4e199924b9742439ed89"
dependencies = [
"version_check",
]
[[package]] [[package]]
name = "unicode-bidi" name = "unicode-bidi"
version = "0.3.15" version = "0.3.15"
@ -1449,6 +1498,12 @@ 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"

View file

@ -10,6 +10,7 @@ edition = "2021"
license = "AGPL-3.0-or-later" license = "AGPL-3.0-or-later"
[dependencies] [dependencies]
actix-files = "0.6.6"
actix-web = "4.8.0" actix-web = "4.8.0"
diesel = { version = "2.2.1", features = ["postgres", "r2d2", "uuid"] } diesel = { version = "2.2.1", features = ["postgres", "r2d2", "uuid"] }
diesel_migrations = { version = "2.2.0", features = ["postgres"] } diesel_migrations = { version = "2.2.0", features = ["postgres"] }

View file

@ -8,7 +8,7 @@ use actix_web::{web, App, HttpServer};
use diesel::prelude::*; use diesel::prelude::*;
use diesel::r2d2; use diesel::r2d2;
use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness}; use diesel_migrations::{embed_migrations, EmbeddedMigrations, MigrationHarness};
use log::info; use log::{debug, info};
pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!(); pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!();
@ -32,13 +32,15 @@ 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}"); info!("Starting on {address}:{port} with static files from {static_root}");
debug!("Serving static files from {static_root}");
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.app_data(web::Data::new(pool.clone())) .app_data(web::Data::new(pool.clone()))
.service(web::scope("/api").configure(li7y::api::config)) .service(web::scope("/api/v1").configure(li7y::api::v1::config))
.service(actix_files::Files::new("/static", &static_root)) .service(actix_files::Files::new("/static", &static_root))
}) })
.bind((address, port))? .bind((address, port))?

0
static/.gitkeep Normal file
View file