Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Studio: Make port configurable #9709

Merged
merged 7 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const {
} = require('@opentelemetry/semantic-conventions')
const { PrismaInstrumentation } = require ('@prisma/instrumentation')

const { getConfig } from '@redwoodjs/project-config'

// You may wish to set this to DiagLogLevel.DEBUG when you need to debug opentelemetry itself
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.INFO)

Expand All @@ -25,10 +27,12 @@ const resource = Resource.default().merge(
})
)

const studioPort = getConfig().experimental.studio.basePort
const exporter = new OTLPTraceExporter({
// Update this URL to point to where your OTLP compatible collector is listening
// The redwood development studio (`yarn rw exp studio`) can collect your telemetry at `http://127.0.0.1:4318/v1/traces`
url: 'http://127.0.0.1:4318/v1/traces',
// The redwood development studio (`yarn rw exp studio`) can collect your
// telemetry at `http://127.0.0.1:<PORT>/v1/traces` (default PORT is 4318)
url: `http://127.0.0.1:${studioPort}/v1/traces`,
})

// You may wish to switch to BatchSpanProcessor in production as it is the recommended choice for performance reasons
Expand Down
1 change: 1 addition & 0 deletions packages/project-config/src/__tests__/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ describe('getConfig', () => {
"enabled": false,
},
"studio": {
"basePort": 4318,
"graphiql": {
"authImpersonation": {
"authProvider": undefined,
Expand Down
2 changes: 2 additions & 0 deletions packages/project-config/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ interface AuthImpersonationConfig {
}

interface StudioConfig {
basePort: number
inMemory: boolean
graphiql?: GraphiQLStudioConfig
}
Expand Down Expand Up @@ -171,6 +172,7 @@ const DEFAULT_CONFIG: Config = {
apiSdk: undefined,
},
studio: {
basePort: 4318,
inMemory: false,
graphiql: {
endpoint: 'graphql',
Expand Down
10 changes: 0 additions & 10 deletions packages/studio/api/fastify/react.ts

This file was deleted.

1 change: 1 addition & 0 deletions packages/studio/api/graphql/yoga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ export const setupYoga = (fastify: FastifyInstance) => {
}

type StudioConfig {
basePort: Int!
inMemory: Boolean
graphiql: GraphiQLConfig
}
Expand Down
22 changes: 15 additions & 7 deletions packages/studio/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import path from 'node:path'

import fastifyStatic from '@fastify/static'
import Fastify from 'fastify'
import type { FastifyInstance } from 'fastify'
import open from 'open'

import withApiProxy from './fastify/plugins/withApiProxy'
import reactRoutes from './fastify/react'
import spanRoutes from './fastify/spanIngester'
import yogaRoutes from './fastify/yoga'
import { setupYoga } from './graphql/yoga'
import { getWebConfig } from './lib/config'
import { getStudioConfig, getWebConfig } from './lib/config'
import { rewriteWebToUsePort } from './lib/rewriteWebToUsePort'
import {
registerMailRelatedWatchers,
startServer as startMailServer,
Expand All @@ -16,7 +19,6 @@ import {
import { runMigrations } from './migrations'

const HOST = 'localhost'
const PORT = 4318

let fastify: FastifyInstance

Expand Down Expand Up @@ -59,20 +61,26 @@ export const start = async (
rewritePrefix: '/' + graphqlEndpoint.split('/').slice(3).join('/'),
})

const studioPort = getStudioConfig().basePort
const webPath = path.join(__dirname, '..', '..', 'web')

rewriteWebToUsePort(webPath, studioPort)

// GraphQL
const yogaServer = setupYoga(fastify)

// Routes
fastify.register(spanRoutes)
fastify.register(yogaRoutes, { yoga: yogaServer })
fastify.register(reactRoutes)
// Statically serve the web side (React)
fastify.register(fastifyStatic, { root: webPath })

fastify.listen({ port: PORT, host: HOST })
fastify.listen({ port: studioPort, host: HOST })
fastify.ready(() => {
console.log(`Studio API listening on ${HOST}:${PORT}`)
console.log(`Studio API listening on ${HOST}:${studioPort}`)

if (autoOpen) {
open(`http://${HOST}:${PORT}`)
open(`http://${HOST}:${studioPort}`)
}
})

Expand Down
12 changes: 12 additions & 0 deletions packages/studio/api/lib/rewriteWebToUsePort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'fs'
import path from 'path'

export function rewriteWebToUsePort(webPath: string, studioPort: number) {
const indexHtmlPath = path.join(webPath, 'index.html')
let indexHtml = fs.readFileSync(indexHtmlPath, 'utf8')
indexHtml = indexHtml.replace(
'RWJS_STUDIO_BASE_PORT=4318',
'RWJS_STUDIO_BASE_PORT=' + studioPort
)
fs.writeFileSync(indexHtmlPath, indexHtml)
}
8 changes: 6 additions & 2 deletions packages/studio/api/mail/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import path from 'node:path'

import { getStudioConfig } from 'api/lib/config'
import chokidar from 'chokidar'
import fs from 'fs-extra'
import { simpleParser as simpleMailParser } from 'mailparser'
Expand Down Expand Up @@ -38,8 +39,11 @@ export function startServer() {
})
},
})
smtpServer.listen(4319, undefined, () => {
console.log('Studio SMTP Server listening on 4319')

const port = getStudioConfig().basePort + 1

smtpServer.listen(port, undefined, () => {
console.log('Studio SMTP Server listening on ' + port)
})
}

Expand Down
1 change: 1 addition & 0 deletions packages/studio/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export interface AuthImpersonationConfig {
}

export interface StudioConfig {
basePort: number
inMemory: boolean
graphiql?: GraphiQLStudioConfig
}
Expand Down
2 changes: 2 additions & 0 deletions packages/studio/web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<link rel="icon" type="image/svg+xml" href="https://redwoodjs.com/images/diecut.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>RedwoodJS Studio</title>
<!-- The port number below will be replaced before serving the web side -->
<script>window.RWJS_STUDIO_BASE_PORT=4318</script>
</head>
<body class="h-full">
<div id="root" class="min-w-full max-w-full"></div>
Expand Down
9 changes: 4 additions & 5 deletions packages/studio/web/src/Pages/GraphiQL.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ const DEFAULT_QUERY = `{
}`

function GraphiQL() {
const port = window.RWJS_STUDIO_BASE_PORT
const { data } = useQuery(GET_AUTH)
let headers = undefined as undefined | Record<string, string>
let headers: undefined | Record<string, string> = undefined

if (data && data.generateAuthHeaders) {
const { authProvider, authorization, cookie } = data.generateAuthHeaders
Expand All @@ -46,20 +47,18 @@ function GraphiQL() {
}

if (headers) {
console.debug('headers', headers)
console.debug('headers JSON', JSON.stringify(headers))
return (
<RedwoodGraphiQL
headers={JSON.stringify(headers)}
endpoint="http://localhost:4318/proxies/graphql"
endpoint={`http://localhost:${port}/proxies/graphql`}
defaultQuery={DEFAULT_QUERY}
/>
)
}

return (
<RedwoodGraphiQL
endpoint="http://localhost:4318/proxies/graphql"
endpoint={`http://localhost:${port}/proxies/graphql`}
defaultQuery={DEFAULT_QUERY}
/>
)
Expand Down
2 changes: 1 addition & 1 deletion packages/studio/web/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import Overview from './Pages/Overview'
import Performance from './Pages/Performance'

const client = new ApolloClient({
uri: 'http://localhost:4318/graphql',
uri: `http://localhost:${window.RWJS_STUDIO_BASE_PORT}/graphql`,
cache: new InMemoryCache({
typePolicies: {
Span: {
Expand Down
8 changes: 8 additions & 0 deletions packages/studio/web/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
/// <reference types="vite/client" />

declare global {
interface Window {
RWJS_STUDIO_BASE_PORT: number
}
}

export {}
Loading