Make lib/app.js a factory for lamassu-server
Extract all command line parsing to `bin/lamassu-server`. This will effectively make it easier to write tests for `lamassu-server`.
This commit is contained in:
parent
3932e17b2e
commit
02fcc42638
2 changed files with 68 additions and 40 deletions
26
bin/lamassu-server
Executable file
26
bin/lamassu-server
Executable file
|
|
@ -0,0 +1,26 @@
|
||||||
|
#!/usr/bin/env node
|
||||||
|
var createServer = require('../lib/app.js');
|
||||||
|
var argv = require('optimist').argv;
|
||||||
|
|
||||||
|
var options = {
|
||||||
|
postgres: process.env.DATABASE_URL
|
||||||
|
};
|
||||||
|
|
||||||
|
var port = process.env.PORT || 3000;
|
||||||
|
|
||||||
|
if (!argv.http) {
|
||||||
|
if (!argv.key || !argv.cert) {
|
||||||
|
console.error('--key and --cert are required');
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
options.https = {
|
||||||
|
key: fs.readFileSync(argv.key),
|
||||||
|
cert: fs.readFileSync(argv.cert)
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
var server = createServer(options);
|
||||||
|
server.listen(port, function () {
|
||||||
|
console.log('lamassu-server listening on port ' + port + ' ' + argv.http ? '(http)' : '(https)');
|
||||||
|
});
|
||||||
82
lib/app.js
82
lib/app.js
|
|
@ -21,62 +21,64 @@ var http = require('http');
|
||||||
var https = require('https');
|
var https = require('https');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var express = require('express');
|
var express = require('express');
|
||||||
var argv = require('optimist').argv;
|
|
||||||
var app = express();
|
|
||||||
var fs = require('fs');
|
var fs = require('fs');
|
||||||
var LamassuConfig = require('lamassu-config');
|
var LamassuConfig = require('lamassu-config');
|
||||||
var routes = require('./routes');
|
var routes = require('./routes');
|
||||||
|
|
||||||
var conString, dbConfig, config;
|
module.exports = function (options) {
|
||||||
|
var connectionString = options.postgres;
|
||||||
|
var app = express();
|
||||||
|
var server;
|
||||||
|
var config;
|
||||||
|
|
||||||
conString = process.env.DATABASE_URL || 'postgres://lamassu:lamassu@localhost/lamassu';
|
connectionString = connectionString ||
|
||||||
|
'postgres://lamassu:lamassu@localhost/lamassu';
|
||||||
|
|
||||||
config = new LamassuConfig(conString);
|
config = new LamassuConfig(connectionString);
|
||||||
|
|
||||||
var port = process.env.PORT || 3000;
|
app.use(express.logger());
|
||||||
app.use(express.logger());
|
app.use(express.favicon());
|
||||||
app.use(express.favicon());
|
app.use(express.bodyParser());
|
||||||
app.use(express.bodyParser());
|
app.use(express.methodOverride());
|
||||||
app.use(express.methodOverride());
|
|
||||||
|
|
||||||
config.load(function(err, conf) {
|
if (!options.https) {
|
||||||
if (err) { console.log(err); process.exit(1); }
|
server = http.createServer(app);
|
||||||
|
|
||||||
var authMiddleware = function (req, res, next) { return next(); };
|
|
||||||
|
|
||||||
if (argv.http) {
|
|
||||||
http.createServer(app).listen(port, function () {
|
|
||||||
console.log('Express server listening on port ' + port + ' (http)');
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
authMiddleware = function(req, res, next) {
|
var serverOptions = {
|
||||||
var fingerprint = req.connection.getPeerCertificate().fingerprint;
|
key: options.https.key,
|
||||||
var e = new Error('Unauthorized');
|
cert: options.https.cert,
|
||||||
e.status = 401;
|
|
||||||
|
|
||||||
config.isAuthorized(fingerprint, function (err, device) {
|
|
||||||
if (err) { return next(e); }
|
|
||||||
if (!device) { return next(e); }
|
|
||||||
req.device = device;
|
|
||||||
next();
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
var options = {
|
|
||||||
key: fs.readFileSync(argv.key),
|
|
||||||
cert: fs.readFileSync(argv.cert),
|
|
||||||
requestCert: true,
|
requestCert: true,
|
||||||
secureProtocol: 'TLSv1_method',
|
secureProtocol: 'TLSv1_method',
|
||||||
ciphers: 'AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH',
|
ciphers: 'AES128-GCM-SHA256:RC4:HIGH:!MD5:!aNULL:!EDH',
|
||||||
honorCipherOrder: true
|
honorCipherOrder: true
|
||||||
};
|
};
|
||||||
|
|
||||||
https.createServer(options, app).listen(port, function () {
|
server = https.createServer(options, app);
|
||||||
console.log('Express server listening on port ' + port + ' (https)');
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
routes.init(app, conf, config, authMiddleware);
|
config.load(function(err, conf) {
|
||||||
|
if (err) { console.log(err); process.exit(1); }
|
||||||
|
|
||||||
});
|
var authMiddleware = function (req, res, next) { return next(); };
|
||||||
|
|
||||||
|
if (options.https) {
|
||||||
|
authMiddleware = function(req, res, next) {
|
||||||
|
var fingerprint = req.connection.getPeerCertificate().fingerprint;
|
||||||
|
var e = new Error('Unauthorized');
|
||||||
|
e.status = 401;
|
||||||
|
|
||||||
|
config.isAuthorized(fingerprint, function (err, device) {
|
||||||
|
if (err) { return next(e); }
|
||||||
|
if (!device) { return next(e); }
|
||||||
|
req.device = device;
|
||||||
|
next();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
routes.init(app, conf, config, authMiddleware);
|
||||||
|
});
|
||||||
|
|
||||||
|
return server;
|
||||||
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue