From 02db034d5d468d00d32db6736f6d2382a81766da Mon Sep 17 00:00:00 2001 From: Mirko von Leipzig Date: Mon, 27 Jan 2025 11:01:24 +0200 Subject: [PATCH] Simplify faucet static file handling --- CHANGELOG.md | 2 +- bin/faucet/src/errors.rs | 4 ---- bin/faucet/src/handlers.rs | 27 ++++++++++++++++++++++----- bin/faucet/src/main.rs | 9 +++++---- 4 files changed, 28 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e746dc5ba..d47c39d37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/bin/faucet/src/errors.rs b/bin/faucet/src/errors.rs index 54122e20e..476f066aa 100644 --- a/bin/faucet/src/errors.rs +++ b/bin/faucet/src/errors.rs @@ -29,9 +29,6 @@ pub enum HandlerError { #[error("invalid asset amount {requested} requested, valid options are {options:?}")] InvalidAssetAmount { requested: u64, options: Vec }, - - #[error("not found")] - NotFound, } impl HandlerError { @@ -41,7 +38,6 @@ impl HandlerError { StatusCode::BAD_REQUEST }, Self::ClientError(_) | Self::Internal(_) => StatusCode::INTERNAL_SERVER_ERROR, - Self::NotFound => StatusCode::NOT_FOUND, } } diff --git a/bin/faucet/src/handlers.rs b/bin/faucet/src/handlers.rs index a563579dc..5a6de2bc0 100644 --- a/bin/faucet/src/handlers.rs +++ b/bin/faucet/src/handlers.rs @@ -1,6 +1,6 @@ use anyhow::Context; use axum::{ - extract::{Path, State}, + extract::State, http::{Response, StatusCode}, response::IntoResponse, Json, @@ -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) -> Result { + get_static_file(state, "index.html") +} + +pub async fn get_index_js(state: State) -> Result { + get_static_file(state, "index.js") +} + +pub async fn get_index_css(state: State) -> Result { + 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, - Path(path): Path, + file: &'static str, ) -> Result { - 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) diff --git a/bin/faucet/src/main.rs b/bin/faucet/src/main.rs index f9dbc9478..35dd9be65 100644 --- a/bin/faucet/src/main.rs +++ b/bin/faucet/src/main.rs @@ -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}; @@ -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 @@ -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())