Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(faucet): embed static website #411

Merged
merged 4 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* [BREAKING] Configuration files with unknown properties are now rejected (#401).
* [BREAKING] Removed redundant node configuration properties (#401).
* Improve type safety of the transaction inputs nullifier mapping (#406).
* Embed the faucet's static website resources (#411).

## 0.4.0 (2024-07-04)

Expand Down
86 changes: 50 additions & 36 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion bin/faucet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ testing = ["miden-objects/testing", "miden-lib/testing"]

[dependencies]
actix-cors = "0.7"
actix-files = "0.6"
actix-web = "4.8"
actix-web-static-files = "4.0"
async-mutex = "1.4"
clap = { version = "4.5", features = ["derive"] }
derive_more = "0.99"
Expand All @@ -32,7 +32,11 @@ miden-tx = { workspace = true, features = ["concurrent"] }
rand = { version = "0.8" }
rand_chacha = "0.3"
serde = { version = "1.0", features = ["derive"] }
static-files = "0.2"
thiserror = { workspace = true }
toml = { version = "0.8" }
tonic = { workspace = true }
tracing = { workspace = true }

[build-dependencies]
static-files = "0.2"
12 changes: 12 additions & 0 deletions bin/faucet/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
use std::str::FromStr;

fn main() -> std::io::Result<()> {
// The location of our static faucet website files.
let static_dir = std::path::PathBuf::from_str(std::env!("CARGO_MANIFEST_DIR"))
.unwrap()
.join("src")
.join("static");
println!("cargo::rerun-if-changed={}", static_dir.to_str().expect("Valid utf-8"));
// This makes the static files available as an embedded resource.
static_files::resource_dir(static_dir).build()
}
16 changes: 9 additions & 7 deletions bin/faucet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ mod state;
use std::{fs::File, io::Write, path::PathBuf};

use actix_cors::Cors;
use actix_files::Files;
use actix_web::{
middleware::{DefaultHeaders, Logger},
web, App, HttpServer,
Expand Down Expand Up @@ -84,12 +83,10 @@ async fn main() -> Result<(), FaucetError> {
.wrap(DefaultHeaders::new().add(("Cache-Control", "no-cache")))
.service(get_metadata)
.service(get_tokens)
.service(
Files::new("/", "bin/faucet/src/static")
.use_etag(false)
.use_last_modified(false)
.index_file("index.html"),
)
.service(actix_web_static_files::ResourceFiles::new(
"/",
static_resources::generate(),
))
})
.bind((config.endpoint.host, config.endpoint.port))
.map_err(|err| FaucetError::StartError(err.to_string()))?
Expand Down Expand Up @@ -127,3 +124,8 @@ async fn main() -> Result<(), FaucetError> {

Ok(())
}

/// The static website files embedded by the build.rs script.
mod static_resources {
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
}
4 changes: 2 additions & 2 deletions bin/faucet/src/static/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ document.addEventListener('DOMContentLoaded', function () {
publicButton.addEventListener('click', () => {handleButtonClick(false)});

function fetchMetadata() {
fetch('http://localhost:8080/get_metadata')
fetch(window.location.href + 'get_metadata')
.then(response => response.json())
.then(data => {
faucetIdElem.textContent = data.id;
Expand Down Expand Up @@ -54,7 +54,7 @@ document.addEventListener('DOMContentLoaded', function () {

loading.style.display = 'block';
try {
const response = await fetch('http://localhost:8080/get_tokens', {
const response = await fetch(window.location.href + 'get_tokens', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ account_id: accountId, is_private_note: isPrivateNote, asset_amount: parseInt(assetSelect.value)})
Expand Down
Loading