140 lines
3.2 KiB
JavaScript
140 lines
3.2 KiB
JavaScript
|
var express = require('express');
|
||
|
var bodyParser = require('body-parser');
|
||
|
var path = require('path');
|
||
|
var debug = require('debug')('ff:tcrud:setup');
|
||
|
var configRegistry = require('./config-registry');
|
||
|
var buildServerApi = require('./build-server-api');
|
||
|
var buildServerApiTEntity = require('./build-server-api-tentity');
|
||
|
|
||
|
var mod = (function () {
|
||
|
|
||
|
var server = null;
|
||
|
|
||
|
var expressCreate = function() {
|
||
|
if (server === null) {
|
||
|
server = express();
|
||
|
} else {
|
||
|
throw new Error('Can only create once.');
|
||
|
}
|
||
|
};
|
||
|
|
||
|
var expressBuild = function(options) {
|
||
|
if (!options) {
|
||
|
options = {
|
||
|
viewsDir: null
|
||
|
};
|
||
|
}
|
||
|
|
||
|
var viewsDirs = [];
|
||
|
viewsDirs.push(path.join(__dirname, 'www_views'));
|
||
|
if (options.viewsDir) {
|
||
|
viewsDirs.push(options.viewsDir);
|
||
|
}
|
||
|
server.set('view engine', 'ejs');
|
||
|
server.set('views', viewsDirs);
|
||
|
debug('build.views: '+viewsDirs);
|
||
|
|
||
|
server.use(bodyParser.json());
|
||
|
server.use(bodyParser.urlencoded());
|
||
|
debug('build.bodyParser');
|
||
|
|
||
|
buildServerApi.build(server);
|
||
|
buildServerApiTEntity.build(server);
|
||
|
debug('build.serverApi');
|
||
|
};
|
||
|
|
||
|
var expressListen = function() {
|
||
|
var tcrud = configRegistry.getRootTEntity();
|
||
|
var serverPortIndex = tcrud.tmeta.tserver.thost.indexOf(':',6); // TODO: make robust
|
||
|
var serverPort = tcrud.tmeta.tserver.thost.substring(serverPortIndex+1);
|
||
|
debug('listen port: '+serverPort);
|
||
|
server.listen(serverPort);
|
||
|
return serverPort;
|
||
|
}
|
||
|
|
||
|
return function TCrudSetup() {
|
||
|
|
||
|
this.pluginLoad = function(plugin) {
|
||
|
configRegistry.assertPhaseInit();
|
||
|
configRegistry.pluginLoad(plugin);
|
||
|
};
|
||
|
|
||
|
this.pluginEnable = function(pluginKey) {
|
||
|
configRegistry.assertPhaseInit();
|
||
|
configRegistry.pluginEnable(pluginKey);
|
||
|
};
|
||
|
|
||
|
this.pluginEnableAll = function() {
|
||
|
configRegistry.assertPhaseInit();
|
||
|
configRegistry.pluginEnable('.*');
|
||
|
};
|
||
|
|
||
|
this.pluginDisable = function(pluginKey) {
|
||
|
configRegistry.assertPhaseInit();
|
||
|
configRegistry.pluginEnable(pluginKey,false);
|
||
|
};
|
||
|
|
||
|
|
||
|
|
||
|
this.phaseConfig = function() {
|
||
|
configRegistry.assertPhaseInit();
|
||
|
configRegistry.phaseNext();
|
||
|
};
|
||
|
|
||
|
this.phaseServer = function() {
|
||
|
configRegistry.assertPhaseConfig();
|
||
|
configRegistry.phaseNext();
|
||
|
};
|
||
|
|
||
|
this.phaseServerUp = function() {
|
||
|
configRegistry.assertPhaseServer();
|
||
|
configRegistry.phaseNext();
|
||
|
|
||
|
var ctx = {
|
||
|
server: server
|
||
|
}
|
||
|
configRegistry.pluginCall('configPostBoot',ctx);
|
||
|
};
|
||
|
|
||
|
|
||
|
this.expressSimple = function(options) {
|
||
|
expressCreate();
|
||
|
expressBuild(options);
|
||
|
return expressListen();
|
||
|
}
|
||
|
|
||
|
this.expressCreate = function() {
|
||
|
configRegistry.assertPhaseServer();
|
||
|
expressCreate();
|
||
|
return server;
|
||
|
};
|
||
|
|
||
|
this.expressBuild = function(options) {
|
||
|
configRegistry.assertPhaseServer();
|
||
|
expressBuild(options);
|
||
|
};
|
||
|
|
||
|
this.expressListen = function() {
|
||
|
configRegistry.assertPhaseServer();
|
||
|
return expressListen();
|
||
|
};
|
||
|
};
|
||
|
})();
|
||
|
|
||
|
module.exports = new mod;
|
||
|
|
||
|
//
|
||
|
//ConfigSetupExpress.prototype.renderTemplatePath = function(viewPath) {
|
||
|
// if (!viewPath) {
|
||
|
// viewPath = '';
|
||
|
// }
|
||
|
// return function (req, res) {
|
||
|
// res.locals.query = req.query;
|
||
|
// var qi = req.url.indexOf('?');
|
||
|
// if (qi === -1) {
|
||
|
// qi = req.url.length;
|
||
|
// }
|
||
|
// res.render(viewPath + req.url.substring(req.route.path.length-1, qi));
|
||
|
// };
|
||
|
//}
|