-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathcliHandlers.ts
174 lines (148 loc) · 5.39 KB
/
cliHandlers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import path from 'path'
import c from 'ansi-colors'
import { getPaths, getConfig } from '@redwoodjs/project-config'
import createFastifyInstance from './fastify'
import withApiProxy from './plugins/withApiProxy'
import withFunctions from './plugins/withFunctions'
import withWebServer from './plugins/withWebServer'
import { startServer as startFastifyServer } from './server'
import type { BothServerArgs, WebServerArgs, ApiServerArgs } from './types'
/*
* This file has defines CLI handlers used by the redwood cli, for `rw serve`
* Also used in index.ts for the api server
*/
const sendProcessReady = () => {
return process.send && process.send('ready')
}
export const commonOptions = {
port: { default: getConfig().web?.port || 8910, type: 'number', alias: 'p' },
socket: { type: 'string' },
} as const
export const apiCliOptions = {
port: { default: getConfig().api?.port || 8911, type: 'number', alias: 'p' },
socket: { type: 'string' },
apiRootPath: {
alias: ['rootPath', 'root-path'],
default: '/',
type: 'string',
desc: 'Root path where your api functions are served',
coerce: coerceRootPath,
},
loadEnvFiles: {
description: 'Load .env and .env.defaults files',
type: 'boolean',
// We have to default to `false` for backwards compatibility.
default: false,
},
} as const
export const webCliOptions = {
port: { default: getConfig().web?.port || 8910, type: 'number', alias: 'p' },
socket: { type: 'string' },
apiHost: {
alias: 'api-host',
type: 'string',
desc: 'Forward requests from the apiUrl, defined in redwood.toml to this host',
},
} as const
export const apiServerHandler = async (options: ApiServerArgs) => {
const { port, socket, apiRootPath, loadEnvFiles } = options
const tsApiServer = Date.now()
process.stdout.write(c.dim(c.italic('Starting API Server...\n')))
if (loadEnvFiles) {
// @ts-expect-error for some reason ts can't find the types here but can find them for other packages
const { config } = await import('dotenv-defaults')
config({
path: path.join(getPaths().base, '.env'),
defaults: path.join(getPaths().base, '.env.defaults'),
multiline: true,
})
}
let fastify = createFastifyInstance()
// Import Server Functions.
fastify = await withFunctions(fastify, options)
const http = startFastifyServer({
port,
socket,
fastify,
}).ready(() => {
console.log(c.italic(c.dim('Took ' + (Date.now() - tsApiServer) + ' ms')))
const on = socket
? socket
: c.magenta(`http://localhost:${port}${apiRootPath}`)
console.log(`API listening on ${on}`)
const graphqlEnd = c.magenta(`${apiRootPath}graphql`)
console.log(`GraphQL endpoint at ${graphqlEnd}`)
sendProcessReady()
})
process.on('exit', () => {
http?.close()
})
}
export const bothServerHandler = async (options: BothServerArgs) => {
const { port, socket } = options
const tsServer = Date.now()
process.stdout.write(c.dim(c.italic('Starting API and Web Servers...\n')))
const apiRootPath = coerceRootPath(getConfig().web.apiUrl)
let fastify = createFastifyInstance()
// Attach plugins
fastify = await withWebServer(fastify, options)
fastify = await withFunctions(fastify, { ...options, apiRootPath })
startFastifyServer({
port,
socket,
fastify,
}).ready(() => {
console.log(c.italic(c.dim('Took ' + (Date.now() - tsServer) + ' ms')))
const on = socket
? socket
: 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}`)
const graphqlEnd = c.magenta(`${apiRootPath}graphql`)
console.log(`GraphQL endpoint at ${graphqlEnd}`)
sendProcessReady()
})
}
export const webServerHandler = async (options: WebServerArgs) => {
const { port, socket, apiHost } = options
const tsServer = Date.now()
process.stdout.write(c.dim(c.italic('Starting Web Server...\n')))
const apiUrl = getConfig().web.apiUrl
// Construct the graphql url from apiUrl by default
// But if apiGraphQLUrl is specified, use that instead
const graphqlEndpoint = coerceRootPath(
getConfig().web.apiGraphQLUrl ?? `${apiUrl}/graphql`
)
let fastify = createFastifyInstance()
// serve static files from "web/dist"
fastify = await withWebServer(fastify, options)
// If apiHost is supplied, it means the functions are running elsewhere
// So we should just proxy requests
if (apiHost) {
// Attach plugin for proxying
fastify = await withApiProxy(fastify, { apiHost, apiUrl })
}
startFastifyServer({
port,
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://localhost:${port}`)
console.log(`Web server started on ${webServer}`)
console.log(`GraphQL endpoint is set to ` + c.magenta(`${graphqlEndpoint}`))
sendProcessReady()
})
}
function coerceRootPath(path: string) {
// Make sure that we create a root path that starts and ends with a slash (/)
const prefix = path.charAt(0) !== '/' ? '/' : ''
const suffix = path.charAt(path.length - 1) !== '/' ? '/' : ''
return `${prefix}${path}${suffix}`
}