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

chore(v6): temporarily revert host-related changes #8765

Merged
merged 3 commits into from
Jun 28, 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
33 changes: 15 additions & 18 deletions docs/docs/cli-commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -1990,12 +1990,11 @@ yarn redwood serve [side]

`yarn rw serve` is useful for debugging locally or for self-hosting—deploying a single server into a serverful environment. Since both the api and the web sides run in the same server, CORS isn't a problem.

| Arguments & Options | Description |
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `side` | Which side(s) to run. Choices are `api` and `web`. Defaults to `api` and `web` |
| `--port` | What port should the server run on [default: 8911] |
| `--host` | What host should the server run on. This defaults to the value of `web.host` in the `redwood.toml` file which itself defaults to `'localhost'`. |
| `--socket` | The socket the server should run. This takes precedence over port |
| Arguments & Options | Description |
| ------------------- | ------------------------------------------------------------------------------ |
| `side` | Which side(s) to run. Choices are `api` and `web`. Defaults to `api` and `web` |
| `--port` | What port should the server run on [default: 8911] |
| `--socket` | The socket the server should run. This takes precedence over port |

### serve api

Expand All @@ -2007,12 +2006,11 @@ yarn rw serve api

This command uses `apiUrl` in your `redwood.toml`. Use this command if you want to run just the api side on a server (e.g. running on Render).

| Arguments & Options | Description |
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `--port` | What port should the server run on [default: 8911] |
| `--host` | What host should the server run on. This defaults to the value of `api.host` in the `redwood.toml` file which itself defaults to `'localhost'`. |
| `--socket` | The socket the server should run. This takes precedence over port |
| `--apiRootPath` | The root path where your api functions are served |
| Arguments & Options | Description |
| ------------------- | ----------------------------------------------------------------- |
| `--port` | What port should the server run on [default: 8911] |
| `--socket` | The socket the server should run. This takes precedence over port |
| `--apiRootPath` | The root path where your api functions are served |

