2015-02-02 09:52:31 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
var http = require('http');
|
|
|
|
var _ = require('lodash');
|
|
|
|
var nStatic = require('node-static');
|
2015-02-04 08:33:17 +00:00
|
|
|
var path = require('path');
|
2015-02-02 09:52:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
var config = require('./config');
|
|
|
|
var logger = require('./logger');
|
|
|
|
|
|
|
|
var env = config.get('NODE_ENV');
|
|
|
|
|
2016-12-14 23:04:28 +00:00
|
|
|
var spserver = function (settings) {
|
2015-02-02 09:52:31 +00:00
|
|
|
if (!settings) {
|
|
|
|
settings = config.get();
|
|
|
|
}
|
|
|
|
if (!settings[env]) {
|
|
|
|
settings[env] = {};
|
|
|
|
}
|
|
|
|
|
2015-02-04 08:33:17 +00:00
|
|
|
var fileServer = new nStatic.Server(path.resolve(settings.serve || settings[env].server));
|
|
|
|
|
|
|
|
var base = generateBase(path.resolve(settings.file || settings[env].file), settings);
|
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();
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(settings.port || settings[env].port);
|
|
|
|
|
|
|
|
logger.info('Static server',
|
|
|
|
settings.name,
|
|
|
|
'is listening on port',
|
|
|
|
settings.port || settings[env].port,
|
|
|
|
'with public folder',
|
|
|
|
settings.serve || settings[env].serve);
|
|
|
|
};
|
|
|
|
|
2015-02-02 15:41:06 +00:00
|
|
|
spserver.generateBase = generateBase;
|
|
|
|
|
2015-02-02 09:52:31 +00:00
|
|
|
function generateBase(file, settings) {
|
|
|
|
if (!file) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.endsWith(file, 'js')) {
|
2015-02-04 08:33:17 +00:00
|
|
|
return require(file);
|
2015-02-02 09:52:31 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 08:33:17 +00:00
|
|
|
var contents = fs.readFileSync(file);
|
2015-02-02 09:52:31 +00:00
|
|
|
|
2015-02-02 15:41:06 +00:00
|
|
|
if (settings.template || settings[env] && settings[env].template) {
|
2015-02-02 09:52:31 +00:00
|
|
|
contents = _.template(contents)(settings);
|
|
|
|
}
|
|
|
|
|
|
|
|
return function(req, res) {
|
|
|
|
res.writeHead(200, {'Content-Type': 'text/html'});
|
|
|
|
res.end(contents);
|
|
|
|
};
|
|
|
|
}
|
2015-02-02 15:41:06 +00:00
|
|
|
|
|
|
|
module.exports = spserver;
|