2015-02-02 09:52:31 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
var http = require('http');
|
2015-02-04 08:33:17 +00:00
|
|
|
var path = require('path');
|
2015-02-02 09:52:31 +00:00
|
|
|
|
2016-12-15 22:43:52 +00:00
|
|
|
var _ = require('lodash');
|
|
|
|
var nStatic = require('node-static');
|
2015-02-02 09:52:31 +00:00
|
|
|
|
|
|
|
var config = require('./config');
|
|
|
|
var logger = require('./logger');
|
|
|
|
|
|
|
|
|
2016-12-15 22:55:59 +00:00
|
|
|
// The different config sources sometimes manipulate different setting names.
|
|
|
|
// E.g. command line flags maniuplate root settings, but config files can
|
|
|
|
// manipulate settings at the prod/debug level. Resolve all of these into a
|
|
|
|
// final object of settings.
|
|
|
|
function _resolveFinalSettings(settings) {
|
|
|
|
var finalSettings = {};
|
|
|
|
var env = config.get('NODE_ENV');
|
|
|
|
|
2015-02-02 09:52:31 +00:00
|
|
|
if (!settings) {
|
|
|
|
settings = config.get();
|
|
|
|
}
|
|
|
|
if (!settings[env]) {
|
|
|
|
settings[env] = {};
|
|
|
|
}
|
|
|
|
|
2016-12-15 22:55:59 +00:00
|
|
|
// For 'name', 'file', 'serve', and 'port', default to the global setting rather than an
|
|
|
|
// individual environment's setting, because it might have been set via command-line flags
|
|
|
|
_(['name', 'file', 'serve', 'port']).forEach(function (field) {
|
|
|
|
finalSettings[field] = settings[field] || settings[env][field];
|
|
|
|
});
|
|
|
|
|
|
|
|
// For 'staticOptions', there are no command-line flags, so individual configuration options
|
|
|
|
// override global defaults where set
|
|
|
|
finalSettings.staticOptions = _.defaultsDeep(settings.staticOptions, settings[env].staticOptions);
|
|
|
|
|
|
|
|
// Make a template function so we can just pass that in downstream
|
|
|
|
finalSettings.template = (settings.template || settings[env].template) ?
|
|
|
|
function (contents) {
|
|
|
|
// Note: template is run with _original_, non-resolved settings
|
|
|
|
return _.template(contents)(settings);
|
|
|
|
} : null;
|
|
|
|
|
|
|
|
return finalSettings;
|
|
|
|
}
|
|
|
|
|
|
|
|
function generateBase(file, finalSettings) {
|
|
|
|
if (!file) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.endsWith(file, 'js')) {
|
|
|
|
return require(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
var contents = fs.readFileSync(file);
|
|
|
|
if (finalSettings.template) {
|
|
|
|
contents = finalSettings.template(contents);
|
|
|
|
}
|
|
|
|
|
|
|
|
return function(req, res) {
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/html'});
|
|
|
|
res.end(contents);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
var spserver = function (settings) {
|
|
|
|
var finalSettings = _resolveFinalSettings(settings);
|
|
|
|
|
|
|
|
var fileServer = new nStatic.Server(
|
|
|
|
path.resolve(finalSettings.serve),
|
|
|
|
finalSettings.staticOptions
|
|
|
|
);
|
2015-02-04 08:33:17 +00:00
|
|
|
|
2016-12-15 22:55:59 +00:00
|
|
|
var base = generateBase(path.resolve(finalSettings.file), finalSettings);
|
2015-02-02 09:52:31 +00:00
|
|
|
|
|
|
|
var server = http.createServer(function (req, res) {
|
|
|
|
logger.debug('[REQ] GET:', req.url);
|
|
|
|
var startTime = new Date().getTime();
|
|
|
|
|
2016-12-14 23:04:28 +00:00
|
|
|
var done = function () {
|
2015-02-02 09:52:31 +00:00
|
|
|
var requestTime = new Date().getTime() - startTime;
|
|
|
|
logger.debug('[RES] GET:', req.url, '(' + res.statusCode + ') took', requestTime, 'ms');
|
|
|
|
};
|
|
|
|
|
|
|
|
res.addListener('finish', done);
|
|
|
|
res.addListener('close', done);
|
|
|
|
|
|
|
|
req.addListener('end', function () {
|
2016-12-14 23:04:28 +00:00
|
|
|
fileServer.serve(req, res, function (err) {
|
|
|
|
if (err) {
|
|
|
|
if (err.status === 404 && base) {
|
|
|
|
return base(req, res);
|
|
|
|
} else {
|
|
|
|
logger.error(err);
|
|
|
|
|
|
|
|
res.writeHead(err.status, err.headers);
|
|
|
|
res.end();
|
|
|
|
}
|
2015-02-02 09:52:31 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}).resume();
|
|
|
|
});
|
|
|
|
|
2016-12-15 22:55:59 +00:00
|
|
|
server.listen(finalSettings.port);
|
|
|
|
|
|
|
|
logger.info(
|
|
|
|
'Started single-page server: ' + finalSettings.name +
|
|
|
|
', base file: ' + finalSettings.file +
|
|
|
|
', static folder: ' + finalSettings.serve +
|
|
|
|
', port: ' + finalSettings.port
|
|
|
|
);
|
2015-02-02 09:52:31 +00:00
|
|
|
|
2016-12-15 22:55:59 +00:00
|
|
|
return server;
|
2015-02-02 09:52:31 +00:00
|
|
|
};
|
|
|
|
|
2015-02-02 15:41:06 +00:00
|
|
|
spserver.generateBase = generateBase;
|
|
|
|
|
|
|
|
module.exports = spserver;
|