For the full list of Server Configuration settings, see [this documentation](app-configuration-redwood-toml.md#api).
If you want to format your log output, you can pipe the command to the Redwood LogFormatter:
Expand All @@ -2035,12 +2033,11 @@ This command serves the contents in `web/dist`. Use this command if you're debug
>
> Probably, but it can be a challenge to setup when you just want something running quickly!

| Arguments & Options | Description |
| ------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `--port` | What port should the server run on [default: 8911] |
| `--host` | What host should the server run on. This defaults to the value of `web.host` in the `redwood.toml` file which itself defaults to `'localhost'`. |
| `--socket` | The socket the server should run. This takes precedence over port |
| `--apiHost` | Forwards requests from the `apiUrl` (defined in `redwood.toml`) to the specified host |
| Arguments & Options | Description |
| ------------------- | ------------------------------------------------------------------------------------- |
| `--port` | What port should the server run on [default: 8911] |
| `--socket` | The socket the server should run. This takes precedence over port |
| `--apiHost` | Forwards requests from the `apiUrl` (defined in `redwood.toml`) to the specified host |

If you want to format your log output, you can pipe the command to the Redwood LogFormatter:

Expand Down
57 changes: 14 additions & 43 deletions packages/api-server/src/cliHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,13 @@ const sendProcessReady = () => {
return process.send && process.send('ready')
}

const redwoodProjectConfig = getConfig()

export const commonOptions = {
port: {
default: redwoodProjectConfig.web.port,
type: 'number',
alias: 'p',
},
host: {
default: redwoodProjectConfig.web.host,
type: 'string',
},
port: { default: getConfig().web?.port || 8910, type: 'number', alias: 'p' },
socket: { type: 'string' },
} as const

export const apiCliOptions = {
port: {
default: redwoodProjectConfig.api.port,
type: 'number',
alias: 'p',
},
host: {
default: redwoodProjectConfig.api.host,
type: 'string',
},
port: { default: getConfig().api?.port || 8911, type: 'number', alias: 'p' },
socket: { type: 'string' },
apiRootPath: {
alias: ['rootPath', 'root-path'],
Expand All @@ -54,15 +36,7 @@ export const apiCliOptions = {
} as const

export const webCliOptions = {
port: {
default: redwoodProjectConfig.web.port,
type: 'number',
alias: 'p',
},
host: {
default: redwoodProjectConfig.web.host,
type: 'string',
},
port: { default: getConfig().web?.port || 8910, type: 'number', alias: 'p' },
socket: { type: 'string' },
apiHost: {
alias: 'api-host',
Expand All @@ -72,7 +46,7 @@ export const webCliOptions = {
} as const

export const apiServerHandler = async (options: ApiServerArgs) => {
const { port, host, socket, apiRootPath } = options
const { port, socket, apiRootPath } = options
const tsApiServer = Date.now()
process.stdout.write(c.dim(c.italic('Starting API Server...\n')))

Expand All @@ -83,15 +57,14 @@ export const apiServerHandler = async (options: ApiServerArgs) => {

const http = startFastifyServer({
port,
host,
socket,
fastify,
}).ready(() => {
console.log(c.italic(c.dim('Took ' + (Date.now() - tsApiServer) + ' ms')))

const on = socket
? socket
: c.magenta(`http://${host}:${port}${apiRootPath}`)
: c.magenta(`http://localhost:${port}${apiRootPath}`)
console.log(`API listening on ${on}`)
const graphqlEnd = c.magenta(`${apiRootPath}graphql`)
console.log(`GraphQL endpoint at ${graphqlEnd}`)
Expand All @@ -103,10 +76,10 @@ export const apiServerHandler = async (options: ApiServerArgs) => {
}

export const bothServerHandler = async (options: BothServerArgs) => {
const { port, host, socket } = options
const { port, socket } = options
const tsServer = Date.now()
process.stdout.write(c.dim(c.italic('Starting API and Web Servers...\n')))
const apiRootPath = coerceRootPath(redwoodProjectConfig.web.apiUrl)
const apiRootPath = coerceRootPath(getConfig().web.apiUrl)

let fastify = createFastifyInstance()

Expand All @@ -116,16 +89,15 @@ export const bothServerHandler = async (options: BothServerArgs) => {

startFastifyServer({
port,
host,
socket,
fastify,
}).ready(() => {
console.log(c.italic(c.dim('Took ' + (Date.now() - tsServer) + ' ms')))
const on = socket
? socket
: c.magenta(`http://${host}:${port}${apiRootPath}`)
const webServer = c.green(`http://${host}:${port}`)
const apiServer = c.magenta(`http://${host}:${port}`)
: c.magenta(`http://localhost:${port}${apiRootPath}`)
const webServer = c.green(`http://localhost:${port}`)
const apiServer = c.magenta(`http://localhost:${port}`)
console.log(`Web server started on ${webServer}`)
console.log(`API serving from ${apiServer}`)
console.log(`API listening on ${on}`)
Expand All @@ -136,14 +108,14 @@ export const bothServerHandler = async (options: BothServerArgs) => {
}

export const webServerHandler = async (options: WebServerArgs) => {
const { port, host, socket, apiHost } = options
const { port, socket, apiHost } = options
const tsServer = Date.now()
process.stdout.write(c.dim(c.italic('Starting Web Server...\n')))
const apiUrl = redwoodProjectConfig.web.apiUrl
const apiUrl = getConfig().web.apiUrl
// Construct the graphql url from apiUrl by default
// But if apiGraphQLUrl is specified, use that instead
const graphqlEndpoint = coerceRootPath(
redwoodProjectConfig.web.apiGraphQLUrl ?? `${apiUrl}/graphql`
getConfig().web.apiGraphQLUrl ?? `${apiUrl}/graphql`
)

let fastify = createFastifyInstance()
Expand All @@ -160,15 +132,14 @@ export const webServerHandler = async (options: WebServerArgs) => {

startFastifyServer({
port,
host,
socket,
fastify,
}).ready(() => {
console.log(c.italic(c.dim('Took ' + (Date.now() - tsServer) + ' ms')))
if (socket) {
console.log(`Listening on ` + c.magenta(`${socket}`))
}
const webServer = c.green(`http://${host}:${port}`)
const webServer = c.green(`http://localhost:${port}`)
console.log(`Web server started on ${webServer}`)
console.log(`GraphQL endpoint is set to ` + c.magenta(`${graphqlEndpoint}`))
sendProcessReady()
Expand Down
3 changes: 1 addition & 2 deletions packages/api-server/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ import { FastifyInstance } from 'fastify'

export interface HttpServerParams {
port: number
host?: string
socket?: string
fastify: FastifyInstance
}

export const startServer = ({
port = 8911,
host = 'localhost',
socket,
fastify,
}: HttpServerParams) => {
const host = process.env.NODE_ENV === 'production' ? '0.0.0.0' : '::'
const serverPort = socket ? parseInt(socket) : port

fastify.listen({ port: serverPort, host })
Expand Down
41 changes: 17 additions & 24 deletions packages/api-server/src/watch.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env node
// This script is called by the `yarn rw dev` command. Specifically, it's the api command.

import { fork } from 'child_process'
import type { ChildProcess } from 'child_process'
Expand All @@ -23,9 +22,6 @@ import {
resolveFile,
} from '@redwoodjs/project-config'

const redwoodProjectPaths = getPaths()
const redwoodProjectConfig = getConfig()

const argv = yargs(hideBin(process.argv))
.option('debug-port', {
alias: 'dp',
Expand All @@ -36,18 +32,15 @@ const argv = yargs(hideBin(process.argv))
alias: 'p',
description: 'Port',
type: 'number',
default: redwoodProjectConfig.api.port,
})
.option('host', {
description: 'Host',
type: 'string',
default: redwoodProjectConfig.api.host,
})
.help()
.alias('help', 'h')
.parseSync()

// If this is run via the yarn rw dev command, this will have already been called.
const rwjsPaths = getPaths()

dotenv.config({
path: redwoodProjectPaths.base,
path: rwjsPaths.base,
})

// TODO:
Expand Down Expand Up @@ -90,9 +83,9 @@ const rebuildApiServer = () => {
}

// OpenTelemetry SDK Setup
if (redwoodProjectConfig.experimental.opentelemetry.enabled) {
if (getConfig().experimental.opentelemetry.enabled) {
const opentelemetrySDKScriptPath =
redwoodProjectConfig.experimental.opentelemetry.apiSdk
getConfig().experimental.opentelemetry.apiSdk
if (opentelemetrySDKScriptPath) {
console.log(
`Setting up OpenTelemetry using the setup file: ${opentelemetrySDKScriptPath}`
Expand All @@ -114,10 +107,12 @@ const rebuildApiServer = () => {
forkOpts.execArgv = forkOpts.execArgv.concat([`--inspect=${debugPort}`])
}

const port = argv.port ?? getConfig().api.port

// Start API server

// Check if experimental server file exists
const serverFile = resolveFile(`${redwoodProjectPaths.api.dist}/server`)
const serverFile = resolveFile(`${rwjsPaths.api.dist}/server`)
if (serverFile) {
const separator = chalk.hex('#ff845e')(
'------------------------------------------------------------------'
Expand All @@ -135,7 +130,7 @@ const rebuildApiServer = () => {
} else {
httpServerProcess = fork(
path.join(__dirname, 'index.js'),
['api', '--port', argv.port.toString(), '--host', `${argv.host}`],
['api', '--port', port.toString()],
forkOpts
)
}
Expand All @@ -156,16 +151,16 @@ const delayRestartServer = debounce(
)

// NOTE: the file comes through as a unix path, even on windows
// So we need to convert the redwoodProjectPaths
// So we need to convert the rwjsPaths

const IGNORED_API_PATHS = [
'api/dist', // use this, because using redwoodProjectPaths.api.dist seems to not ignore on first build
redwoodProjectPaths.api.types,
redwoodProjectPaths.api.db,
'api/dist', // use this, because using rwjsPaths.api.dist seems to not ignore on first build
rwjsPaths.api.types,
rwjsPaths.api.db,
].map((path) => ensurePosixPath(path))

chokidar
.watch(redwoodProjectPaths.api.base, {
.watch(rwjsPaths.api.base, {
persistent: true,
ignoreInitial: true,
ignored: (file: string) => {
Expand Down Expand Up @@ -204,9 +199,7 @@ chokidar
}

console.log(
c.dim(
`[${eventName}] ${filePath.replace(redwoodProjectPaths.api.base, '')}`
)
c.dim(`[${eventName}] ${filePath.replace(rwjsPaths.api.base, '')}`)
)
delayRestartServer.cancel()
delayRestartServer()
Expand Down
8 changes: 2 additions & 6 deletions packages/cli/src/commands/__tests__/dev.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ describe('yarn rw dev', () => {
getConfig.mockReturnValue({
web: {
port: 8910,
host: 'localhost',
},
api: {
port: 8911,
Expand All @@ -101,7 +100,7 @@ describe('yarn rw dev', () => {
)

expect(apiCommand.command).toMatchInlineSnapshot(
`"yarn cross-env NODE_ENV=development NODE_OPTIONS=--enable-source-maps yarn nodemon --quiet --watch "/mocked/project/redwood.toml" --exec "yarn rw-api-server-watch --port 8911 --host '::' --debug-port 18911 | rw-log-formatter""`
`"yarn cross-env NODE_ENV=development NODE_OPTIONS=--enable-source-maps yarn nodemon --quiet --watch "/mocked/project/redwood.toml" --exec "yarn rw-api-server-watch --port 8911 --debug-port 18911 | rw-log-formatter""`
)

expect(generateCommand.command).toEqual('yarn rw-gen-watch')
Expand All @@ -111,7 +110,6 @@ describe('yarn rw dev', () => {
getConfig.mockReturnValue({
web: {
port: 8910,
host: 'localhost',
},
api: {
port: 8911,
Expand All @@ -129,15 +127,14 @@ describe('yarn rw dev', () => {
const apiCommand = find(concurrentlyArgs, { name: 'api' })

expect(apiCommand.command).toContain(
"yarn rw-api-server-watch --port 8911 --host '::' --debug-port 90909090"
'yarn rw-api-server-watch --port 8911 --debug-port 90909090'
)
})

it('Can disable debugger by setting toml to false', async () => {
getConfig.mockReturnValue({
web: {
port: 8910,
host: 'localhost',
},
api: {
port: 8911,
Expand All @@ -160,7 +157,6 @@ describe('yarn rw dev', () => {
getConfig.mockReturnValue({
web: {
port: 8910,
host: 'localhost',
bundler: 'vite', // <-- enable vite mode
},
api: {
Expand Down
Loading