Skip to content

Commit dcdbd17

Browse files
authored
fix(api-server): Use --port and --host options (#81)
The `rw-server` bin was documented to have `--port` and `--host` command line options, but they were never actually used. `-p` would work, but not `--port`. You could configure them both before using env vars, and you still can, but now also the long version of the command line arguments work.
1 parent 1b544a0 commit dcdbd17

File tree

3 files changed

+55
-42
lines changed

3 files changed

+55
-42
lines changed

packages/api-server/src/__tests__/createServer.test.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import { getConfig } from '@redmix/project-config'
1818
import type { createServer as tCreateServer } from '../createServer.js'
1919
import {
2020
resolveOptions,
21-
DEFAULT_CREATE_SERVER_OPTIONS,
21+
getDefaultCreateServerOptions,
2222
} from '../createServerHelpers'
2323

2424
// Set up RWJS_CWD.
@@ -223,21 +223,28 @@ describe('createServer', () => {
223223
})
224224

225225
describe('resolveOptions', () => {
226-
it('nothing passed', () => {
226+
it('uses defaults when no arguments are passed', () => {
227227
const resolvedOptions = resolveOptions()
228+
const defaults = getDefaultCreateServerOptions()
228229

229230
expect(resolvedOptions).toEqual({
230-
apiRootPath: DEFAULT_CREATE_SERVER_OPTIONS.apiRootPath,
231-
configureApiServer: DEFAULT_CREATE_SERVER_OPTIONS.configureApiServer,
231+
apiRootPath: defaults.apiRootPath,
232+
// Can't really compare functions with toEqual() unless it's the same
233+
// reference. And when calling `getDefaulCreateServerOptions()` you get a
234+
// new function, and thus a new reference, each time
235+
configureApiServer: resolvedOptions.configureApiServer,
232236
fastifyServerOptions: {
233-
requestTimeout:
234-
DEFAULT_CREATE_SERVER_OPTIONS.fastifyServerOptions.requestTimeout,
235-
logger: DEFAULT_CREATE_SERVER_OPTIONS.logger,
236-
bodyLimit: DEFAULT_CREATE_SERVER_OPTIONS.fastifyServerOptions.bodyLimit,
237+
requestTimeout: defaults.fastifyServerOptions.requestTimeout,
238+
logger: defaults.logger,
239+
bodyLimit: defaults.fastifyServerOptions.bodyLimit,
237240
},
238-
apiPort: 65501,
239241
apiHost: '::',
242+
apiPort: 65501,
240243
})
244+
245+
expect(resolvedOptions.configureApiServer.toString()).toEqual(
246+
defaults.configureApiServer.toString(),
247+
)
241248
})
242249

243250
it('ensures `apiRootPath` has slashes', () => {
@@ -300,7 +307,7 @@ describe('resolveOptions', () => {
300307

301308
expect(resolvedOptions).toMatchObject({
302309
fastifyServerOptions: {
303-
logger: DEFAULT_CREATE_SERVER_OPTIONS.logger,
310+
logger: getDefaultCreateServerOptions().logger,
304311
},
305312
})
306313
})

packages/api-server/src/apiCLIConfigHandler.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import chalk from 'chalk'
22

33
import { coerceRootPath } from '@redmix/fastify-web'
44

5-
import { getAPIPort, getAPIHost } from './cliHelpers'
65
import { createServer } from './createServer'
76
import type { APIParsedOptions } from './types'
87

@@ -14,11 +13,10 @@ export async function handler(options: APIParsedOptions = {}) {
1413

1514
const fastify = await createServer({
1615
apiRootPath: options.apiRootPath,
16+
apiHost: options.host,
17+
apiPort: options.port,
1718
})
1819

19-
options.host ??= getAPIHost()
20-
options.port ??= getAPIPort()
21-
2220
await fastify.start()
2321

2422
fastify.log.trace(

packages/api-server/src/createServerHelpers.ts

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export interface CreateServerOptions {
3434

3535
/** Whether to parse args or not. Defaults to `true` */
3636
parseArgs?: boolean
37+
38+
/** The port to listen on. Defaults to what's configured in redwood.toml */
39+
apiPort?: number
40+
41+
/** The host to bind to. Defaults to what's configured in redwood.toml */
42+
apiHost?: string
3743
}
3844

3945
type DefaultCreateServerOptions = Required<
@@ -42,26 +48,29 @@ type DefaultCreateServerOptions = Required<
4248
}
4349
>
4450

45-
export const DEFAULT_CREATE_SERVER_OPTIONS: DefaultCreateServerOptions = {
46-
apiRootPath: '/',
47-
logger: {
48-
level:
49-
process.env.LOG_LEVEL ??
50-
(process.env.NODE_ENV === 'development' ? 'debug' : 'warn'),
51-
},
52-
fastifyServerOptions: {
53-
requestTimeout: 15_000,
54-
bodyLimit: 1024 * 1024 * 100, // 100MB
55-
},
56-
configureApiServer: () => {},
57-
parseArgs: true,
58-
}
51+
// This is a function instead of just a constant so that we don't execute
52+
// getAPIHost and getAPIPort just by importing this file.
53+
export const getDefaultCreateServerOptions: () => DefaultCreateServerOptions =
54+
() => ({
55+
apiRootPath: '/',
56+
logger: {
57+
level:
58+
process.env.LOG_LEVEL ??
59+
(process.env.NODE_ENV === 'development' ? 'debug' : 'warn'),
60+
},
61+
fastifyServerOptions: {
62+
requestTimeout: 15_000,
63+
bodyLimit: 1024 * 1024 * 100, // 100MB
64+
},
65+
configureApiServer: () => {},
66+
parseArgs: true,
67+
apiHost: getAPIHost(),
68+
apiPort: getAPIPort(),
69+
})
5970

6071
type ResolvedOptions = Required<
6172
Omit<CreateServerOptions, 'logger' | 'fastifyServerOptions' | 'parseArgs'> & {
6273
fastifyServerOptions: FastifyServerOptions
63-
apiPort: number
64-
apiHost: string
6574
}
6675
>
6776

@@ -71,29 +80,28 @@ export function resolveOptions(
7180
) {
7281
options.parseArgs ??= true
7382

74-
options.logger ??= DEFAULT_CREATE_SERVER_OPTIONS.logger
83+
const defaults = getDefaultCreateServerOptions()
84+
85+
options.logger ??= defaults.logger
7586

7687
// Set defaults.
7788
const resolvedOptions: ResolvedOptions = {
78-
apiRootPath:
79-
options.apiRootPath ?? DEFAULT_CREATE_SERVER_OPTIONS.apiRootPath,
89+
apiRootPath: options.apiRootPath ?? defaults.apiRootPath,
8090

8191
fastifyServerOptions: options.fastifyServerOptions ?? {
82-
requestTimeout:
83-
DEFAULT_CREATE_SERVER_OPTIONS.fastifyServerOptions.requestTimeout,
84-
logger: options.logger ?? DEFAULT_CREATE_SERVER_OPTIONS.logger,
85-
bodyLimit: DEFAULT_CREATE_SERVER_OPTIONS.fastifyServerOptions.bodyLimit,
92+
requestTimeout: defaults.fastifyServerOptions.requestTimeout,
93+
logger: options.logger ?? defaults.logger,
94+
bodyLimit: defaults.fastifyServerOptions.bodyLimit,
8695
},
8796
configureApiServer:
88-
options.configureApiServer ??
89-
DEFAULT_CREATE_SERVER_OPTIONS.configureApiServer,
90-
apiHost: getAPIHost(),
91-
apiPort: getAPIPort(),
97+
options.configureApiServer ?? defaults.configureApiServer,
98+
apiHost: options.apiHost ?? defaults.apiHost,
99+
apiPort: options.apiPort ?? defaults.apiPort,
92100
}
93101

94102
// Merge fastifyServerOptions.
95103
resolvedOptions.fastifyServerOptions.requestTimeout ??=
96-
DEFAULT_CREATE_SERVER_OPTIONS.fastifyServerOptions.requestTimeout
104+
defaults.fastifyServerOptions.requestTimeout
97105
resolvedOptions.fastifyServerOptions.logger = options.logger
98106

99107
if (options.parseArgs) {

0 commit comments

Comments
 (0)