forked from logux/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
191 lines (176 loc) · 5.83 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
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
188
189
190
191
'use strict'
const yargs = require('yargs')
const bunyan = require('bunyan')
const BaseServer = require('./base-server')
const humanReporter = require('./reporters/human/process')
const bunyanReporter = require('./reporters/bunyan/process')
function bunyanLog (logger, payload) {
const details = payload.details || {}
logger[payload.level](details, payload.msg)
}
function reportRuntimeError (e, app) {
if (app.options.reporter === 'bunyan') {
bunyanLog(app.options.bunyanLogger, bunyanReporter('error', app, e))
} else {
process.stderr.write(humanReporter('error', app, e))
}
}
yargs
.option('h', {
alias: 'host',
describe: 'Host to bind server.',
type: 'string'
})
.option('p', {
alias: 'port',
describe: 'Port to bind server',
type: 'number'
})
.option('k', {
alias: 'key',
describe: 'Path to SSL key ',
type: 'string'
})
.option('c', {
alias: 'cert',
describe: 'Path to SSL certificate',
type: 'string'
})
.option('r', {
alias: 'reporter',
describe: 'Reporter type',
type: 'string'
})
.epilog(
'Environment variables: ' +
'\n LOGUX_HOST, LOGUX_PORT, LOGUX_KEY, LOGUX_CERT, LOGUX_REPORTER'
)
.example('$0 --port 31337 --host 127.0.0.1')
.example('LOGUX_PORT=1337 $0')
.locale('en')
.help()
/**
* End-user API to create Logux server.
*
* @param {object} options Server options.
* @param {string} options.subprotocol Server current application
* subprotocol version in SemVer format.
* @param {string} options.supports npm’s version requirements for client
* subprotocol version.
* @param {string|number} [options.nodeId] Unique server ID. Be default,
* `server:` with compacted UUID.
* @param {string} [options.root=process.cwd()] Application root to load files
* and show errors.
* @param {number} [options.timeout=20000] Timeout in milliseconds
* to disconnect connection.
* @param {number} [options.ping=10000] Milliseconds since last message to test
* connection by sending ping.
* @param {function} [options.timer] Timer to use in log. Will be default
* timer with server `nodeId`, by default.
* @param {"text"|"bunyan"} [options.reporter="text"] Report process/errors to
* CLI in text or bunyan
* logger in JSON.
* @param {Logger} [options.bunyanLogger] Bunyan logger with custom settings
* @param {Store} [options.store] Store to save log. Will be `MemoryStore`,
* by default.
* @param {"production"|"development"} [options.env] Development or production
* server mode. By default,
* it will be taken from
* `NODE_ENV` environment
* variable. On empty
* `NODE_ENV` it will
* be `"development"`.
*
* @example
* import { Server } from 'logux-server'
*
* let env = process.env.NODE_ENV || 'development'
* let envOptions = {}
* if (env === 'production') {
* envOptions = {
* cert: 'cert.pem',
* key: 'key.pem'
* }
* }
*
* const app = new Server(Object.assign({
* subprotocol: '1.0.0',
* supports: '1.x || 0.x',
* root: __dirname
* }, envOptions))
*
* app.listen()
*
* @extends BaseServer
*/
class Server extends BaseServer {
constructor (options) {
options.pid = process.pid
options.reporter = options.reporter || 'text'
if (options.reporter === 'bunyan' && !options.bunyanLogger) {
options.bunyanLogger = bunyan.createLogger({ name: 'logux-server' })
}
let reporter
if (options.reporter === 'bunyan') {
reporter = function () {
bunyanLog(options.bunyanLogger, bunyanReporter.apply(null, arguments))
}
} else {
reporter = function () {
process.stderr.write(humanReporter.apply(null, arguments))
}
}
super(options, reporter)
const onError = e => {
this.emitter.emit('error', e)
this.destroy().then(() => {
process.exit(1)
})
}
process.on('uncaughtException', onError)
process.on('unhandledRejection', onError)
const onExit = () => {
this.destroy().then(() => {
process.exit(0)
})
}
process.on('SIGINT', onExit)
this.unbind.push(() => {
process.removeListener('SIGINT', onExit)
})
}
listen () {
const origin = BaseServer.prototype.listen
return origin.apply(this, arguments).catch(e => {
reportRuntimeError(e, this)
process.exit(1)
})
}
/**
* Load options from command-line arguments and/or environment
*
* @param {object} process Current process object.
* @param {object} options Server options.
* @return {object} Parsed options object.
*
* @example
* const app = new Server(Server.loadOptions(process, {
* subprotocol: '1.0.0',
* supports: '1.x',
* root: __dirname,
* port: 31337
* }))
*/
static loadOptions (process, options) {
options = options || { }
const argv = yargs.parse(process.argv)
const env = process.env
options.host = options.host || argv.h || env.LOGUX_HOST
options.port = parseInt(options.port || argv.p || env.LOGUX_PORT, 10)
options.cert = options.cert || argv.c || env.LOGUX_CERT
options.key = options.key || argv.k || env.LOGUX_KEY
options.reporter = options.reporter || argv.r || env.LOGUX_REPORTER
return options
}
}
module.exports = Server