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

fix(api-server): Use createServer in all cases, to make fastify config consistent #11176

Merged
merged 4 commits into from
Aug 12, 2024
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
5 changes: 5 additions & 0 deletions .changesets/11176.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- fix(api-server): Use createServer in all cases, to make fastify config consistent (#11176) by @dac09

This PR removes all the cases where we use `createFastifyInstance` for the api server, and replaces it with `createServer`.

This makes sure that the API server config is always consistent - whether you use a server file or not. (createServer was only used when we had a server file before)
27 changes: 4 additions & 23 deletions packages/api-server/src/apiCLIConfigHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import chalk from 'chalk'
import { coerceRootPath } from '@redwoodjs/fastify-web'

import { getAPIPort, getAPIHost } from './cliHelpers'
import createFastifyInstance from './fastify'
import { redwoodFastifyAPI } from './plugins/api'
import { createServer } from './createServer'
import type { APIParsedOptions } from './types'

export async function handler(options: APIParsedOptions = {}) {
Expand All @@ -13,32 +12,14 @@ export async function handler(options: APIParsedOptions = {}) {

options.apiRootPath = coerceRootPath(options.apiRootPath ?? '/')

const fastify = await createFastifyInstance()
fastify.register(redwoodFastifyAPI, {
redwood: {
...options,
loadUserConfig: true,
},
const fastify = await createServer({
apiRootPath: options.apiRootPath,
})

options.host ??= getAPIHost()
options.port ??= getAPIPort()

await fastify.listen({
port: options.port,
host: options.host,
listenTextResolver: (address) => {
// In the past, in development, we've prioritized showing a friendlier
// host than the listen-on-all-ipv6-addresses '[::]'. Here we replace it
// with 'localhost' only if 1) we're not in production and 2) it's there.
// In production it's important to be transparent.
if (process.env.NODE_ENV !== 'production') {
address = address.replace(/http:\/\/\[::\]/, 'http://localhost')
}

return `Server listening at ${address}`
},
})
await fastify.start()

fastify.log.trace(
{ custom: { ...fastify.initialConfig } },
Expand Down
10 changes: 3 additions & 7 deletions packages/api-server/src/bothCLIConfigHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import chalk from 'chalk'
import { redwoodFastifyWeb, coerceRootPath } from '@redwoodjs/fastify-web'

import { getWebHost, getWebPort, getAPIHost, getAPIPort } from './cliHelpers'
import { createServer as createApiServer } from './createServer'
import createFastifyInstance from './fastify'
import { redwoodFastifyAPI } from './plugins/api'
import type { BothParsedOptions } from './types'

export async function handler(options: BothParsedOptions) {
Expand Down Expand Up @@ -33,12 +33,8 @@ export async function handler(options: BothParsedOptions) {
},
})

const apiFastify = await createFastifyInstance()
apiFastify.register(redwoodFastifyAPI, {
redwood: {
apiRootPath: options.apiRootPath,
loadUserConfig: true,
},
const apiFastify = await await createApiServer({
apiRootPath: options.apiRootPath,
})

await webFastify.listen({
Expand Down
Loading