app.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 util = require('util');
  17. var path = require('path');
  18. var P = require('memdb-client').Promise;
  19. var EventEmitter = require('events').EventEmitter;
  20. var logger = require('memdb-client').logger.getLogger('test', __filename);
  21. /*
  22. * @param opts.serverId
  23. * @param opts.components - {'name' : opts}
  24. * @param opts.rpc
  25. */
  26. var App = function(opts){
  27. opts = opts || {};
  28. this.serverId = opts.serverId;
  29. this.serverType = opts.serverType;
  30. this.settings = {};
  31. this.components = {};
  32. this._routes = {};
  33. this.event = new EventEmitter();
  34. this._base = path.join(__dirname, '../..');
  35. };
  36. var proto = App.prototype;
  37. proto.start = function(cb){
  38. P.bind(this)
  39. .then(function(){
  40. return P.promisify(this.optComponents, this)('start');
  41. })
  42. .then(function(){
  43. return P.promisify(this.optComponents, this)('afterStart');
  44. })
  45. .nodeify(cb);
  46. };
  47. proto.stop = function(force, cb){
  48. P.bind(this)
  49. .then(function(){
  50. return P.promisify(this.optComponents, this)('beforeStop');
  51. })
  52. .then(function(){
  53. return P.promisify(this.stopComponents, this)(force);
  54. })
  55. .nodeify(cb);
  56. };
  57. proto.load = function(component, opts){
  58. var instance = component(this, opts);
  59. this.components[instance.name] = instance;
  60. };
  61. proto.optComponents = function(method, cb){
  62. P.bind(this)
  63. .then(function(){
  64. return Object.keys(this.components);
  65. })
  66. .map(function(name){
  67. var component = this.components[name];
  68. if(typeof(component[method]) === 'function'){
  69. return P.promisify(component[method], component)();
  70. }
  71. })
  72. .nodeify(cb);
  73. };
  74. proto.stopComponents = function(force, cb){
  75. if(typeof(force) === 'function'){
  76. cb = force;
  77. force = false;
  78. }
  79. P.bind(this)
  80. .then(function(){
  81. return Object.keys(this.components);
  82. })
  83. .map(function(name){
  84. var component = this.components[name];
  85. if(typeof(component.stop) === 'function'){
  86. return P.promisify(component.stop, component)(force);
  87. }
  88. })
  89. .nodeify(cb);
  90. };
  91. proto.getServerId = function(){
  92. return this.serverId;
  93. };
  94. proto.getServerType = function(){
  95. return this.serverType;
  96. };
  97. proto.get = function(name){
  98. return this.settings[name];
  99. };
  100. proto.set = function(name, value, attach){
  101. this.settings[name] = value;
  102. if(attach){
  103. this[name] = value;
  104. }
  105. };
  106. proto.getBase = function(){
  107. return this._base;
  108. };
  109. proto.setBase = function(basePath){
  110. this._base = basePath;
  111. };
  112. proto.route = function(serverType, fn){
  113. this._routes[serverType] = fn;
  114. };
  115. proto.rpcInvoke = function(serverId, opts, cb){
  116. logger.info('rpcInvoke %s %j', serverId, opts);
  117. cb(null);
  118. };
  119. module.exports = function(opts){
  120. return new App(opts);
  121. };