Get rid of the wasm_s3 build flag. Instead use the URL of the wasm app and find the data/ directory off of that, and assume only localhost has uncompressed files. https://github.com/cyipt/actdev/issues/28

This commit is contained in:
Dustin Carlino 2021-02-08 12:41:28 -08:00
parent 3cc441e1a9
commit 829c321190
5 changed files with 39 additions and 21 deletions

View File

@ -10,7 +10,7 @@ pub struct CmdArgs {
}
impl CmdArgs {
/// On native, initialize with real flags. On web, always empty.
/// On native, initialize with real flags. On web, transform URL query parameters into flags.
///
/// Calling this has the side-effect of initializing logging on both native and web. This
/// should probably be done independently, but for the moment, every app wants both.

View File

@ -7,8 +7,6 @@ edition = "2018"
[features]
native = ["reqwest", "tokio"]
wasm = ["js-sys", "wasm-bindgen", "wasm-bindgen-futures", "web-sys"]
# Just a marker to not use localhost URLs
wasm_s3 = []
# A marker to use a named release from S3 instead of dev for updating files
release_s3 = []

View File

@ -148,21 +148,22 @@ mod wasm_loader {
path: String,
on_load: Box<dyn FnOnce(&mut EventCtx, &mut A, &mut Timer, Result<T>) -> Transition<A>>,
) -> Box<dyn State<A>> {
// Note that files are only gzipepd on S3. When running locally, we just symlink the
// data/ directory, where files aren't compressed.
let url = if cfg!(feature = "wasm_s3") {
// Anytime data with a new binary format is uploaded, the web client has to be
// re-deployed too
format!(
"http://abstreet.s3-website.us-east-2.amazonaws.com/dev/data/{}.gz",
path.strip_prefix(&abstio::path("")).unwrap()
)
} else {
format!(
"http://0.0.0.0:8000/{}",
path.strip_prefix(&abstio::path("")).unwrap()
)
};
// The current URL is of the index.html page. We can find the data directory relative
// to that.
let base_url = get_base_url().unwrap();
let file_path = path.strip_prefix(&abstio::path("")).unwrap();
// Note that files are gzipped on S3 and other deployments. When running locally, we
// just symlink the data/ directory, where files aren't compressed.
let url =
if base_url.contains("http://0.0.0.0") || base_url.contains("http://localhost") {
format!("{}/{}", base_url, file_path)
} else if base_url.contains("abstreet.s3-website") {
// The directory structure on S3 is a little weird -- the base directory has
// data/ alongside game/, fifteen_min/, etc.
format!("{}/../data/{}.gz", base_url, file_path)
} else {
format!("{}/{}.gz", base_url, file_path)
};
// Make the HTTP request nonblockingly. When the response is received, send it through
// the channel.
@ -249,6 +250,23 @@ mod wasm_loader {
self.panel.draw(g);
}
}
/// Returns the base URL where the game is running, excluding query parameters and the
/// implicit index.html that might be there.
fn get_base_url() -> Result<String> {
let window = web_sys::window().ok_or(anyhow!("no window?"))?;
let url = window.location().href().map_err(|err| {
anyhow!(err
.as_string()
.unwrap_or("window.location.href failed".to_string()))
})?;
// Consider using a proper url parsing crate. This works fine for now, though.
let url = url.split("?").next().ok_or(anyhow!("empty URL?"))?;
Ok(url
.trim_end_matches("index.html")
.trim_end_matches("/")
.to_string())
}
}
pub struct FutureLoader<A, T>

View File

@ -5,7 +5,7 @@ set -e
mkdir -p abst_actdev
cd game
wasm-pack build --release --target web -- --no-default-features --features wasm,map_gui/wasm_s3
wasm-pack build --release --target web -- --no-default-features --features wasm
# Temporarily remove the symlink to the data directory
rm -f pkg/system
# Expand symlinks
@ -22,4 +22,6 @@ done
cp -Rv data/system/study_areas abst_actdev/system
gzip `find abst_actdev/ | grep bin | xargs`
echo "Go upload abst_actdev/ somewhere"
zip -r abst_actdev abst_actdev
rm -rf abst_actdev
echo "Go upload abst_actdev.zip to https://github.com/cyipt/actdev/releases"

View File

@ -7,7 +7,7 @@ set -e
# The parking mapper doesn't work on WASM yet, so don't include it
for tool in game santa fifteen_min osm_viewer; do
cd $tool
wasm-pack build --release --target web -- --no-default-features --features wasm,map_gui/wasm_s3
wasm-pack build --release --target web -- --no-default-features --features wasm
cd pkg
# Temporarily remove the symlink to the data directory; it's uploaded separately by the updater tool
rm -f system