From f2137bb2d1fc8ded34c50725c1b09b5f45d61e31 Mon Sep 17 00:00:00 2001 From: Tushar Mathur Date: Thu, 1 Feb 2024 13:07:27 +0530 Subject: [PATCH] chore: rename variables to app_ctx --- src/http/request_context.rs | 20 +++++++-------- src/http/request_handler.rs | 49 +++++++++++++++++-------------------- tests/graphql_spec.rs | 2 +- 3 files changed, 34 insertions(+), 37 deletions(-) diff --git a/src/http/request_context.rs b/src/http/request_context.rs index 36ee621375..e498b8d509 100644 --- a/src/http/request_context.rs +++ b/src/http/request_context.rs @@ -100,20 +100,20 @@ impl RequestContext { } impl From<&AppContext> for RequestContext { - fn from(server_ctx: &AppContext) -> Self { + fn from(app_ctx: &AppContext) -> Self { Self { - h_client: server_ctx.runtime.http.clone(), - h2_client: server_ctx.runtime.http2_only.clone(), - server: server_ctx.blueprint.server.clone(), - upstream: server_ctx.blueprint.upstream.clone(), + h_client: app_ctx.runtime.http.clone(), + h2_client: app_ctx.runtime.http2_only.clone(), + server: app_ctx.blueprint.server.clone(), + upstream: app_ctx.blueprint.upstream.clone(), req_headers: HeaderMap::new(), - http_data_loaders: server_ctx.http_data_loaders.clone(), - gql_data_loaders: server_ctx.gql_data_loaders.clone(), - cache: server_ctx.runtime.cache.clone(), - grpc_data_loaders: server_ctx.grpc_data_loaders.clone(), + http_data_loaders: app_ctx.http_data_loaders.clone(), + gql_data_loaders: app_ctx.gql_data_loaders.clone(), + cache: app_ctx.runtime.cache.clone(), + grpc_data_loaders: app_ctx.grpc_data_loaders.clone(), min_max_age: Arc::new(Mutex::new(None)), cache_public: Arc::new(Mutex::new(None)), - env_vars: server_ctx.runtime.env.clone(), + env_vars: app_ctx.runtime.env.clone(), } } } diff --git a/src/http/request_handler.rs b/src/http/request_handler.rs index 234933b5b7..5ba09096b9 100644 --- a/src/http/request_handler.rs +++ b/src/http/request_handler.rs @@ -37,19 +37,19 @@ fn not_found() -> Result> { .body(Body::empty())?) } -fn create_request_context(req: &Request, server_ctx: &AppContext) -> RequestContext { - let upstream = server_ctx.blueprint.upstream.clone(); +fn create_request_context(req: &Request, app_ctx: &AppContext) -> RequestContext { + let upstream = app_ctx.blueprint.upstream.clone(); let allowed = upstream.get_allowed_headers(); let headers = create_allowed_headers(req.headers(), &allowed); - RequestContext::from(server_ctx).req_headers(headers) + RequestContext::from(app_ctx).req_headers(headers) } fn update_cache_control_header( response: GraphQLResponse, - server_ctx: &AppContext, + app_ctx: &AppContext, req_ctx: Arc, ) -> GraphQLResponse { - if server_ctx.blueprint.server.enable_cache_control_header { + if app_ctx.blueprint.server.enable_cache_control_header { let ttl = req_ctx.get_min_max_age().unwrap_or(0); let cache_public_flag = req_ctx.is_cache_public().unwrap_or(true); return response.set_cache_control(ttl, cache_public_flag); @@ -57,29 +57,26 @@ fn update_cache_control_header( response } -pub fn update_response_headers(resp: &mut hyper::Response, server_ctx: &AppContext) { - if !server_ctx.blueprint.server.response_headers.is_empty() { +pub fn update_response_headers(resp: &mut hyper::Response, app_ctx: &AppContext) { + if !app_ctx.blueprint.server.response_headers.is_empty() { resp.headers_mut() - .extend(server_ctx.blueprint.server.response_headers.clone()); + .extend(app_ctx.blueprint.server.response_headers.clone()); } } pub async fn graphql_request( req: Request, - server_ctx: &AppContext, + app_ctx: &AppContext, ) -> Result> { - let req_ctx = Arc::new(create_request_context(&req, server_ctx)); + let req_ctx = Arc::new(create_request_context(&req, app_ctx)); let bytes = hyper::body::to_bytes(req.into_body()).await?; let request = serde_json::from_slice::(&bytes); match request { Ok(request) => { - let mut response = request - .data(req_ctx.clone()) - .execute(&server_ctx.schema) - .await; - response = update_cache_control_header(response, server_ctx, req_ctx); + let mut response = request.data(req_ctx.clone()).execute(&app_ctx.schema).await; + response = update_cache_control_header(response, app_ctx, req_ctx); let mut resp = response.to_response()?; - update_response_headers(&mut resp, server_ctx); + update_response_headers(&mut resp, app_ctx); Ok(resp) } Err(err) => { @@ -111,37 +108,37 @@ fn create_allowed_headers(headers: &HeaderMap, allowed: &BTreeSet) -> He pub async fn handle_request( req: Request, - state: Arc, + app_ctx: Arc, ) -> Result> { match *req.method() { // NOTE: // The first check for the route should be for `/graphql` // This is always going to be the most used route. hyper::Method::POST if req.uri().path() == "/graphql" => { - graphql_request::(req, state.as_ref()).await + graphql_request::(req, app_ctx.as_ref()).await } hyper::Method::POST - if state.blueprint.server.enable_showcase + if app_ctx.blueprint.server.enable_showcase && req.uri().path().ends_with("/showcase/graphql") => { let resources = ShowcaseResources { - http: state.universal_http_client.clone(), - env: None, // Disallow access to environment varialbes + http: app_ctx.universal_http_client.clone(), + env: None, // Disallow access to environment variables file: None, // Disallow local file reading // TODO: Generically accessible way to create new clean cache - cache: state.cache.clone(), + cache: app_ctx.cache.clone(), }; - let server_ctx = match showcase_get_app_ctx::(&req, resources).await? { - Ok(server_ctx) => server_ctx, + let app_ctx = match showcase_get_app_ctx::(&req, resources).await? { + Ok(app_ctx) => app_ctx, Err(res) => return Ok(res), }; - graphql_request::(req, &server_ctx).await + graphql_request::(req, &app_ctx).await } - hyper::Method::GET if state.blueprint.server.enable_graphiql => graphiql(&req), + hyper::Method::GET if app_ctx.blueprint.server.enable_graphiql => graphiql(&req), _ => not_found(), } } diff --git a/tests/graphql_spec.rs b/tests/graphql_spec.rs index 1c2dca2499..1188884adf 100644 --- a/tests/graphql_spec.rs +++ b/tests/graphql_spec.rs @@ -340,7 +340,7 @@ async fn test_execution() -> std::io::Result<()> { HeaderName::from_static("authorization"), HeaderValue::from_static("1"), ); - let req_ctx = Arc::new(RequestContext::from(&server_ctx).req_headers(headers)); + let req_ctx = Arc::new(RequestContext::from(&app_ctx).req_headers(headers)); let req = Request::from(q.query.as_str()).data(req_ctx.clone()); let res = schema.execute(req).await; let json = serde_json::to_string(&res).unwrap();