-
Notifications
You must be signed in to change notification settings - Fork 7
/
server.js
104 lines (78 loc) · 2.15 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 (c) 2017, Joyent, Inc.
*/
/*
* Main entry-point for the firewall API.
*/
'use strict';
var fwapi = require('./lib/app');
var assert = require('assert-plus');
var bunyan = require('bunyan');
var fs = require('fs');
var restify = require('restify');
// --- Globals
var CONFIG_FILE = __dirname + '/config.json';
// --- Functions
/**
* Loads the config, throwing an error if the config is incomplete / invalid
*/
function loadConfig(configFile) {
assert.string(configFile, 'configFile');
var config = JSON.parse(fs.readFileSync(configFile, 'utf-8'));
if (!config.hasOwnProperty('port')) {
config.port = 80;
}
if (config.hasOwnProperty('fwrule_version')) {
assert.number(config.fwrule_version, 'config.fwrule_version');
} else {
config.fwrule_version = 1;
}
assert.optionalNumber(config.maxHttpSockets, 'config.maxHttpSockets');
return config;
}
/**
* Main entry point
*/
function main() {
var server;
var log = bunyan.createLogger({
name: 'fwapi',
level: 'info',
serializers: restify.bunyan.serializers
});
log.info('Loading config file: %s', CONFIG_FILE);
var config = loadConfig(CONFIG_FILE);
if (config.logLevel) {
log.level(config.logLevel);
}
try {
server = fwapi.create({
config: config,
log: log
});
} catch (err) {
log.error(err, 'Error creating server');
process.exit(1);
}
server.listen(function (lErr) {
if (lErr) {
throw lErr;
}
var addr = server.info();
log.info('%s listening on <http://%s:%s>',
addr.name, addr.address, addr.port);
});
// Try to ensure we clean up properly on exit.
process.on('SIGINT', function _cleanup() {
log.info('SIGINT: cleaning up');
return server.close(function () {
process.exit(1);
});
});
}
main();