http-svr.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*********************** 对koa.io进行了rpc封装,支持通讯里面用yield ******************/
  2. 'use strict';
  3. var co = require('co');
  4. var koa = require('koa.io');
  5. var rpcServer = require('./libs/rpcserver');
  6. var wordFilter = require('./libs/wordfilter');
  7. var jsonp = require('koa-safe-jsonp');
  8. var router = require('koa-router');
  9. var bodyParser = require('koa-better-body');
  10. var _ = require('underscore');
  11. _.str = require('underscore.string');
  12. _.v = require('validator');
  13. var gzip = require('./libs/mygzip');//压缩页面
  14. var session = require('koa-generic-session');
  15. var redisStore = require('koa-redis');
  16. var cors = require('koa-cors');
  17. var F = require('./common/function');
  18. var C = require('./config');
  19. var apiPre = C.apiPre;
  20. var modelMgr = require('./manager/model');
  21. var commonMgr = require('./manager/common');
  22. var app = new rpcServer();
  23. var httpapp = app.app;
  24. httpapp.use(bodyParser());
  25. jsonp(httpapp, {
  26. callback: '_callback', // default is 'callback'
  27. limit: 50 // max callback name string length, default is 512
  28. });
  29. httpapp.root_dir = __dirname;
  30. httpapp.proxy = true;
  31. httpapp.use(cors({credentials: true}));
  32. httpapp.use(gzip());
  33. httpapp.keys = ['keys', 'keykeys'];
  34. httpapp.use(session({
  35. // 默认采用 memory 方式
  36. store: redisStore({
  37. host: C.redis.host,
  38. db: C.redis.db,
  39. pass: C.redis.options.auth_pass
  40. }),
  41. key: C.session.key,
  42. prefix: C.session.prefix,
  43. rolling: true,//always reset the cookie and sessions, default false
  44. ttl: C.session.ttl * 1000,//此处单位为毫秒,所以得乘于1000
  45. cookie: {
  46. signed: false,
  47. httpOnly: false,
  48. //domain: C.cookie.domain,
  49. //maxage: C.cookie.maxage
  50. }
  51. }));
  52. httpapp.use(router(httpapp));
  53. ///////渲染模板
  54. var render = require('koa-ejs');
  55. render(httpapp, {
  56. root: './view',
  57. layout: 'template',
  58. viewExt: 'html',
  59. cache: false,
  60. debug: true
  61. });
  62. var port = C.http_port;
  63. app.port = port;
  64. app.listen(port, function () {
  65. console.log('Server listening at port %d', port);
  66. });
  67. app.model_mgr = new modelMgr();
  68. app.common_mgr = new commonMgr(app);
  69. co(app.common_mgr.mgr_map.im.clearLock());
  70. var http_common = require('./routes/httpCommon');
  71. http_common(app);
  72. // 处理服务器内部接口
  73. require('./routes/httpInnerApi')(app);
  74. // 登录相关http接口
  75. require('./routes/httpLogin')(app);
  76. // 成员相关http接口
  77. require('./routes/httpMemberMgr')(app);
  78. //
  79. require('./routes/httpMsgMgr')(app);
  80. //
  81. require('./routes/httpPublicRoomMgr')(app);
  82. // 房间坑位管理http接口
  83. require('./routes/httpQueueMgr')(app);
  84. // 房间管理http接口
  85. require('./routes/httpRoomMgr')(app);
  86. //app.setBeatTime(5000,15000);
  87. /**
  88. * @desc 监听客户端的连接和断开
  89. */