@@ -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
3945type  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
6071type  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