-
Notifications
You must be signed in to change notification settings - Fork 1k
/
serveBothHandler.js
187 lines (155 loc) · 4.41 KB
/
serveBothHandler.js
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
175
176
177
178
179
180
181
182
183
184
185
186
187
import path from 'path'
import chalk from 'chalk'
import execa from 'execa'
import {
coerceRootPath,
createFastifyInstance,
redwoodFastifyAPI,
redwoodFastifyWeb,
} from '@redwoodjs/fastify'
import { getConfig, getPaths } from '@redwoodjs/project-config'
export const bothExperimentalServerFileHandler = async () => {
logExperimentalHeader()
if (getConfig().experimental?.rsc?.enabled) {
logSkippingFastifyWebServer()
await execa(
'node',
[
'--conditions react-server',
'./node_modules/@redwoodjs/vite/dist/runRscFeServer.js',
],
{
cwd: getPaths().base,
stdio: 'inherit',
shell: true,
}
)
} else if (getConfig().experimental?.streamingSsr?.enabled) {
logSkippingFastifyWebServer()
await execa('yarn', ['rw-serve-fe'], {
cwd: getPaths().web.base,
stdio: 'inherit',
shell: true,
})
} else {
await execa(
'yarn',
['node', path.join('dist', 'server.js'), '--enable-web'],
{
cwd: getPaths().api.base,
stdio: 'inherit',
shell: true,
}
)
}
}
export const bothRscServerHandler = async (argv) => {
const { apiServerHandler } = await import('./serveApiHandler.js')
// TODO (RSC) Allow specifying port, socket and apiRootPath
const apiPromise = apiServerHandler({
...argv,
port: 8911,
apiRootPath: '/',
})
// TODO (RSC) More gracefully handle Ctrl-C
const fePromise = execa(
'node',
[
'--experimental-loader @redwoodjs/vite/node-loader',
'--experimental-loader @redwoodjs/vite/react-node-loader',
'--conditions react-server',
'./node_modules/@redwoodjs/vite/dist/runRscFeServer.js',
],
{
cwd: getPaths().base,
stdio: 'inherit',
shell: true,
}
)
await Promise.all([apiPromise, fePromise])
}
export const bothSsrServerHandler = async (argv) => {
const { apiServerHandler } = await import('./serveApiHandler.js')
// TODO (STREAMING) Allow specifying port, socket and apiRootPath
const apiPromise = apiServerHandler({
...argv,
port: 8911,
apiRootPath: '/',
})
// TODO (STREAMING) More gracefully handle Ctrl-C
// Right now you get a big red error box when you kill the process
const fePromise = execa('yarn', ['rw-serve-fe'], {
cwd: getPaths().web.base,
stdio: 'inherit',
shell: true,
})
await Promise.all([apiPromise, fePromise])
}
export const bothServerHandler = async (options) => {
const { port, socket } = options
const tsServer = Date.now()
console.log(chalk.italic.dim('Starting API and Web Servers...'))
const fastify = createFastifyInstance()
process.on('exit', () => {
fastify?.close()
})
await fastify.register(redwoodFastifyWeb, {
redwood: {
...options,
},
})
const apiRootPath = coerceRootPath(getConfig().web.apiUrl)
await fastify.register(redwoodFastifyAPI, {
redwood: {
...options,
apiRootPath,
},
})
let listenOptions
if (socket) {
listenOptions = { path: socket }
} else {
listenOptions = {
port,
host: process.env.NODE_ENV === 'production' ? '0.0.0.0' : '::',
}
}
fastify.listen(listenOptions)
fastify.ready(() => {
console.log(chalk.italic.dim('Took ' + (Date.now() - tsServer) + ' ms'))
const on = socket
? socket
: chalk.magenta(`http://localhost:${port}${apiRootPath}`)
const webServer = chalk.green(`http://localhost:${port}`)
const apiServer = chalk.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 = chalk.magenta(`${apiRootPath}graphql`)
console.log(`GraphQL endpoint at ${graphqlEnd}`)
sendProcessReady()
})
}
function sendProcessReady() {
return process.send && process.send('ready')
}
const separator = chalk.hex('#ff845e')(
'------------------------------------------------------------------'
)
function logExperimentalHeader() {
console.log(
[
separator,
`🧪 ${chalk.green('Experimental Feature')} 🧪`,
separator,
'Using the experimental API server file at api/dist/server.js',
separator,
].join('\n')
)
}
function logSkippingFastifyWebServer() {
console.warn('')
console.warn('⚠️ Skipping Fastify web server ⚠️')
console.warn('⚠️ Using new RSC server instead ⚠️')
console.warn('')
}