From 08d8abdf4e17626d0c319f2904aba11ddc33d8c0 Mon Sep 17 00:00:00 2001 From: Hannes Klinckaert Date: Thu, 3 Oct 2024 14:42:34 +0200 Subject: [PATCH] zess: prodocker and static files --- Dockerfile | 2 +- vingo/Cargo.lock | 46 ++++++++++++++++++++++++++++++++++++++++++++++ vingo/Cargo.toml | 1 + vingo/src/main.rs | 15 ++++++++++++--- 4 files changed, 60 insertions(+), 4 deletions(-) diff --git a/Dockerfile b/Dockerfile index 1af3190..81de714 100644 --- a/Dockerfile +++ b/Dockerfile @@ -40,7 +40,7 @@ WORKDIR / COPY --from=build_backend target/release/vingo . COPY --from=build_frontend /dist public -ENV DEVELOPMENT=false +ENV DEVELOPMENT=FALSE EXPOSE 4000 diff --git a/vingo/Cargo.lock b/vingo/Cargo.lock index 637732e..3289edf 100644 --- a/vingo/Cargo.lock +++ b/vingo/Cargo.lock @@ -1081,6 +1081,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "http-range-header" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ce4ef31cda248bbdb6e6820603b82dfcd9e833db65a43e997a0ccec777d11fe" + [[package]] name = "httparse" version = "1.9.4" @@ -1371,6 +1377,16 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6877bb514081ee2a7ff5ef9de3281f14a4dd4bceac4c09388074a6b5df8a139a" +[[package]] +name = "mime_guess" +version = "2.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7c44f8e672c00fe5308fa235f821cb4198414e1c77935c1ab6948d3fd78550e" +dependencies = [ + "mime", + "unicase", +] + [[package]] name = "minimal-lexical" version = "0.2.1" @@ -2808,6 +2824,19 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-util" +version = "0.7.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9cf6b47b3771c49ac75ad09a6162f53ad4b8088b76ac60e8ec1455b31a189fe1" +dependencies = [ + "bytes", + "futures-core", + "futures-sink", + "pin-project-lite", + "tokio", +] + [[package]] name = "tower" version = "0.4.13" @@ -2848,10 +2877,18 @@ checksum = "1e9cd434a998747dd2c4276bc96ee2e0c7a2eadf3cae88e52be55a05fa9053f5" dependencies = [ "bitflags 2.6.0", "bytes", + "futures-util", "http", "http-body", "http-body-util", + "http-range-header", + "httpdate", + "mime", + "mime_guess", + "percent-encoding", "pin-project-lite", + "tokio", + "tokio-util", "tower-layer", "tower-service", "tracing", @@ -2994,6 +3031,15 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" 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]] name = "unicode-bidi" version = "0.3.15" diff --git a/vingo/Cargo.toml b/vingo/Cargo.toml index 3d1d361..66c7528 100644 --- a/vingo/Cargo.toml +++ b/vingo/Cargo.toml @@ -35,6 +35,7 @@ tokio = { version = "1.39.3", default-features = false, features = [ tower-http = { version = "0.5.2", default-features = false, features = [ "trace", "cors", + "fs", ] } tower-sessions = { version = "0.12.3", default-features = false, features = [ "memory-store", diff --git a/vingo/src/main.rs b/vingo/src/main.rs index 3f3575f..41c1370 100644 --- a/vingo/src/main.rs +++ b/vingo/src/main.rs @@ -2,7 +2,7 @@ mod entities; mod middleware; mod routes; -use std::sync::Arc; +use std::{env, sync::Arc}; use chrono::Local; use routes::{auth, cards, days, leaderboard, scans, seasons, settings}; @@ -15,7 +15,10 @@ use axum::{ use sea_orm::{prelude::DateTimeWithTimeZone, Database, DatabaseConnection}; use tokio::sync::Mutex; use tower_http::cors::CorsLayer; -use tower_http::trace::TraceLayer; +use tower_http::{ + services::{ServeDir, ServeFile}, + trace::TraceLayer, +}; use tower_sessions::{cookie::SameSite, MemoryStore, SessionManagerLayer}; use migration::{Migrator, MigratorTrait}; @@ -60,13 +63,19 @@ async fn main() { }; // build our application with a route - let app = Router::new() + let mut app = Router::new() .nest("/api", routes()) .layer(sess_mw) .layer(CorsLayer::very_permissive()) .layer(TraceLayer::new_for_http()) .with_state(state); + if env::var("DEVELOPMENT").unwrap_or("".into()) != "TRUE" { + app = app.fallback_service( + ServeDir::new("public").not_found_service(ServeFile::new("public/index.html")), + ); + }; + // run it let listener = tokio::net::TcpListener::bind("0.0.0.0:4000").await.unwrap();