-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
141 lines (123 loc) · 3.06 KB
/
index.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
/* eslint-disable no-console */
'use strict'
const fs = require('fs')
const express = require('express')
const app = express()
const assert = require('assert')
/**
* Initialize the server. Performed before startup.
*/
let server
let _logger
/**
* Closing the server
* @param signal the log message depending on the given signal.
*/
function serverClose(
signal,
done = () => {
process.exit(0)
}
) {
_logger.info('Process received ' + signal + ', exiting ....')
if (server) {
server.close(done)
} else {
done()
}
}
function start(params = {}) {
const { logger, useSsl, passphrase, key, ca, pfx, cert, port = 3000 } = params
// Set default params
_logger = logger || {
info(msg) {
console.log(msg)
},
debug(msg) {
console.log(msg)
},
warn(msg) {
console.warn(msg)
},
trace(msg) {
console.log(msg)
},
error(msg) {
console.error(msg)
},
}
if (useSsl) {
if (!pfx && !cert) {
assert(false, 'Missing pfx or cert required for SSL')
}
if (pfx) {
assert(passphrase, 'When using pfx we also require a passphrase. Should be path to file')
}
if (cert) {
assert(ca, 'Missing ca required for SSL with cert. Should be path to file')
assert(key, 'Missing key required for SSL with cert. Should be path to file')
}
}
if (cert || pfx) {
assert(useSsl, 'You are passing a cert or pfx but not enabling SSL')
}
let options
if (useSsl) {
if (pfx) {
let password = fs.readFileSync(passphrase) + ''
password = password.trim()
_logger.info('Setting key for HTTPS(pfx): ' + pfx)
options = {
pfx: fs.readFileSync(pfx),
passphrase: password,
}
} else {
options = {
key: fs.readFileSync(key),
cert: fs.readFileSync(cert),
ca: fs.readFileSync(ca),
}
_logger.info('Setting key for HTTPS(cert): ' + cert)
}
}
let serverTypeMsg
if (useSsl) {
serverTypeMsg = 'using Secure HTTPS server'
server = require('https').createServer(options, app)
} else {
server = require('http').createServer(app)
serverTypeMsg = 'using unsecure HTTP server'
if (process.env.NODE_ENV === 'production') {
_logger.warn('>>>>>>>>>>> ' + serverTypeMsg + '<<<<<<<<<<')
}
}
return new Promise((resolve, reject) => {
try {
server.listen(port, () => {
_logger.info('*** *************************')
_logger.info('*** SERVER STARTED')
_logger.info('*** ' + serverTypeMsg)
_logger.info('*** Listening on port: ' + port)
_logger.info('*** *************************')
// eslint-disable-next-line prefer-rest-params
resolve.apply(resolve, arguments)
})
} catch (e) {
reject(e)
}
})
}
module.exports = app
// .start and .close returns a promise
module.exports.start = start
module.exports.close = function _close() {
return new Promise((resolve, reject) => {
try {
serverClose('SIGHUP', () => {
resolve()
})
} catch (e) {
reject(e)
}
})
}