forked from TritonDataCenter/sdc-papi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
104 lines (80 loc) · 2.07 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
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/*
* Copyright 2019 Joyent, Inc.
*/
/*
* Main entry-point for the packages HTTP API. Parses the command line, config
* file, and then loads up the API.
*/
var restify = require('restify');
var Logger = require('bunyan');
var nopt = require('nopt');
var path = require('path');
var papi = require('./lib/papi');
var DEFAULT_CFG = __dirname + '/etc/config.json';
var LOG;
var PARSED;
var opts = {
'debug': Boolean,
'file': String,
'port': Number,
'help': Boolean
};
var shortOpts = {
'd': ['--debug'],
'f': ['--file'],
'p': ['--port'],
'h': ['--help']
};
/*
* Optionally display a message, then how to use this command, then exit.
*/
function usage(code, message) {
var _opts = '';
Object.keys(shortOpts).forEach(function (k) {
var longOpt = shortOpts[k][0].replace('--', '');
var type = opts[longOpt].name || 'string';
if (type && type === 'boolean') {
type = '';
}
type = type.toLowerCase();
_opts += ' [--' + longOpt + ' ' + type + ']';
});
var msg = (message ? message + '\n' : '') +
'usage: ' + path.basename(process.argv[1]) + _opts;
process.stderr.write(msg + '\n');
process.exit(code);
}
/*
* Fire up HTTP server
*/
function run() {
papi.createServer({
config: PARSED.file || DEFAULT_CFG,
overrides: PARSED,
log: LOG
}, function (err, server) {
if (err) {
LOG.error(err, 'failed to start server');
process.abort();
}
LOG.info('Packages API listening at %s', server.url);
});
}
// --- Mainline
PARSED = nopt(opts, shortOpts, process.argv, 2);
if (PARSED.help) {
usage(0);
}
LOG = new Logger({
level: (PARSED.debug ? 'trace' : 'info'),
name: 'PackagesAPI',
stream: process.stderr,
serializers: restify.bunyan.serializers
});
// There we go!:
run();