Skip to content

Commit

Permalink
naming
Browse files Browse the repository at this point in the history
  • Loading branch information
shakyShane committed Dec 25, 2024
1 parent 4cd17c3 commit f82a924
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 17 deletions.
21 changes: 21 additions & 0 deletions crates/bsnext_core/src/dynamic_query_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use axum::extract::{Query, Request};
use axum::middleware::Next;
use axum::response::IntoResponse;
use std::convert::Infallible;
use std::time::Duration;
use tokio::time::sleep;

#[derive(Debug, serde::Deserialize)]
pub struct DynamicQueryParams {
/// Allow a request to have a ?bslive.delay.ms=200 style param to simulate a TTFB delay
#[serde(rename = "bslive.delay.ms")]
delay: Option<u64>,
}

pub async fn dynamic_query_params_handler(req: Request, next: Next) -> impl IntoResponse {
if let Ok(Query(DynamicQueryParams { delay: Some(ms) })) = Query::try_from_uri(req.uri()) {
sleep(Duration::from_millis(ms)).await;
}
let res = next.run(req).await;
Ok::<_, Infallible>(res)
}
1 change: 1 addition & 0 deletions crates/bsnext_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod server;
pub mod servers_supervisor;

pub mod dir_loader;
mod dynamic_query_params;
pub mod export;
pub mod handler_stack;
pub mod handlers;
Expand Down
21 changes: 4 additions & 17 deletions crates/bsnext_core/src/optional_layers.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use axum::extract::{Query, Request, State};
use crate::dynamic_query_params;
use axum::extract::{Request, State};
use axum::middleware::{map_response_with_state, Next};
use axum::response::{IntoResponse, Response};
use axum::routing::MethodRouter;
Expand All @@ -7,6 +8,7 @@ use axum_extra::middleware::option_layer;
use bsnext_input::route::{CompType, CompressionOpts, CorsOpts, DelayKind, DelayOpts, Opts};
use bsnext_resp::cache_opts::CacheOpts;
use bsnext_resp::{response_modifications_layer, InjectHandling};
use dynamic_query_params::dynamic_query_params_handler;
use http::header::{CACHE_CONTROL, EXPIRES, PRAGMA};
use http::{HeaderName, HeaderValue};
use std::collections::BTreeMap;
Expand Down Expand Up @@ -55,7 +57,7 @@ pub fn optional_layers(app: MethodRouter, opts: &Opts) -> MethodRouter {
});

let optional_stack = ServiceBuilder::new()
.layer(middleware::from_fn(delay_mw_dynamic))
.layer(middleware::from_fn(dynamic_query_params_handler))
.layer(option_layer(inject_layer))
.layer(option_layer(prevent_cache_headers_layer))
.layer(option_layer(set_response_headers_layer))
Expand Down Expand Up @@ -90,21 +92,6 @@ async fn delay_mw(
}
}

#[derive(Debug, serde::Deserialize)]
struct DynamicQueryParams {
/// Allow a request to have a ?bslive.delay.ms=200 style param to simulate a TTFB delay
#[serde(rename = "bslive.delay.ms")]
delay: Option<u64>,
}

async fn delay_mw_dynamic(req: Request, next: Next) -> impl IntoResponse {
if let Ok(Query(DynamicQueryParams { delay: Some(ms) })) = Query::try_from_uri(req.uri()) {
sleep(Duration::from_millis(ms)).await;
}
let res = next.run(req).await;
Ok::<_, Infallible>(res)
}

async fn set_resp_headers_from_strs<B>(
State(header_map): State<BTreeMap<String, String>>,
mut response: Response<B>,
Expand Down

0 comments on commit f82a924

Please sign in to comment.