Skip to content

Commit

Permalink
Added a feature to the backend to avoid building the backend if a fea…
Browse files Browse the repository at this point in the history
…ture is specified
  • Loading branch information
TylerBloom committed Sep 28, 2023
1 parent 15cdf8d commit 0053c64
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
1 change: 1 addition & 0 deletions squire_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ authors = ["TylerBloom <[email protected]>"]

[features]
db-tests = []
ignore-frontend = []

[dependencies]
# In-House deps
Expand Down
9 changes: 6 additions & 3 deletions squire_core/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
use std::{env, process::Command};

fn main() -> Result<(), i32> {
if env::var("CARGO_FEATURE_IGNORE_FRONTEND").is_ok() {
return Ok(())
}

let wd = env::var("CARGO_MANIFEST_DIR").unwrap();
let fe_path = format!("{wd}/../squire_web");

println!("cargo:rerun-if-changed={fe_path}");

// Install external dependency (in the shuttle container only)
if std::env::var("HOSTNAME")
.unwrap_or_default()
Expand Down Expand Up @@ -47,9 +53,6 @@ fn main() -> Result<(), i32> {
}
cmd.arg(format!("{fe_path}/index.html"));

println!("cargo:rerun-if-changed={fe_path}");
println!("cargo:rerun-if-changed=build.rs");

// If in debug mode, all for failed compilation of frontend.
// In release mode, require that the frontend to be functional.
if matches!(cmd.status().map(|s| s.success()), Ok(false) | Err(_)) && is_release {
Expand Down
68 changes: 45 additions & 23 deletions squire_core/src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,52 @@
use axum::response::{Html, Response};
use http::{header, HeaderMap, HeaderValue, StatusCode};
use hyper::{body::Bytes, Body};
#[allow(clippy::wildcard_imports)]
pub use frontend::*;

const INDEX_HTML: &str = include_str!("../../assets/index.html");
const APP_WASM: &[u8] = include_bytes!("../../assets/squire_web_bg.wasm");
const APP_JS: &str = include_str!("../../assets/squire_web.js");
#[cfg(feature = "ignore-frontend")]
mod frontend {
use axum::response::Html;

pub async fn landing() -> Html<&'static str> {
Html(INDEX_HTML)
}
pub async fn landing() -> Html<&'static str> {
Html("Frontend not compiled...")
}

pub async fn get_wasm() -> Response<Body> {
let bytes = Bytes::copy_from_slice(APP_WASM);
let body: Body = bytes.into();
pub async fn get_wasm() {}

Response::builder()
.header(header::CONTENT_TYPE, "application/wasm")
.body(body)
.unwrap()
pub async fn get_js() {}
}

pub async fn get_js() -> (StatusCode, HeaderMap, &'static str) {
let mut headers = HeaderMap::with_capacity(1);
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/javascript;charset=utf-8"),
);
(StatusCode::OK, headers, APP_JS)
#[cfg(all(feature = "ignore-frontend", not(debug_assertions)))]
compile_error!("In release mode, you must compile the frontend!");

#[cfg(not(feature = "ignore-frontend"))]
mod frontend {
use axum::response::{Html, Response};
use http::{header, HeaderMap, HeaderValue, StatusCode};
use hyper::{body::Bytes, Body};

const INDEX_HTML: &str = include_str!("../../assets/index.html");
const APP_WASM: &[u8] = include_bytes!("../../assets/squire_web_bg.wasm");
const APP_JS: &str = include_str!("../../assets/squire_web.js");

pub async fn landing() -> Html<&'static str> {
Html(INDEX_HTML)
}

pub async fn get_wasm() -> Response<Body> {
let bytes = Bytes::copy_from_slice(APP_WASM);
let body: Body = bytes.into();

Response::builder()
.header(header::CONTENT_TYPE, "application/wasm")
.body(body)
.unwrap()
}

pub async fn get_js() -> (StatusCode, HeaderMap, &'static str) {
let mut headers = HeaderMap::with_capacity(1);
headers.insert(
header::CONTENT_TYPE,
HeaderValue::from_static("application/javascript;charset=utf-8"),
);
(StatusCode::OK, headers, APP_JS)
}
}

0 comments on commit 0053c64

Please sign in to comment.