// Copyright 2015 rain1017. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or // implied. See the License for the specific language governing // permissions and limitations under the License. See the AUTHORS file // for names of contributors. 'use strict'; var _ = require('lodash'); var path = require('path'); var logger = require('memdb-client').logger.getLogger('route', __filename); var util = require('util'); var requireChildren = require('../../utils/require-children'); var dispatcher = require('../../utils/dispatcher'); var DEFAULT_BASE_PATH = 'app/routes'; var Routes = function(app, opts){ opts = app.get('routesConfig') || opts || {}; this.app = app; this.basePath = opts.basePath || DEFAULT_BASE_PATH; }; var proto = Routes.prototype; proto.name = 'routes'; proto.start = function(cb){ var basePath = path.join(this.app.getBase(), this.basePath); this.loadRoutes(basePath); cb(); }; proto.stop = function(force, cb){ cb(); }; proto.loadRoutes = function(basePath){ var modules = requireChildren(module, basePath); var self = this; Object.keys(modules).forEach(function(serverType){ var route = modules[serverType]; self.app.route(serverType, function(sessionOrParam, msg, app, cb){ var servers = app.getServersByType(serverType); if(servers.length === 0){ cb(new Error('No server for type ' + serverType)); } var key = null, method = null; if(msg.namespace === 'sys' && msg.service === 'msgRemote' && msg.method === 'forwardMessage'){ // handler message method = msg.args[0].route; var body = msg.args[0].body; if(typeof(route.handler) === 'function'){ key = route.handler(sessionOrParam, method, body); } } else{ // remote message if(typeof(route.remote) === 'function'){ method = msg.serverType + '.' + msg.service + '.' + msg.method; key = route.remote(sessionOrParam, method, msg.args); } else{ key = sessionOrParam; } } var server = null; if(!key){ server = _.sample(servers); } else{ server = dispatcher.hashDispatch(key, servers); } logger.debug('%s %j => %s => %j', sessionOrParam, msg, key, server.id); cb(null, server.id); }); }); // Connector router is exception this.app.route('connector', function(routeParam, msg, app, cb){ cb(null, routeParam.frontendId); }); }; module.exports = function(app, opts){ var routes = new Routes(app, opts); return routes; };