index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2015 rain1017.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. // implied. See the License for the specific language governing
  13. // permissions and limitations under the License. See the AUTHORS file
  14. // for names of contributors.
  15. 'use strict';
  16. var _ = require('lodash');
  17. var path = require('path');
  18. var logger = require('memdb-client').logger.getLogger('route', __filename);
  19. var util = require('util');
  20. var requireChildren = require('../../utils/require-children');
  21. var dispatcher = require('../../utils/dispatcher');
  22. var DEFAULT_BASE_PATH = 'app/routes';
  23. var Routes = function(app, opts){
  24. opts = app.get('routesConfig') || opts || {};
  25. this.app = app;
  26. this.basePath = opts.basePath || DEFAULT_BASE_PATH;
  27. };
  28. var proto = Routes.prototype;
  29. proto.name = 'routes';
  30. proto.start = function(cb){
  31. var basePath = path.join(this.app.getBase(), this.basePath);
  32. this.loadRoutes(basePath);
  33. cb();
  34. };
  35. proto.stop = function(force, cb){
  36. cb();
  37. };
  38. proto.loadRoutes = function(basePath){
  39. var modules = requireChildren(module, basePath);
  40. var self = this;
  41. Object.keys(modules).forEach(function(serverType){
  42. var route = modules[serverType];
  43. self.app.route(serverType, function(sessionOrParam, msg, app, cb){
  44. var servers = app.getServersByType(serverType);
  45. if(servers.length === 0){
  46. cb(new Error('No server for type ' + serverType));
  47. }
  48. var key = null, method = null;
  49. if(msg.namespace === 'sys' && msg.service === 'msgRemote' && msg.method === 'forwardMessage'){
  50. // handler message
  51. method = msg.args[0].route;
  52. var body = msg.args[0].body;
  53. if(typeof(route.handler) === 'function'){
  54. key = route.handler(sessionOrParam, method, body);
  55. }
  56. }
  57. else{
  58. // remote message
  59. if(typeof(route.remote) === 'function'){
  60. method = msg.serverType + '.' + msg.service + '.' + msg.method;
  61. key = route.remote(sessionOrParam, method, msg.args);
  62. }
  63. else{
  64. key = sessionOrParam;
  65. }
  66. }
  67. var server = null;
  68. if(!key){
  69. server = _.sample(servers);
  70. }
  71. else{
  72. server = dispatcher.hashDispatch(key, servers);
  73. }
  74. logger.debug('%s %j => %s => %j', sessionOrParam, msg, key, server.id);
  75. cb(null, server.id);
  76. });
  77. });
  78. // Connector router is exception
  79. this.app.route('connector', function(routeParam, msg, app, cb){
  80. cb(null, routeParam.frontendId);
  81. });
  82. };
  83. module.exports = function(app, opts){
  84. var routes = new Routes(app, opts);
  85. return routes;
  86. };