forked from mozilla/zippy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
73 lines (58 loc) · 1.44 KB
/
main.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
//var bunyan = require('bunyan');
var getopt = require('posix-getopt');
var zippy = require('./lib');
var NAME = 'zippyapp';
///--- Helpers
/**
* Standard POSIX getopt-style options parser.
*/
function parseOptions() {
var option;
var opts = {};
var parser = new getopt.BasicParser('hvnp:', process.argv);
while ((option = parser.getopt()) !== undefined) {
switch (option.option) {
case 'h':
usage();
break;
case 'n':
opts.noAuth = true;
break;
case 'p':
opts.port = parseInt(option.optarg, 10);
break;
default:
usage('invalid option: ' + option.option);
break;
}
}
return (opts);
}
function usage(msg) {
if (msg) {
console.error(msg);
}
var str = 'usage: ' + NAME + ' [-v] [-n] [-p port]\n' +
' -v stands for verbosity\n' +
' -n stands for noAuth (removing OAuth middleware)';
console.error(str);
process.exit(msg ? 1 : 0);
}
var http = require("http");
function main(options) {
options = options || parseOptions();
var server = http.createServer(zippy.createApp({
options: options,
}));
var port = options.port || 8080;
server.listen(port, function onServerStart() {
var addr = this.address();
console.log('listening at %s:%s', addr.address, addr.port);
});
}
// A rough node equivalent of python's if __name__ == "__main__".
if (!module.parent) {
main();
} else {
module.exports = main;
}