-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.ts
115 lines (94 loc) · 3.03 KB
/
server.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
import { parseArgs } from 'node:util'
import path from 'path'
import chalk from 'chalk'
import { config } from 'dotenv-defaults'
import Fastify from 'fastify'
import {
coerceRootPath,
redwoodFastifyWeb,
redwoodFastifyAPI,
redwoodFastifyGraphQLServer,
DEFAULT_REDWOOD_FASTIFY_CONFIG,
} from '@redwoodjs/fastify'
import { getPaths, getConfig } from '@redwoodjs/project-config'
import directives from 'src/directives/**/*.{js,ts}'
import sdls from 'src/graphql/**/*.sdl.{js,ts}'
import services from 'src/services/**/*.{js,ts}'
// Import if using RedwoodJS authentication
// import { authDecoder } from '@redwoodjs/<your-auth-provider>'
// import { getCurrentUser } from 'src/lib/auth'
import { logger } from 'src/lib/logger'
// Import if using RedwoodJS Realtime via `yarn rw exp setup-realtime`
// import { realtime } from 'src/lib/realtime'
async function serve() {
// Parse server file args
const { values: args } = parseArgs({
options: {
['enable-web']: {
type: 'boolean',
default: false,
},
},
})
const { ['enable-web']: enableWeb } = args
// Load .env files
const redwoodProjectPaths = getPaths()
const redwoodConfig = getConfig()
const apiRootPath = enableWeb ? coerceRootPath(redwoodConfig.web.apiUrl) : ''
const port = enableWeb ? redwoodConfig.web.port : redwoodConfig.api.port
const tsServer = Date.now()
config({
path: path.join(redwoodProjectPaths.base, '.env'),
defaults: path.join(redwoodProjectPaths.base, '.env.defaults'),
multiline: true,
})
console.log(chalk.italic.dim('Starting API and Web Servers...'))
// Configure Fastify
const fastify = Fastify({
...DEFAULT_REDWOOD_FASTIFY_CONFIG,
})
if (enableWeb) {
await fastify.register(redwoodFastifyWeb)
}
await fastify.register(redwoodFastifyAPI, {
redwood: {
apiRootPath,
},
})
await fastify.register(redwoodFastifyGraphQLServer, {
// If authenticating, be sure to import and add in
// authDecoder,
// getCurrentUser,
loggerConfig: {
logger: logger,
},
graphiQLEndpoint: enableWeb ? '/.redwood/functions/graphql' : '/graphql',
sdls,
services,
directives,
allowIntrospection: true,
allowGraphiQL: true,
// Configure if using RedwoodJS Realtime
// realtime,
})
const host = process.env.NODE_ENV === 'production' ? '0.0.0.0' : '::'
// Start
fastify.listen({ port, host })
fastify.ready(() => {
console.log(chalk.italic.dim('Took ' + (Date.now() - tsServer) + ' ms'))
const on = chalk.magenta(`http://${host}:${port}${apiRootPath}`)
if (enableWeb) {
const webServer = chalk.green(`http://${host}:${port}`)
console.log(`Web server started on ${webServer}`)
}
const apiServer = chalk.magenta(`http://${host}:${port}`)
console.log(`API serving from ${apiServer}`)
console.log(`API listening on ${on}`)
const graphqlEnd = chalk.magenta(`${apiRootPath}graphql`)
console.log(`GraphQL function endpoint at ${graphqlEnd}`)
})
process.on('exit', () => {
fastify.close()
})
}
serve()