From c7351db0fcec8c4a1d2de2e1af381c22d40cbb78 Mon Sep 17 00:00:00 2001 From: Mirko von Leipzig Date: Fri, 24 Jan 2025 13:36:07 +0200 Subject: [PATCH] Re-add css and js faucet files Remove --testing from build package action --- bin/faucet/src/errors.rs | 4 ++++ bin/faucet/src/handlers.rs | 11 +++++++---- bin/faucet/src/main.rs | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/bin/faucet/src/errors.rs b/bin/faucet/src/errors.rs index 476f066aa..54122e20e 100644 --- a/bin/faucet/src/errors.rs +++ b/bin/faucet/src/errors.rs @@ -29,6 +29,9 @@ pub enum HandlerError { #[error("invalid asset amount {requested} requested, valid options are {options:?}")] InvalidAssetAmount { requested: u64, options: Vec }, + + #[error("not found")] + NotFound, } impl HandlerError { @@ -38,6 +41,7 @@ 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 6ff7b75d5..a563579dc 100644 --- a/bin/faucet/src/handlers.rs +++ b/bin/faucet/src/handlers.rs @@ -1,6 +1,6 @@ use anyhow::Context; use axum::{ - extract::State, + extract::{Path, State}, http::{Response, StatusCode}, response::IntoResponse, Json, @@ -116,10 +116,13 @@ pub async fn get_tokens( .map_err(Into::into) } -pub async fn get_index(state: State) -> Result { - info!(target: COMPONENT, "Serving `index.html`"); +pub async fn get_static_file( + State(state): State, + Path(path): Path, +) -> Result { + info!(target: COMPONENT, path, "Serving static file"); - let static_file = state.static_files.get("index.html").expect("index.html should be bundled"); + let static_file = state.static_files.get(path.as_str()).ok_or(HandlerError::NotFound)?; Response::builder() .status(StatusCode::OK) diff --git a/bin/faucet/src/main.rs b/bin/faucet/src/main.rs index 4b4feeb0f..ceeea2352 100644 --- a/bin/faucet/src/main.rs +++ b/bin/faucet/src/main.rs @@ -33,7 +33,7 @@ use tracing::info; use crate::{ config::{FaucetConfig, DEFAULT_FAUCET_ACCOUNT_PATH}, - handlers::{get_index, get_metadata, get_tokens}, + handlers::{get_metadata, get_static_file, get_tokens}, }; // CONSTANTS @@ -102,9 +102,9 @@ async fn main() -> anyhow::Result<()> { info!(target: COMPONENT, %config, "Initializing server"); let app = Router::new() - .route("/", get(get_index)) .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())