@@ -5,18 +5,18 @@ use alloy::dyn_abi::Eip712Domain;
55use anyhow;
66use axum:: extract:: MatchedPath ;
77use axum:: extract:: Request as ExtractRequest ;
8- use axum:: http:: { Method , Request } ;
98use axum:: {
9+ http:: { Method , Request } ,
10+ middleware:: { from_fn, from_fn_with_state} ,
1011 response:: IntoResponse ,
1112 routing:: { get, post} ,
12- Json , Router ,
13+ serve , Json , Router , ServiceExt ,
1314} ;
14- use axum:: { serve, ServiceExt } ;
1515use build_info:: BuildInfo ;
1616use indexer_attestation:: AttestationSigner ;
1717use indexer_monitor:: {
18- attestation_signers, dispute_manager , escrow_accounts , indexer_allocations , DeploymentDetails ,
19- EscrowAccounts , SubgraphClient ,
18+ attestation_signers, deployment_to_allocation , dispute_manager , escrow_accounts ,
19+ indexer_allocations , DeploymentDetails , EscrowAccounts , SubgraphClient ,
2020} ;
2121use prometheus:: TextEncoder ;
2222use reqwest:: StatusCode ;
@@ -30,12 +30,23 @@ use thegraph_core::{Address, Attestation};
3030use tokio:: net:: TcpListener ;
3131use tokio:: signal;
3232use tokio:: sync:: watch:: Receiver ;
33+ use tower:: ServiceBuilder ;
3334use tower_governor:: { governor:: GovernorConfigBuilder , GovernorLayer } ;
3435use tower_http:: validate_request:: ValidateRequestHeaderLayer ;
3536use tower_http:: { cors, cors:: CorsLayer , normalize_path:: NormalizePath , trace:: TraceLayer } ;
3637use tracing:: warn;
3738use tracing:: { error, info, info_span} ;
3839
40+ use crate :: metrics:: HANDLER_FAILURE ;
41+ use crate :: metrics:: HANDLER_HISTOGRAM ;
42+ use crate :: middleware:: allocation_middleware;
43+ use crate :: middleware:: deployment_middleware;
44+ use crate :: middleware:: labels_middleware;
45+ use crate :: middleware:: receipt_middleware;
46+ use crate :: middleware:: sender_middleware;
47+ use crate :: middleware:: AllocationState ;
48+ use crate :: middleware:: MetricsMiddlewareLayer ;
49+ use crate :: middleware:: SenderState ;
3950use crate :: routes:: health;
4051use crate :: routes:: request_handler;
4152use crate :: routes:: static_subgraph_request_handler;
@@ -257,7 +268,7 @@ pub async fn run(options: IndexerServiceOptions) -> Result<(), anyhow::Error> {
257268
258269 let checks = IndexerTapContext :: get_checks (
259270 database,
260- allocations,
271+ allocations. clone ( ) ,
261272 escrow_accounts. clone ( ) ,
262273 domain_separator. clone ( ) ,
263274 timestamp_error_tolerance,
@@ -276,8 +287,8 @@ pub async fn run(options: IndexerServiceOptions) -> Result<(), anyhow::Error> {
276287 attestation_signers,
277288 tap_manager,
278289 service_impl : options. service_impl ,
279- escrow_accounts,
280- domain_separator,
290+ escrow_accounts : escrow_accounts . clone ( ) ,
291+ domain_separator : domain_separator . clone ( ) ,
281292 } ) ;
282293
283294 // Rate limits by allowing bursts of 10 requests and requiring 100ms of
@@ -362,13 +373,39 @@ pub async fn run(options: IndexerServiceOptions) -> Result<(), anyhow::Error> {
362373
363374 misc_routes = misc_routes. with_state ( state. clone ( ) ) ;
364375
376+ let deployment_to_allocation = deployment_to_allocation ( allocations) ;
377+ let allocation_state = AllocationState {
378+ deployment_to_allocation,
379+ } ;
380+ let sender_state = SenderState {
381+ escrow_accounts,
382+ domain_separator,
383+ } ;
384+
385+ let service_builder = ServiceBuilder :: new ( )
386+ // inject deployment id
387+ . layer ( from_fn ( deployment_middleware) )
388+ // inject receipt
389+ . layer ( from_fn ( receipt_middleware) )
390+ // inject deployment id
391+ . layer ( from_fn_with_state ( allocation_state, allocation_middleware) )
392+ // inject sender
393+ . layer ( from_fn_with_state ( sender_state, sender_middleware) )
394+ // inject metrics labels
395+ . layer ( from_fn ( labels_middleware) )
396+ // metrics for histogram and failure
397+ . layer ( MetricsMiddlewareLayer :: new (
398+ HANDLER_HISTOGRAM . clone ( ) ,
399+ HANDLER_FAILURE . clone ( ) ,
400+ ) ) ;
401+
365402 let data_routes = Router :: new ( )
366403 . route (
367404 PathBuf :: from ( & options. config . service . url_prefix )
368405 . join ( format ! ( "{}/id/:id" , options. url_namespace) )
369406 . to_str ( )
370407 . expect ( "Failed to set up `/{url_namespace}/id/:id` route" ) ,
371- post ( request_handler) ,
408+ post ( request_handler) . route_layer ( service_builder ) ,
372409 )
373410 . with_state ( state. clone ( ) ) ;
374411
0 commit comments