@azteam/express 1.2.488 → 1.2.492
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/Server.js +112 -64
- package/package.json +3 -2
- package/src/Server.js +115 -62
package/lib/Server.js
CHANGED
|
@@ -13,14 +13,18 @@ var _express = _interopRequireDefault(require("express"));
|
|
|
13
13
|
require("express-async-errors");
|
|
14
14
|
var _fs = _interopRequireDefault(require("fs"));
|
|
15
15
|
var _helmet = _interopRequireDefault(require("helmet"));
|
|
16
|
-
var _http =
|
|
16
|
+
var _http = _interopRequireWildcard(require("http"));
|
|
17
17
|
var _lodash = _interopRequireDefault(require("lodash"));
|
|
18
18
|
var _methodOverride = _interopRequireDefault(require("method-override"));
|
|
19
19
|
var _morgan = _interopRequireDefault(require("morgan"));
|
|
20
|
+
var _pako = _interopRequireDefault(require("pako"));
|
|
20
21
|
var _path = _interopRequireDefault(require("path"));
|
|
21
22
|
var _psl = _interopRequireDefault(require("psl"));
|
|
23
|
+
var _socket = require("socket.io");
|
|
22
24
|
var _error = require("@azteam/error");
|
|
23
25
|
var _util = require("@azteam/util");
|
|
26
|
+
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
27
|
+
function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { "default": e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n["default"] = e, t && t.set(e, n), n; }
|
|
24
28
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
|
|
25
29
|
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
26
30
|
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
@@ -45,11 +49,13 @@ var RES_TYPE = {
|
|
|
45
49
|
OBJECT: 'OBJECT',
|
|
46
50
|
DOCS: 'DOCS'
|
|
47
51
|
};
|
|
52
|
+
function compressJson(message) {
|
|
53
|
+
return _pako["default"].deflate(JSON.stringify(message));
|
|
54
|
+
}
|
|
48
55
|
var Server = /*#__PURE__*/function () {
|
|
49
|
-
function Server(
|
|
50
|
-
var
|
|
51
|
-
var
|
|
52
|
-
var errorCallback = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : function () {};
|
|
56
|
+
function Server() {
|
|
57
|
+
var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
|
|
58
|
+
var errorCallback = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {};
|
|
53
59
|
_classCallCheck(this, Server);
|
|
54
60
|
this.options = _objectSpread(_objectSpread({
|
|
55
61
|
redis: null,
|
|
@@ -74,12 +80,55 @@ var Server = /*#__PURE__*/function () {
|
|
|
74
80
|
});
|
|
75
81
|
this.middlewares = [];
|
|
76
82
|
this.controllers = [];
|
|
77
|
-
|
|
78
|
-
return _this.initController(dir);
|
|
79
|
-
});
|
|
83
|
+
this.socketEvents = [];
|
|
80
84
|
this.callbackError = errorCallback;
|
|
81
85
|
}
|
|
82
86
|
_createClass(Server, [{
|
|
87
|
+
key: "createExpressApp",
|
|
88
|
+
value: function createExpressApp() {
|
|
89
|
+
var _this$options = this.options,
|
|
90
|
+
whiteList = _this$options.whiteList,
|
|
91
|
+
systemUser = _this$options.systemUser;
|
|
92
|
+
var app = (0, _express["default"])();
|
|
93
|
+
app.use((0, _helmet["default"])({
|
|
94
|
+
frameguard: false
|
|
95
|
+
}));
|
|
96
|
+
app.set('view engine', 'ejs');
|
|
97
|
+
app.use((0, _methodOverride["default"])());
|
|
98
|
+
app.use(_bodyParser["default"].urlencoded({
|
|
99
|
+
limit: '5mb',
|
|
100
|
+
extended: true
|
|
101
|
+
}));
|
|
102
|
+
app.use(_bodyParser["default"].json({
|
|
103
|
+
limit: '5mb'
|
|
104
|
+
}));
|
|
105
|
+
app.set('trust proxy', 1);
|
|
106
|
+
app.use((0, _cookieParser["default"])(process.env.SECRET_KEY));
|
|
107
|
+
app.use((0, _compression["default"])());
|
|
108
|
+
app.use((0, _cors["default"])(function (req, callback) {
|
|
109
|
+
var origin = req.get('Origin');
|
|
110
|
+
var authorization = req.get('Authorization');
|
|
111
|
+
var agent = req.get('User-Agent') || 'null';
|
|
112
|
+
var error = null;
|
|
113
|
+
if (!authorization && !agent.startsWith(systemUser)) {
|
|
114
|
+
if (origin && whiteList && !whiteList.some(function (re) {
|
|
115
|
+
return origin.endsWith(re);
|
|
116
|
+
})) {
|
|
117
|
+
console.error("CORS ".concat(agent, " ").concat(origin));
|
|
118
|
+
error = new _error.ErrorException(_error.CORS, "".concat(origin, " Not allowed by CORS"));
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
callback(error, {
|
|
122
|
+
credentials: true,
|
|
123
|
+
origin: true
|
|
124
|
+
});
|
|
125
|
+
}));
|
|
126
|
+
if (this.options.debug) {
|
|
127
|
+
app.use((0, _morgan["default"])('dev'));
|
|
128
|
+
}
|
|
129
|
+
return app;
|
|
130
|
+
}
|
|
131
|
+
}, {
|
|
83
132
|
key: "addController",
|
|
84
133
|
value: function addController(name, version, controller) {
|
|
85
134
|
this.controllers.push({
|
|
@@ -100,9 +149,9 @@ var Server = /*#__PURE__*/function () {
|
|
|
100
149
|
if (_fs["default"].statSync("".concat(apiDir, "/").concat(dirName)).isDirectory()) {
|
|
101
150
|
var versionDirs = _lodash["default"].differenceWith(_fs["default"].readdirSync("".concat(apiDir, "/").concat(dirName)), excludeList);
|
|
102
151
|
for (var j = 0; j < versionDirs.length; j += 1) {
|
|
103
|
-
var versionName = versionDirs[j]
|
|
104
|
-
|
|
105
|
-
|
|
152
|
+
var versionName = versionDirs[j];
|
|
153
|
+
// eslint-disable-next-line global-require,import/no-dynamic-require
|
|
154
|
+
var controller = require("".concat(apiDir, "/").concat(dirName, "/").concat(versionName, "/controller"))["default"];
|
|
106
155
|
this.addController(dirName, versionName, controller);
|
|
107
156
|
}
|
|
108
157
|
}
|
|
@@ -113,49 +162,14 @@ var Server = /*#__PURE__*/function () {
|
|
|
113
162
|
}, {
|
|
114
163
|
key: "startAPI",
|
|
115
164
|
value: function startAPI(port) {
|
|
116
|
-
var
|
|
165
|
+
var _this = this;
|
|
166
|
+
var serviceDirs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
167
|
+
serviceDirs.map(function (dir) {
|
|
168
|
+
return _this.initController(dir);
|
|
169
|
+
});
|
|
117
170
|
if (!_lodash["default"].isEmpty(this.controllers)) {
|
|
118
|
-
var
|
|
119
|
-
|
|
120
|
-
cookieOption = _this$options.cookieOption,
|
|
121
|
-
systemUser = _this$options.systemUser,
|
|
122
|
-
app = (0, _express["default"])();
|
|
123
|
-
app.use((0, _helmet["default"])({
|
|
124
|
-
frameguard: false
|
|
125
|
-
}));
|
|
126
|
-
app.set('view engine', 'ejs');
|
|
127
|
-
app.use((0, _methodOverride["default"])());
|
|
128
|
-
app.use(_bodyParser["default"].urlencoded({
|
|
129
|
-
limit: '5mb',
|
|
130
|
-
extended: true
|
|
131
|
-
}));
|
|
132
|
-
app.use(_bodyParser["default"].json({
|
|
133
|
-
limit: '5mb'
|
|
134
|
-
}));
|
|
135
|
-
app.set('trust proxy', 1);
|
|
136
|
-
app.use((0, _cookieParser["default"])(process.env.SECRET_KEY));
|
|
137
|
-
app.use((0, _compression["default"])());
|
|
138
|
-
app.use((0, _cors["default"])(function (req, callback) {
|
|
139
|
-
var origin = req.get('Origin'),
|
|
140
|
-
authorization = req.get('Authorization'),
|
|
141
|
-
agent = req.get('User-Agent') || 'null';
|
|
142
|
-
var error = null;
|
|
143
|
-
if (!authorization && !agent.startsWith(systemUser)) {
|
|
144
|
-
if (origin && whiteList && !whiteList.some(function (re) {
|
|
145
|
-
return origin.endsWith(re);
|
|
146
|
-
})) {
|
|
147
|
-
console.error("CORS ".concat(agent, " ").concat(origin));
|
|
148
|
-
error = new _error.ErrorException(_error.CORS, "".concat(origin, " Not allowed by CORS"));
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
callback(error, {
|
|
152
|
-
credentials: true,
|
|
153
|
-
origin: true
|
|
154
|
-
});
|
|
155
|
-
}));
|
|
156
|
-
if (this.options.debug) {
|
|
157
|
-
app.use((0, _morgan["default"])('dev'));
|
|
158
|
-
}
|
|
171
|
+
var cookieOption = this.options.cookieOption;
|
|
172
|
+
var app = this.createExpressApp();
|
|
159
173
|
app.get('/robots.txt', function (req, res) {
|
|
160
174
|
res.type('text/plain');
|
|
161
175
|
res.send('User-agent: *\nDisallow: /');
|
|
@@ -275,14 +289,14 @@ var Server = /*#__PURE__*/function () {
|
|
|
275
289
|
});
|
|
276
290
|
var msg = [];
|
|
277
291
|
_lodash["default"].map(this.controllers, function (data) {
|
|
278
|
-
var controller = data.controller
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
292
|
+
var controller = data.controller;
|
|
293
|
+
var controllerName = data.name;
|
|
294
|
+
var controllerVersion = data.version;
|
|
295
|
+
var listPublicRouter = controller.publicRouter();
|
|
282
296
|
_lodash["default"].map(listPublicRouter, function (method) {
|
|
283
297
|
var name = method.name,
|
|
284
|
-
type = method.type
|
|
285
|
-
|
|
298
|
+
type = method.type;
|
|
299
|
+
var router = controller[name]();
|
|
286
300
|
if (!router.disabled) {
|
|
287
301
|
router.path = "/".concat(method.path, "/").concat(router.path);
|
|
288
302
|
router.path = controller.pathName ? "/".concat(controller.pathName).concat(router.path) : router.path;
|
|
@@ -345,8 +359,8 @@ var Server = /*#__PURE__*/function () {
|
|
|
345
359
|
if (process.env.NODE_ENV === 'development') {
|
|
346
360
|
console.log(error.errors);
|
|
347
361
|
}
|
|
348
|
-
if (
|
|
349
|
-
|
|
362
|
+
if (_this.callbackError) {
|
|
363
|
+
_this.callbackError(error, req.originalUrl);
|
|
350
364
|
}
|
|
351
365
|
if (req.callbackError) {
|
|
352
366
|
req.callbackError(error);
|
|
@@ -389,13 +403,47 @@ var Server = /*#__PURE__*/function () {
|
|
|
389
403
|
}
|
|
390
404
|
throw new Error('No controllers in use API');
|
|
391
405
|
}
|
|
406
|
+
}, {
|
|
407
|
+
key: "initSocketEvent",
|
|
408
|
+
value: function initSocketEvent(socketDir) {
|
|
409
|
+
var excludeList = this.options.excludeList;
|
|
410
|
+
if (_fs["default"].existsSync(socketDir)) {
|
|
411
|
+
var controllerDirs = _fs["default"].readdirSync(socketDir);
|
|
412
|
+
for (var i = 0; i < controllerDirs.length; i += 1) {
|
|
413
|
+
var dirName = controllerDirs[i];
|
|
414
|
+
if (_fs["default"].statSync("".concat(socketDir, "/").concat(dirName)).isDirectory()) {
|
|
415
|
+
var versionDirs = _lodash["default"].differenceWith(_fs["default"].readdirSync("".concat(socketDir, "/").concat(dirName)), excludeList);
|
|
416
|
+
for (var j = 0; j < versionDirs.length; j += 1) {
|
|
417
|
+
// const versionName = versionDirs[j];
|
|
418
|
+
// // eslint-disable-next-line global-require,import/no-dynamic-require
|
|
419
|
+
// const controller = require(`${apiDir}/${dirName}/${versionName}/controller`).default;
|
|
420
|
+
// this.addController(dirName, versionName, controller);
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
return this;
|
|
426
|
+
}
|
|
392
427
|
}, {
|
|
393
428
|
key: "startSocket",
|
|
394
429
|
value: function startSocket(port) {
|
|
395
|
-
|
|
396
|
-
|
|
430
|
+
var _this4 = this;
|
|
431
|
+
var serviceDirs = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
|
|
432
|
+
serviceDirs.map(function (dir) {
|
|
433
|
+
return _this4.initSocketEvent(dir);
|
|
434
|
+
});
|
|
435
|
+
if (!_lodash["default"].isEmpty(this.socketEvents)) {
|
|
436
|
+
var httpServer = (0, _http.createServer)();
|
|
437
|
+
var app = this.createExpressApp();
|
|
438
|
+
httpServer.on('request', app);
|
|
439
|
+
var io = new _socket.Server(httpServer);
|
|
440
|
+
httpServer.listen(port);
|
|
441
|
+
httpServer.on('listening', function () {
|
|
442
|
+
console.log('listening', "ServerIO start at http://localhost:".concat(httpServer.address().port));
|
|
443
|
+
});
|
|
444
|
+
return io;
|
|
397
445
|
}
|
|
398
|
-
throw new Error('No
|
|
446
|
+
throw new Error('No events in use SOCKET');
|
|
399
447
|
}
|
|
400
448
|
}]);
|
|
401
449
|
return Server;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@azteam/express",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.492",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"author": "toda <sp.azsolution.net@gmail.com>",
|
|
6
6
|
"main": "./lib/index.js",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"pluralize": "8.0.0",
|
|
38
38
|
"psl": "1.8.0",
|
|
39
39
|
"socket.io": "4.4.1",
|
|
40
|
-
"ua-parser-js": "1.0.32"
|
|
40
|
+
"ua-parser-js": "1.0.32",
|
|
41
|
+
"pako": "2.0.4"
|
|
41
42
|
}
|
|
42
43
|
}
|
package/src/Server.js
CHANGED
|
@@ -7,12 +7,14 @@ import express from 'express';
|
|
|
7
7
|
import 'express-async-errors';
|
|
8
8
|
import fs from 'fs';
|
|
9
9
|
import helmet from 'helmet';
|
|
10
|
-
import http from 'http';
|
|
10
|
+
import http, {createServer} from 'http';
|
|
11
11
|
import _ from 'lodash';
|
|
12
12
|
import methodOverride from 'method-override';
|
|
13
13
|
import morgan from 'morgan';
|
|
14
|
+
import pako from 'pako';
|
|
14
15
|
import path from 'path';
|
|
15
16
|
import psl from 'psl';
|
|
17
|
+
import {Server as SocketIO} from 'socket.io';
|
|
16
18
|
|
|
17
19
|
import {CORS, ErrorException, NOT_FOUND, UNKNOWN, errorCatch} from '@azteam/error';
|
|
18
20
|
import {omitArrayItem, omitItem} from '@azteam/util';
|
|
@@ -23,8 +25,12 @@ const RES_TYPE = {
|
|
|
23
25
|
DOCS: 'DOCS',
|
|
24
26
|
};
|
|
25
27
|
|
|
28
|
+
function compressJson(message) {
|
|
29
|
+
return pako.deflate(JSON.stringify(message));
|
|
30
|
+
}
|
|
31
|
+
|
|
26
32
|
class Server {
|
|
27
|
-
constructor(
|
|
33
|
+
constructor(options = {}, errorCallback = () => {}) {
|
|
28
34
|
this.options = {
|
|
29
35
|
redis: null,
|
|
30
36
|
whiteList: null,
|
|
@@ -48,14 +54,58 @@ class Server {
|
|
|
48
54
|
|
|
49
55
|
this.middlewares = [];
|
|
50
56
|
this.controllers = [];
|
|
51
|
-
|
|
52
|
-
serviceDirs.map((dir) => {
|
|
53
|
-
return this.initController(dir);
|
|
54
|
-
});
|
|
57
|
+
this.socketEvents = [];
|
|
55
58
|
|
|
56
59
|
this.callbackError = errorCallback;
|
|
57
60
|
}
|
|
58
61
|
|
|
62
|
+
createExpressApp() {
|
|
63
|
+
const {whiteList, systemUser} = this.options;
|
|
64
|
+
|
|
65
|
+
const app = express();
|
|
66
|
+
|
|
67
|
+
app.use(
|
|
68
|
+
helmet({
|
|
69
|
+
frameguard: false,
|
|
70
|
+
})
|
|
71
|
+
);
|
|
72
|
+
|
|
73
|
+
app.set('view engine', 'ejs');
|
|
74
|
+
app.use(methodOverride());
|
|
75
|
+
app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
|
|
76
|
+
app.use(bodyParser.json({limit: '5mb'}));
|
|
77
|
+
|
|
78
|
+
app.set('trust proxy', 1);
|
|
79
|
+
|
|
80
|
+
app.use(cookieParser(process.env.SECRET_KEY));
|
|
81
|
+
app.use(compression());
|
|
82
|
+
app.use(
|
|
83
|
+
cors(function (req, callback) {
|
|
84
|
+
const origin = req.get('Origin');
|
|
85
|
+
const authorization = req.get('Authorization');
|
|
86
|
+
const agent = req.get('User-Agent') || 'null';
|
|
87
|
+
|
|
88
|
+
let error = null;
|
|
89
|
+
if (!authorization && !agent.startsWith(systemUser)) {
|
|
90
|
+
if (origin && whiteList && !whiteList.some((re) => origin.endsWith(re))) {
|
|
91
|
+
console.error(`CORS ${agent} ${origin}`);
|
|
92
|
+
error = new ErrorException(CORS, `${origin} Not allowed by CORS`);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
callback(error, {
|
|
96
|
+
credentials: true,
|
|
97
|
+
origin: true,
|
|
98
|
+
});
|
|
99
|
+
})
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
if (this.options.debug) {
|
|
103
|
+
app.use(morgan('dev'));
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
return app;
|
|
107
|
+
}
|
|
108
|
+
|
|
59
109
|
addController(name, version, controller) {
|
|
60
110
|
this.controllers.push({
|
|
61
111
|
name,
|
|
@@ -77,9 +127,9 @@ class Server {
|
|
|
77
127
|
if (fs.statSync(`${apiDir}/${dirName}`).isDirectory()) {
|
|
78
128
|
const versionDirs = _.differenceWith(fs.readdirSync(`${apiDir}/${dirName}`), excludeList);
|
|
79
129
|
for (let j = 0; j < versionDirs.length; j += 1) {
|
|
80
|
-
const versionName = versionDirs[j]
|
|
81
|
-
|
|
82
|
-
|
|
130
|
+
const versionName = versionDirs[j];
|
|
131
|
+
// eslint-disable-next-line global-require,import/no-dynamic-require
|
|
132
|
+
const controller = require(`${apiDir}/${dirName}/${versionName}/controller`).default;
|
|
83
133
|
this.addController(dirName, versionName, controller);
|
|
84
134
|
}
|
|
85
135
|
}
|
|
@@ -88,50 +138,14 @@ class Server {
|
|
|
88
138
|
return this;
|
|
89
139
|
}
|
|
90
140
|
|
|
91
|
-
startAPI(port) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
app.use(
|
|
97
|
-
helmet({
|
|
98
|
-
frameguard: false,
|
|
99
|
-
})
|
|
100
|
-
);
|
|
101
|
-
|
|
102
|
-
app.set('view engine', 'ejs');
|
|
103
|
-
|
|
104
|
-
app.use(methodOverride());
|
|
105
|
-
app.use(bodyParser.urlencoded({limit: '5mb', extended: true}));
|
|
106
|
-
app.use(bodyParser.json({limit: '5mb'}));
|
|
107
|
-
|
|
108
|
-
app.set('trust proxy', 1);
|
|
109
|
-
|
|
110
|
-
app.use(cookieParser(process.env.SECRET_KEY));
|
|
111
|
-
app.use(compression());
|
|
112
|
-
app.use(
|
|
113
|
-
cors(function (req, callback) {
|
|
114
|
-
const origin = req.get('Origin'),
|
|
115
|
-
authorization = req.get('Authorization'),
|
|
116
|
-
agent = req.get('User-Agent') || 'null';
|
|
117
|
-
|
|
118
|
-
let error = null;
|
|
119
|
-
if (!authorization && !agent.startsWith(systemUser)) {
|
|
120
|
-
if (origin && whiteList && !whiteList.some((re) => origin.endsWith(re))) {
|
|
121
|
-
console.error(`CORS ${agent} ${origin}`);
|
|
122
|
-
error = new ErrorException(CORS, `${origin} Not allowed by CORS`);
|
|
123
|
-
}
|
|
124
|
-
}
|
|
125
|
-
callback(error, {
|
|
126
|
-
credentials: true,
|
|
127
|
-
origin: true,
|
|
128
|
-
});
|
|
129
|
-
})
|
|
130
|
-
);
|
|
141
|
+
startAPI(port, serviceDirs = []) {
|
|
142
|
+
serviceDirs.map((dir) => {
|
|
143
|
+
return this.initController(dir);
|
|
144
|
+
});
|
|
131
145
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
146
|
+
if (!_.isEmpty(this.controllers)) {
|
|
147
|
+
const {cookieOption} = this.options;
|
|
148
|
+
const app = this.createExpressApp();
|
|
135
149
|
|
|
136
150
|
app.get('/robots.txt', function (req, res) {
|
|
137
151
|
res.type('text/plain');
|
|
@@ -268,14 +282,14 @@ class Server {
|
|
|
268
282
|
|
|
269
283
|
const msg = [];
|
|
270
284
|
_.map(this.controllers, (data) => {
|
|
271
|
-
const {controller} = data
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
285
|
+
const {controller} = data;
|
|
286
|
+
const controllerName = data.name;
|
|
287
|
+
const controllerVersion = data.version;
|
|
288
|
+
const listPublicRouter = controller.publicRouter();
|
|
275
289
|
|
|
276
290
|
_.map(listPublicRouter, (method) => {
|
|
277
|
-
const {name, type} = method
|
|
278
|
-
|
|
291
|
+
const {name, type} = method;
|
|
292
|
+
const router = controller[name]();
|
|
279
293
|
if (!router.disabled) {
|
|
280
294
|
router.path = `/${method.path}/${router.path}`;
|
|
281
295
|
router.path = controller.pathName ? `/${controller.pathName}${router.path}` : router.path;
|
|
@@ -371,11 +385,50 @@ class Server {
|
|
|
371
385
|
throw new Error('No controllers in use API');
|
|
372
386
|
}
|
|
373
387
|
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
388
|
+
initSocketEvent(socketDir) {
|
|
389
|
+
const {excludeList} = this.options;
|
|
390
|
+
|
|
391
|
+
if (fs.existsSync(socketDir)) {
|
|
392
|
+
const controllerDirs = fs.readdirSync(socketDir);
|
|
393
|
+
|
|
394
|
+
for (let i = 0; i < controllerDirs.length; i += 1) {
|
|
395
|
+
const dirName = controllerDirs[i];
|
|
396
|
+
|
|
397
|
+
if (fs.statSync(`${socketDir}/${dirName}`).isDirectory()) {
|
|
398
|
+
const versionDirs = _.differenceWith(fs.readdirSync(`${socketDir}/${dirName}`), excludeList);
|
|
399
|
+
for (let j = 0; j < versionDirs.length; j += 1) {
|
|
400
|
+
// const versionName = versionDirs[j];
|
|
401
|
+
// // eslint-disable-next-line global-require,import/no-dynamic-require
|
|
402
|
+
// const controller = require(`${apiDir}/${dirName}/${versionName}/controller`).default;
|
|
403
|
+
// this.addController(dirName, versionName, controller);
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
}
|
|
377
407
|
}
|
|
378
|
-
|
|
408
|
+
return this;
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
startSocket(port, serviceDirs = []) {
|
|
412
|
+
serviceDirs.map((dir) => {
|
|
413
|
+
return this.initSocketEvent(dir);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
if (!_.isEmpty(this.socketEvents)) {
|
|
417
|
+
const httpServer = createServer();
|
|
418
|
+
const app = this.createExpressApp();
|
|
419
|
+
httpServer.on('request', app);
|
|
420
|
+
|
|
421
|
+
const io = new SocketIO(httpServer);
|
|
422
|
+
httpServer.listen(port);
|
|
423
|
+
|
|
424
|
+
httpServer.on('listening', () => {
|
|
425
|
+
console.log('listening', `ServerIO start at http://localhost:${httpServer.address().port}`);
|
|
426
|
+
});
|
|
427
|
+
|
|
428
|
+
return io;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
throw new Error('No events in use SOCKET');
|
|
379
432
|
}
|
|
380
433
|
}
|
|
381
434
|
|