Skip to content

Commit

Permalink
Simplify faucet static file handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Mirko-von-Leipzig committed Jan 28, 2025
1 parent b4c2776 commit 02db034
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Changes

- [BREAKING] Default faucet endpoint is now public instead of localhost.
- [BREAKING] Default faucet endpoint is now public instead of localhost (#647).

## v0.7.0 (2025-01-23)

Expand Down
4 changes: 0 additions & 4 deletions bin/faucet/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,6 @@ pub enum HandlerError {

#[error("invalid asset amount {requested} requested, valid options are {options:?}")]
InvalidAssetAmount { requested: u64, options: Vec<u64> },

#[error("not found")]
NotFound,
}

impl HandlerError {
Expand All @@ -41,7 +38,6 @@ impl HandlerError {
StatusCode::BAD_REQUEST
},
Self::ClientError(_) | Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR,
Self::NotFound => StatusCode::NOT_FOUND,
}
}

Expand Down
27 changes: 22 additions & 5 deletions bin/faucet/src/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Context;
use axum::{
extract::{Path, State},
extract::State,
http::{Response, StatusCode},
response::IntoResponse,
Json,
Expand Down Expand Up @@ -116,13 +116,30 @@ pub async fn get_tokens(
.map_err(Into::into)
}

pub async fn get_static_file(
pub async fn get_index_html(state: State<FaucetState>) -> Result<impl IntoResponse, HandlerError> {
get_static_file(state, "index.html")
}

pub async fn get_index_js(state: State<FaucetState>) -> Result<impl IntoResponse, HandlerError> {
get_static_file(state, "index.js")
}

pub async fn get_index_css(state: State<FaucetState>) -> Result<impl IntoResponse, HandlerError> {
get_static_file(state, "index.css")
}

/// Returns a static file bundled with the app state.
///
/// # Panics
///
/// Panics if the file does not exist.
fn get_static_file(
State(state): State<FaucetState>,
Path(path): Path<String>,
file: &'static str,
) -> Result<impl IntoResponse, HandlerError> {
info!(target: COMPONENT, path, "Serving static file");
info!(target: COMPONENT, file, "Serving static file");

let static_file = state.static_files.get(path.as_str()).ok_or(HandlerError::NotFound)?;
let static_file = state.static_files.get(file).expect("static file not found");

Response::builder()
.status(StatusCode::OK)
Expand Down
9 changes: 5 additions & 4 deletions bin/faucet/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ use std::path::PathBuf;

use anyhow::Context;
use axum::{
extract::Path,
routing::{get, post},
Router,
};
use clap::{Parser, Subcommand};
use client::initialize_faucet_client;
use handlers::{get_index_css, get_index_html, get_index_js};
use http::HeaderValue;
use miden_lib::{account::faucets::create_basic_fungible_faucet, AuthScheme};
use miden_node_utils::{config::load_config, crypto::get_rpo_random_coin, version::LongVersion};
Expand All @@ -34,7 +34,7 @@ use tracing::info;

use crate::{
config::{FaucetConfig, DEFAULT_FAUCET_ACCOUNT_PATH},
handlers::{get_metadata, get_static_file, get_tokens},
handlers::{get_metadata, get_tokens},
};

// CONSTANTS
Expand Down Expand Up @@ -103,10 +103,11 @@ async fn main() -> anyhow::Result<()> {
info!(target: COMPONENT, %config, "Initializing server");

let app = Router::new()
.route("/", get(|state| get_static_file(state, Path("index.html".to_string()))))
.route("/", get(get_index_html))
.route("/index.js", get(get_index_js))
.route("/index.css", get(get_index_css))
.route("/get_metadata", get(get_metadata))
.route("/get_tokens", post(get_tokens))
.route("/*path", get(get_static_file))
.layer(
ServiceBuilder::new()
.layer(TraceLayer::new_for_http())
Expand Down

0 comments on commit 02db034

Please sign in to comment.