feat: use cache busting bin and load assets generated by it

This commit is contained in:
realaravinth 2022-05-14 16:00:22 +05:30
parent 0593e254bb
commit 9d7bb3c0be
No known key found for this signature in database
GPG key ID: AD9F0F08E855ED88
3 changed files with 29 additions and 82 deletions

View file

@ -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

View file

@ -14,9 +14,9 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
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();
}

View file

@ -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);
}
}
}