From 9d7bb3c0be96f2956e9d4432077b42a86dbd60de Mon Sep 17 00:00:00 2001 From: realaravinth Date: Sat, 14 May 2022 16:00:22 +0530 Subject: [PATCH] feat: use cache busting bin and load assets generated by it --- Makefile | 12 ++++++ build.rs | 31 +------------- src/static_assets/static_files.rs | 68 ++++++++----------------------- 3 files changed, 29 insertions(+), 82 deletions(-) diff --git a/Makefile b/Makefile index 2ed872f4..9228cdb9 100644 --- a/Makefile +++ b/Makefile @@ -7,23 +7,32 @@ define frontend_env ## install frontend deps cd docs/openapi && yarn install endef +define cache_bust ## run cache_busting program + cd utils/cache-bust && cargo run +endef + default: frontend ## Build app in debug mode cargo build check: ## Check for syntax errors on all workspaces cargo check --workspace --tests --all-features + cd utils/cache-bust && cargo check --tests --all-features cd db/db-migrations && cargo check --tests --all-features cd db/db-sqlx-postgres &&\ DATABASE_URL=${POSTGRES_DATABASE_URL}\ cargo check cd db/db-core/ && cargo check +cache-bust: ## Run cache buster on static assets + $(call cache_bust) + clean: ## Delete build artifacts @cargo clean @yarn cache clean @-rm $(CLEAN_UP) coverage: migrate ## Generate code coverage report in HTML format + $(call cache_bust) cargo tarpaulin -t 1200 --out Html doc: ## Generate documentation @@ -78,6 +87,7 @@ migrate: ## Run database migrations DATABASE_URL=${POSTGRES_DATABASE_URL} cargo run release: frontend ## Build app with release optimizations + $(call cache_bust) cargo build --release run: frontend ## Run app in debug mode @@ -94,6 +104,7 @@ sqlx-offline-data: ## prepare sqlx offline data # && DATABASE_URL=${SQLITE_DATABASE_URL} cargo sqlx prepare test: frontend-test frontend ## Run all available tests + $(call cache_bust) cd db/db-sqlx-postgres &&\ DATABASE_URL=${POSTGRES_DATABASE_URL}\ cargo test --no-fail-fast @@ -101,6 +112,7 @@ test: frontend-test frontend ## Run all available tests # ./scripts/tests.sh xml-test-coverage: migrate ## Generate code coverage report in XML format + $(call cache_bust) cargo tarpaulin -t 1200 --out Xml help: ## Prints help for targets with comments diff --git a/build.rs b/build.rs index cd425aa2..b262849d 100644 --- a/build.rs +++ b/build.rs @@ -14,9 +14,9 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +use std::env; use std::process::Command; -use cache_buster::{BusterBuilder, NoHashCategory}; use sqlx::types::time::OffsetDateTime; fn main() { @@ -30,33 +30,4 @@ fn main() { let now = OffsetDateTime::now_utc().format("%y-%m-%d"); println!("cargo:rustc-env=COMPILED_DATE={}", &now); - - #[cfg(not(debug_assertions))] - cache_bust(); -} - -fn cache_bust() { - // until APPLICATION_WASM gets added to mime crate - // PR: https://github.com/hyperium/mime/pull/138 - // let types = vec![ - // mime::IMAGE_PNG, - // mime::IMAGE_SVG, - // mime::IMAGE_JPEG, - // mime::IMAGE_GIF, - // mime::APPLICATION_JAVASCRIPT, - // mime::TEXT_CSS, - // ]; - - println!("cargo:rerun-if-changed=static/cache"); - let no_hash = vec![NoHashCategory::FileExtentions(vec!["wasm"])]; - - let config = BusterBuilder::default() - .source("./static/cache/") - .result("./assets") - .no_hash(no_hash) - .follow_links(true) - .build() - .unwrap(); - - config.process().unwrap(); } diff --git a/src/static_assets/static_files.rs b/src/static_assets/static_files.rs index d6829d62..03cef73d 100644 --- a/src/static_assets/static_files.rs +++ b/src/static_assets/static_files.rs @@ -141,64 +141,28 @@ mod tests { use actix_web::http::StatusCode; use actix_web::test; - use super::*; use crate::*; #[actix_rt::test] async fn static_assets_work() { let app = get_app!().await; - let resp = test::call_service( - &app, - test::TestRequest::get().uri(*crate::JS).to_request(), - ) - .await; - assert_eq!(resp.status(), StatusCode::OK); + let urls = [ + *crate::JS, + *crate::VERIFICATIN_WIDGET_JS, + *crate::VERIFICATIN_WIDGET_CSS, + crate::FILES + .get("./static/cache/img/icon-trans.png") + .unwrap(), + "/favicon.ico", + ]; - let resp = test::call_service( - &app, - test::TestRequest::get() - .uri(*crate::VERIFICATIN_WIDGET_JS) - .to_request(), - ) - .await; - assert_eq!(resp.status(), StatusCode::OK); - - let resp = test::call_service( - &app, - test::TestRequest::get() - .uri(*crate::VERIFICATIN_WIDGET_CSS) - .to_request(), - ) - .await; - assert_eq!(resp.status(), StatusCode::OK); - - let resp = test::call_service( - &app, - test::TestRequest::get() - .uri( - crate::FILES - .get("./static/cache/img/icon-trans.png") - .unwrap(), - ) - .to_request(), - ) - .await; - assert_eq!(resp.status(), StatusCode::OK); - } - - #[actix_rt::test] - async fn favicons_work() { - assert!(Favicons::get("favicon.ico").is_some()); - - //let app = test::init_service(App::new().configure(services)).await; - let app = get_app!().await; - - let resp = test::call_service( - &app, - test::TestRequest::get().uri("/favicon.ico").to_request(), - ) - .await; - assert_eq!(resp.status(), StatusCode::OK); + for u in urls.iter() { + println!("[*] Testing static asset at URL: {u}"); + let resp = + test::call_service(&app, test::TestRequest::get().uri(u).to_request()) + .await; + assert_eq!(resp.status(), StatusCode::OK); + } } }