From 4a540475c2832dd29756e3cc974301daa77652c0 Mon Sep 17 00:00:00 2001 From: amit Date: Tue, 30 Apr 2024 13:43:52 +0530 Subject: [PATCH] feat: migrate playground --- src/cli/server/mod.rs | 1 + src/http/mod.rs | 2 +- src/http/request_handler.rs | 62 +------------------------------ tailcall-cloudflare/src/handle.rs | 12 +----- 4 files changed, 5 insertions(+), 72 deletions(-) diff --git a/src/cli/server/mod.rs b/src/cli/server/mod.rs index efce6d98c6a..d5c571a4d25 100644 --- a/src/cli/server/mod.rs +++ b/src/cli/server/mod.rs @@ -16,6 +16,7 @@ fn log_launch_and_open_browser(sc: &ServerConfig) { ); if sc.graphiql() { let url = sc.graphiql_url(); + let url = format!("https://tailcall.run/playground/?u={}/graphql", url); tracing::info!("🌍 Playground: {}", url); let _ = webbrowser::open(url.as_str()); diff --git a/src/http/mod.rs b/src/http/mod.rs index ba8c00d7fda..5e18cc7d1bf 100644 --- a/src/http/mod.rs +++ b/src/http/mod.rs @@ -15,7 +15,7 @@ pub use data_loader::*; pub use data_loader_request::*; pub use method::Method; pub use request_context::RequestContext; -pub use request_handler::{graphiql, handle_request, API_URL_PREFIX}; +pub use request_handler::{handle_request, API_URL_PREFIX}; pub use request_template::RequestTemplate; pub use response::*; diff --git a/src/http/request_handler.rs b/src/http/request_handler.rs index 36e6ce078c5..862de24a409 100644 --- a/src/http/request_handler.rs +++ b/src/http/request_handler.rs @@ -1,17 +1,14 @@ -use std::borrow::Cow; use std::collections::BTreeSet; use std::ops::Deref; use std::sync::Arc; use anyhow::Result; -use async_graphql::http::{playground_source, GraphQLPlaygroundConfig}; use async_graphql::ServerError; -use hyper::header::{self, CONTENT_TYPE}; +use hyper::header::{self}; use hyper::http::Method; use hyper::{Body, HeaderMap, Request, Response, StatusCode}; use opentelemetry::trace::SpanKind; use opentelemetry_semantic_conventions::trace::{HTTP_REQUEST_METHOD, HTTP_ROUTE}; -use prometheus::{Encoder, ProtobufEncoder, TextEncoder, PROTOBUF_FORMAT, TEXT_FORMAT}; use serde::de::DeserializeOwned; use tracing::Instrument; use tracing_opentelemetry::OpenTelemetrySpanExt; @@ -20,51 +17,9 @@ use super::request_context::RequestContext; use super::telemetry::{get_response_status_code, RequestCounter}; use super::{showcase, telemetry, AppContext}; use crate::async_graphql_hyper::{GraphQLRequestLike, GraphQLResponse}; -use crate::blueprint::telemetry::TelemetryExporter; -use crate::config::{PrometheusExporter, PrometheusFormat}; pub const API_URL_PREFIX: &str = "/api"; -pub fn graphiql(req: &Request) -> Result> { - let query = req.uri().query(); - let endpoint = "/graphql"; - let endpoint = if let Some(query) = query { - if query.is_empty() { - Cow::Borrowed(endpoint) - } else { - Cow::Owned(format!("{}?{}", endpoint, query)) - } - } else { - Cow::Borrowed(endpoint) - }; - - Ok(Response::new(Body::from(playground_source( - GraphQLPlaygroundConfig::new(&endpoint).title("Tailcall - GraphQL IDE"), - )))) -} - -fn prometheus_metrics(prometheus_exporter: &PrometheusExporter) -> Result> { - let metric_families = prometheus::default_registry().gather(); - let mut buffer = vec![]; - - match prometheus_exporter.format { - PrometheusFormat::Text => TextEncoder::new().encode(&metric_families, &mut buffer)?, - PrometheusFormat::Protobuf => { - ProtobufEncoder::new().encode(&metric_families, &mut buffer)? - } - }; - - let content_type = match prometheus_exporter.format { - PrometheusFormat::Text => TEXT_FORMAT, - PrometheusFormat::Protobuf => PROTOBUF_FORMAT, - }; - - Ok(Response::builder() - .status(200) - .header(CONTENT_TYPE, content_type) - .body(Body::from(buffer))?) -} - fn not_found() -> Result> { Ok(Response::builder() .status(StatusCode::NOT_FOUND) @@ -279,21 +234,6 @@ async fn handle_request_inner( graphql_request::(req, &app_ctx, req_counter).await } - hyper::Method::GET => { - if let Some(TelemetryExporter::Prometheus(prometheus)) = - app_ctx.blueprint.telemetry.export.as_ref() - { - if req.uri().path() == prometheus.path { - return prometheus_metrics(prometheus); - } - }; - - if app_ctx.blueprint.server.enable_graphiql { - return graphiql(&req); - } - - not_found() - } _ => not_found(), } } diff --git a/tailcall-cloudflare/src/handle.rs b/tailcall-cloudflare/src/handle.rs index 36fb31b5250..4dd2123d228 100644 --- a/tailcall-cloudflare/src/handle.rs +++ b/tailcall-cloudflare/src/handle.rs @@ -3,10 +3,10 @@ use std::collections::HashMap; use std::rc::Rc; use std::sync::{Arc, RwLock}; -use hyper::{Body, Method, Request, Response}; +use hyper::{Body, Request, Response}; use lazy_static::lazy_static; use tailcall::async_graphql_hyper::GraphQLRequest; -use tailcall::http::{graphiql, handle_request, showcase, AppContext}; +use tailcall::http::{handle_request, showcase, AppContext}; use crate::http::{to_request, to_response}; use crate::runtime; @@ -28,14 +28,6 @@ pub async fn fetch( ); let req = to_request(req).await?; - // Quick exit to GraphiQL - // - // Has to be done here, since when using GraphiQL, a config query parameter is - // not specified, and get_app_ctx will fail without it. - if req.method() == Method::GET { - return to_response(graphiql(&req)?).await; - } - let env = Rc::new(env); let app_ctx = match get_app_ctx(env, &req).await? { Ok(app_ctx) => app_ctx,