dl
Version:
DreamLab Libs
165 lines (143 loc) • 4.89 kB
JavaScript
/**
* @overview OpalServer
* @copyright Dreamlab Onet.pl sp. z. o o 2012
*/
var JsonRpcServer = require('core').jsonrpc.JsonRpcServer,
Server = require('core').http.Server,
YAML = require('core').yaml.YAML,
fs = require('fs'),
path = require('path'),
HealthCheckMethod = require('../jsonrpc/HealthCheckMethod.js').HealthCheckMethod,
GetFunctionListMethod = require('../jsonrpc/GetFunctionListMethod.js').GetFunctionListMethod,
OpalServerRequestProcessor = require('./OpalServerRequestProcessor.js').OpalServerRequestProcessor;
/**
* @class OpalServer
* @classdesc OpalServer
* @extends JsonRpcServer
*
* @requires JsonRpcServer
* @requires Server
* @requires YAML
* @requires fs
* @requires path
* @requires HealthCheckMethod
* @requires OpalServerRequestProcessor
*
* @return {OpalServer}
*/
var OpalServer = function() {
// cant call JsonRpcServer.call because of different request processor class
this._methods = {};
Server.call(this, OpalServerRequestProcessor);
// ---
// parsing applications app.yaml
var appYaml = YAML.parseFileSync('./app.yaml').pop();
// add methods from appYaml
this._addMethods(appYaml);
if (!process.env['DISABLE_EVENTLOOP_MEASURE']) {
this.startEventLoopMeasure();
}
};
OpalServer.prototype = Object.create(JsonRpcServer.prototype);
/**
* Adding methods in interfaces from app.yaml data
*
* @param {Object} appYaml
* @returns {undefined}
* @method
* @private
*/
OpalServer.prototype._addMethods = function (appYaml) {
var interfacesPath = 'lib/interfaces/';
var interfaces = Object.keys(appYaml.interfaces);
var suffix = '.js';
console.info('Supported interfaces:', interfaces);
if (appYaml && interfaces.length > 0) {
for(var i=0, max=interfaces.length; i<max; i++) {
var fileNames = fs.readdirSync('./' + interfacesPath + interfaces[i] + '/');
for (var j=0; j<fileNames.length; j++) {
if (fileNames[j].indexOf(suffix, fileNames[j].length - suffix.length) == -1) {
continue;
}
var filePath = process.cwd() + '/' + interfacesPath + interfaces[i] + '/' + fileNames[j]
var moduleName = path.basename(fileNames[j], '.js');
var methodName = path.basename(moduleName, 'Method');
var methodNameCamel = methodName[0].toLowerCase() + methodName.substr(1);
var MethodClass = require(filePath)[moduleName];
this.addMethod(interfaces[i] + ':' + methodNameCamel, MethodClass, interfaces[i]);
}
this.addMethod(interfaces[i] + ':' + '_healthcheck', HealthCheckMethod, interfaces[i]);
this.addMethod(interfaces[i] + ':' + '_getfunctionlist', GetFunctionListMethod, interfaces[i]);
}
} else {
console.error('OpalServer/constructor: Cant find any interface definition in app.yaml');
process.exit(1);
}
};
/**
* Returns JsonRpc method
* @param {String} methodName
* @param {Request} request
* @returns {JsonRpcMethod|false}
* @method
*/
OpalServer.prototype.getMethod = function (methodName, request) {
var interfaceName = this.getInterface(request);
var name = interfaceName + ':' + methodName;
if (this._methods.hasOwnProperty(name)) {
return this._methods[name];
}
return false;
};
/**
* Returns JsonRpc method
* @param {String} interfaceName
* @returns {Object}
* @method
*/
OpalServer.prototype.getMethods = function (interfaceName) {
var methods = Object.keys(this._methods);
var retVal = { list: [] };
for(var i=0, max=methods.length; i<max; i++) {
var method = methods[i].split(':');
if(method[0] == interfaceName) {
retVal.list.push({
'funcname': method[1],
'description': methods[i]
});
}
}
return retVal;
};
/**
* Returns Opal interface name
* @param {Request} request
* @returns {String}
* @throws OpalServer.Errors.NO_INTERFACE_IN_HOST_HEADER
* @method
*/
OpalServer.prototype.getInterface = function (request) {
var hostHeader = request.getHeader('host');
if (hostHeader) {
var hostParts = hostHeader.split('.');
hostParts.pop();
hostParts.pop();
hostParts.pop();
return hostParts.join('.');
}
throw OpalServer.Errors.NO_INTERFACE_IN_HOST_HEADER
};
exports.OpalServer = OpalServer;
/**
* @static
* @constant
* @namespace
*/
OpalServer.Errors = {};
/**
* @static
* @constant
* @type {String}
* @default
*/
OpalServer.Errors.NO_INTERFACE_IN_HOST_HEADER = 'OpalServer.Errors.NO_INTERFACE_IN_HOST_HEADER';