Skip to content

Commit

Permalink
Make content_length_limit configurable (#24)
Browse files Browse the repository at this point in the history
  • Loading branch information
fleupold authored Nov 25, 2023
1 parent c51d065 commit a38f77d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ ETHERSCAN_KEY=
API_KEY=
# Port to run the simulator on, defaults to 8080
PORT=
# Maximum size for incoming requests (in KB), defaults to 16
MAX_REQUEST_SIZE=
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub struct Config {
pub fork_url: Option<String>,
pub etherscan_key: Option<String>,
pub api_key: Option<String>,
pub max_request_size: u64,
}

pub fn config() -> Config {
Expand All @@ -24,12 +25,18 @@ fn load_config() -> Config {
.ok()
.filter(|k| !k.is_empty());
let api_key = std::env::var("API_KEY").ok().filter(|k| !k.is_empty());
let max_request_size = std::env::var("MAX_REQUEST_SIZE")
.unwrap_or("16".to_string())
.parse::<u64>()
.expect("MAX_REQUEST_SIZE must be a valid u64")
* 1024;

Config {
fork_url,
port,
etherscan_key,
api_key,
max_request_size,
}
}

Expand Down
20 changes: 11 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ pub fn simulate_routes(
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
simulate(config.clone())
.or(simulate_bundle(config.clone()))
.or(simulate_stateful_new(config, state.clone()))
.or(simulate_stateful_new(config.clone(), state.clone()))
.or(simulate_stateful_end(state.clone()))
.or(simulate_stateful(state))
.or(simulate_stateful(config, state))
}

/// POST /simulate
pub fn simulate(config: Config) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
warp::path!("simulate")
.and(warp::post())
.and(json_body::<SimulationRequest>())
.and(json_body::<SimulationRequest>(&config))
.and(with_config(config))
.and_then(simulation::simulate)
}
Expand All @@ -45,7 +45,7 @@ pub fn simulate_bundle(
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
warp::path!("simulate-bundle")
.and(warp::post())
.and(json_body())
.and(json_body(&config))
.and(with_config(config))
.and_then(simulation::simulate_bundle)
}
Expand All @@ -57,7 +57,7 @@ pub fn simulate_stateful_new(
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
warp::path!("simulate-stateful")
.and(warp::post())
.and(json_body::<StatefulSimulationRequest>())
.and(json_body::<StatefulSimulationRequest>(&config))
.and(with_config(config))
.and(with_state(state))
.and_then(simulation::simulate_stateful_new)
Expand All @@ -75,11 +75,12 @@ pub fn simulate_stateful_end(

/// POST /simulate-stateful/{statefulSimulationId}
pub fn simulate_stateful(
config: Config,
state: Arc<SharedSimulationState>,
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
warp::path!("simulate-stateful" / Uuid)
.and(warp::post())
.and(json_body())
.and(json_body(&config))
.and(with_state(state))
.and_then(simulation::simulate_stateful)
}
Expand All @@ -97,7 +98,8 @@ fn with_state(
warp::any().map(move || state.clone())
}

fn json_body<T: DeserializeOwned + Send>() -> impl Filter<Extract = (T,), Error = Rejection> + Clone
{
warp::body::content_length_limit(1024 * 16).and(warp::body::json())
fn json_body<T: DeserializeOwned + Send>(
config: &Config,
) -> impl Filter<Extract = (T,), Error = Rejection> + Clone {
warp::body::content_length_limit(config.max_request_size).and(warp::body::json())
}

0 comments on commit a38f77d

Please sign in to comment.