-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
62 lines (49 loc) · 2.65 KB
/
server.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
'use strict'
import dotenv from 'dotenv'
import colors from 'colors'
import https from 'https'
import http from 'http'
import fs from 'fs'
import appBuilder from './app.js'
dotenv.config()
const SECURED = process.env.SECURED === 'yes'
const credentials = {
key: SECURED ? fs.readFileSync(process.env.SECURED_PRIVKEY) : null,
cert: SECURED ? fs.readFileSync(process.env.SECURED_CERT) : null
}
const startServer = async () => {
console.log(`Server starting in ${colors.red(process.env.NODE_ENV || 'development')} mode...`)
const { app, apolloServer, buildSubscriptionServer } = await appBuilder()
if (SECURED) {
const httpsServer = https.createServer(credentials, app)
const subscriptionServer = buildSubscriptionServer(httpsServer)
httpsServer.listen({ host: process.env.HOST, port: process.env.PORT }, () => {
console.log(` ${colors.brightGreen('✱')} ${colors.grey('Graphql API ready at')} ${colors.brightGreen('https')}://${colors.brightWhite(process.env.HOST)}:${colors.brightWhite(process.env.PORT)}${colors.brightCyan(apolloServer.graphqlPath)}`)
console.log(` ${colors.brightGreen('✱')} ${colors.grey('Graphql subscriptions ready at')} ${colors.brightGreen('wss')}://${colors.brightWhite(process.env.HOST)}:${colors.brightWhite(process.env.PORT)}${colors.brightCyan(subscriptionServer.wsServer.options.path)}`)
})
} else {
const httpServer = http.createServer(app)
const subscriptionServer = buildSubscriptionServer(httpServer)
httpServer.listen({ host: process.env.HOST, port: process.env.PORT }, () => {
console.log(` ${colors.yellow('✱')} ${colors.grey('Graphql API ready at')} ${colors.yellow('http')}://${colors.brightWhite(process.env.HOST)}:${colors.brightWhite(process.env.PORT)}${colors.brightCyan(apolloServer.graphqlPath)}`)
console.log(` ${colors.yellow('✱')} ${colors.grey('Graphql subscriptions ready at')} ${colors.yellow('ws')}://${colors.brightWhite(process.env.HOST)}:${colors.brightWhite(process.env.PORT)}${colors.brightCyan(subscriptionServer.wsServer.options.path)}`)
})
}
function exitHandler (options, err) {
if (options.cleanup) {
// cleanly close other connections here
}
if (err && err.stack) {
console.log(err.stack)
}
if (options.exit) {
process.exit()
}
}
process.on('exit', exitHandler.bind(null, { cleanup: true, exit: true }))
process.on('SIGINT', exitHandler.bind(null, { exit: true }))
process.on('SIGUSR1', exitHandler.bind(null, { exit: true }))
process.on('SIGUSR2', exitHandler.bind(null, { exit: true }))
process.on('uncaughtException', exitHandler.bind(null, { exit: true }))
}
startServer()