From aa6dd92e645cd893cf1f35ec9ec556eb5dbe2f06 Mon Sep 17 00:00:00 2001 From: omar Date: Wed, 21 Feb 2024 20:11:41 -0800 Subject: [PATCH] chore: set port env --- README.md | 4 +-- src/main.rs | 7 +++-- src/schema.rs | 76 --------------------------------------------------- 3 files changed, 5 insertions(+), 82 deletions(-) delete mode 100644 src/schema.rs diff --git a/README.md b/README.md index baee4eb..6eb4ff1 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,6 @@ # Introspect -### ⚡ Serverless Function deployed to serve you entire schema as `JSON` or [`SDL`](https://sdk.vercel.ai/s/2x7agG8) or as a [live playground](https://introspect.lagon.dev/playground/https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3) of any `GraphQL` API - -#### Deployed on [Lagon](https://lagon.app/) - [Open Source JS Runtime](https://github.com/lagonapp/lagon) +### ⚡ Serve you entire schema as `JSON` or [`SDL`](https://sdk.vercel.ai/s/2x7agG8) or as a [live playground](https://introspect.up.railway.app/graphiql/https://api.thegraph.com/subgraphs/name/uniswap/uniswap-v3) of any `GraphQL` API ### Try it diff --git a/src/main.rs b/src/main.rs index ab23b67..26e9b23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,5 @@ mod graphiql; mod index; -mod schema; use axum::{http::StatusCode, response::IntoResponse, routing::get, Router}; use log::{info, warn}; @@ -8,7 +7,6 @@ use tracing_subscriber::layer::SubscriberExt; use graphiql::graphiql_handler; use index::index_handler; -use schema::schema_handler; pub type CapturedParams = (String, String); @@ -24,7 +22,10 @@ async fn main() { .route("/graphiql/*endpoint", get(graphiql_handler)); let app = app.fallback(handler_404); - let listener = tokio::net::TcpListener::bind("127.0.0.1:3443") + // port has to be $PORT for railway app to work + let port = std::env::var("PORT").unwrap_or_else(|_| "3443".to_string()); + + let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port)) .await .unwrap(); println!("Listening on http://{}", listener.local_addr().unwrap()); diff --git a/src/schema.rs b/src/schema.rs deleted file mode 100644 index 3efdf2c..0000000 --- a/src/schema.rs +++ /dev/null @@ -1,76 +0,0 @@ -use axum::http::StatusCode; -use axum::{ - body::{Body, Bytes}, - extract::State, - response::{IntoResponse, Response}, - routing::get, - Router, -}; -use reqwest::{ - header::{HeaderMap, HeaderName, HeaderValue}, - Client, -}; -use serde_json::json; - -pub async fn schema_handler( - State(url): State, - State(format): State, - State(headers): State, -) -> impl IntoResponse { - let introspection_options = json!({ - "descriptions": true, - "directiveIsRepeatable": true, - "schemaDescription": true - }); - let introspection_query = json!({ - "query": "query IntrospectionQuery($introspectionOptions: IntrospectionOptions) { __schema { ...FullSchema } } fragment FullSchema on __Schema { queryType { name } mutationType { name } subscriptionType { name } types { ...FullType } directives { name description locations args { ...InputValue } } } fragment FullType on __Type { kind name description fields(includeDeprecated: true) { name description args { ...InputValue } type { ...TypeRef } isDeprecated deprecationReason } inputFields { ...InputValue } interfaces { ...TypeRef } enumValues(includeDeprecated: true) { name description isDeprecated deprecationReason } possibleTypes { ...TypeRef } } fragment InputValue on __InputValue { name description type { ...TypeRef } defaultValue } fragment TypeRef on __Type { kind name ofType { kind name ofType { kind name ofType { kind name } } } }", - "variables": { - "introspectionOptions": introspection_options - } - }); - let client = Client::new(); - let response = client - .post(&url) - .headers(headers) - .json(&introspection_query) - .send() - .await - .unwrap(); - let status = response.status(); - let body = response.bytes().await.unwrap(); - if !status.is_success() { - return (status, body); - } - let body = body.to_vec(); - let body = String::from_utf8(body).unwrap(); - match format.as_str() { - "sdl" => { - let schema = json_schema_to_sdl(&body); - (StatusCode::OK, schema) - } - "json" => (StatusCode::OK, body), - _ => (StatusCode::OK, body), - } -} - -fn json_schema_to_sdl(json_schema: &str) -> String { - let schema: serde_json::Value = serde_json::from_str(json_schema).unwrap(); - let schema = schema.get("data").unwrap().get("__schema").unwrap(); - let query_type = schema.get("queryType").unwrap().get("name").unwrap(); - let mutation_type = schema.get("mutationType").unwrap().get("name").unwrap(); - let subscription_type = schema.get("subscriptionType").unwrap().get("name").unwrap(); - let types = schema.get("types").unwrap(); - let directives = schema.get("directives").unwrap(); - let sdl = format!( - "schema {{ - query: {} - mutation: {} - subscription: {} -}} -{} -{} -", - query_type, mutation_type, subscription_type, types, directives - ); - sdl -}