mas-server 2.0.53 → 2.0.55
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/dist/index.js +16 -4
- package/dist/utils/apiLimit.js +20 -0
- package/dist/utils/logs.js +44 -0
- package/dist/utils/printLog.js +12 -0
- package/package.json +52 -50
- package/src/apiDoc/assets/{empty-a6de4920.js → empty-b57cf97c.js} +1 -1
- package/src/apiDoc/assets/{index-f9ab4187.js → index-7aebccaa.js} +76 -76
- package/src/apiDoc/assets/{index-be53fe8f.css → index-a3168eed.css} +1 -1
- package/src/apiDoc/index.html +2 -2
- package/src/index.ts +16 -5
- package/src/typings/index.ts +9 -1
- package/src/utils/apiLimit.ts +15 -0
- package/src/utils/logs.ts +17 -0
- package/src/utils/{getLog.ts → printLog.ts} +1 -9
- package/test/config/config.ts +7 -1
- package/test//345/276/205/345/274/200/345/217/221.md +0 -20
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ const express_1 = __importDefault(require("express"));
|
|
|
9
9
|
const cors_1 = __importDefault(require("cors"));
|
|
10
10
|
const path_1 = __importDefault(require("path"));
|
|
11
11
|
const state_1 = require("./state");
|
|
12
|
-
const
|
|
12
|
+
const printLog_1 = __importDefault(require("./utils/printLog"));
|
|
13
13
|
const quickSend_1 = __importDefault(require("./utils/quickSend"));
|
|
14
14
|
const readApi_1 = __importDefault(require("./utils/readApi"));
|
|
15
15
|
exports.readApi = readApi_1.default;
|
|
@@ -39,8 +39,11 @@ const validType_1 = __importDefault(require("./utils/validType"));
|
|
|
39
39
|
exports.validType = validType_1.default;
|
|
40
40
|
const createRouter_1 = __importDefault(require("./utils/createRouter"));
|
|
41
41
|
const getRouterInfo_1 = __importDefault(require("./utils/getRouterInfo"));
|
|
42
|
+
const logs_1 = __importDefault(require("./utils/logs"));
|
|
43
|
+
const apiLimit_1 = __importDefault(require("./utils/apiLimit"));
|
|
42
44
|
// =================================================================
|
|
43
45
|
function getApp(DIRNAME, beforeMounted) {
|
|
46
|
+
var _a;
|
|
44
47
|
// 路径和配置放入state
|
|
45
48
|
state_1.state.DIRNAME = DIRNAME;
|
|
46
49
|
const config = require(path_1.default.join(DIRNAME, "/config/config.ts")).default;
|
|
@@ -50,6 +53,10 @@ function getApp(DIRNAME, beforeMounted) {
|
|
|
50
53
|
state_1.state.apiCN = [];
|
|
51
54
|
// new express
|
|
52
55
|
const app = (0, express_1.default)();
|
|
56
|
+
// ip限制
|
|
57
|
+
if ((_a = config.apiLimit) === null || _a === void 0 ? void 0 : _a.open) {
|
|
58
|
+
(0, apiLimit_1.default)(app, config.apiLimit || {});
|
|
59
|
+
}
|
|
53
60
|
// 是否允许跨域
|
|
54
61
|
config.cors && app.use((0, cors_1.default)());
|
|
55
62
|
if (beforeMounted) {
|
|
@@ -67,10 +74,15 @@ function getApp(DIRNAME, beforeMounted) {
|
|
|
67
74
|
});
|
|
68
75
|
//快速return
|
|
69
76
|
app.use(quickSend_1.default);
|
|
70
|
-
//
|
|
71
|
-
if (config.logs.
|
|
77
|
+
// 打印日志
|
|
78
|
+
if (config.logs.debug) {
|
|
72
79
|
app.use(require("express-ip")().getIpInfoMiddleware);
|
|
73
|
-
app.use(
|
|
80
|
+
app.use(printLog_1.default);
|
|
81
|
+
}
|
|
82
|
+
// 存储日志
|
|
83
|
+
if (config.logs.open) {
|
|
84
|
+
// app.use(printLog);
|
|
85
|
+
(0, logs_1.default)(app, path_1.default.join(DIRNAME + `/logs/`));
|
|
74
86
|
}
|
|
75
87
|
// 权限管理
|
|
76
88
|
if (config.token.open) {
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const express_rate_limit_1 = __importDefault(require("express-rate-limit"));
|
|
7
|
+
const apiLimit = (app, config) => {
|
|
8
|
+
// 创建一个速率限制器,限制每个IP地址在一分钟内最多可以发送10个请求
|
|
9
|
+
const limiter = (0, express_rate_limit_1.default)({
|
|
10
|
+
windowMs: (config === null || config === void 0 ? void 0 : config.windowMs) || 60 * 1000,
|
|
11
|
+
max: (config === null || config === void 0 ? void 0 : config.max) || 20,
|
|
12
|
+
// 当达到限制时调用的处理函数
|
|
13
|
+
handler: function (req, res) {
|
|
14
|
+
res.status(502).end();
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
// 将速率限制器应用于所有请求
|
|
18
|
+
app.use(limiter);
|
|
19
|
+
};
|
|
20
|
+
exports.default = apiLimit;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
const morgan_1 = __importDefault(require("morgan"));
|
|
30
|
+
const fs_1 = __importDefault(require("fs"));
|
|
31
|
+
const rfs = __importStar(require("rotating-file-stream"));
|
|
32
|
+
const getLogs = (app, dir) => {
|
|
33
|
+
// 创建一个日志目录(如果它不存在的话)
|
|
34
|
+
const logDirectory = dir;
|
|
35
|
+
fs_1.default.existsSync(logDirectory) || fs_1.default.mkdirSync(logDirectory);
|
|
36
|
+
// 创建一个循环日志流
|
|
37
|
+
const accessLogStream = rfs.createStream("access.log", {
|
|
38
|
+
size: "50M",
|
|
39
|
+
interval: "1d",
|
|
40
|
+
path: logDirectory,
|
|
41
|
+
});
|
|
42
|
+
app.use((0, morgan_1.default)("combined", { stream: accessLogStream }));
|
|
43
|
+
};
|
|
44
|
+
exports.default = getLogs;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const moment_1 = __importDefault(require("moment"));
|
|
7
|
+
exports.default = (req, res, next) => {
|
|
8
|
+
const date = (0, moment_1.default)().format("YYYY-MM-DD HH:mm:ss");
|
|
9
|
+
const data = `${date} ${req.method} ${req.ipInfo.ip} ${req.url} ${req.body ? JSON.stringify(req.body) : ""}\n`;
|
|
10
|
+
console.info(data);
|
|
11
|
+
next();
|
|
12
|
+
};
|
package/package.json
CHANGED
|
@@ -1,50 +1,52 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "mas-server",
|
|
3
|
-
"version": "2.0.
|
|
4
|
-
"description": "一款基于express面向中小型项目的后端框架",
|
|
5
|
-
"main": "dist/index.js",
|
|
6
|
-
"typings": "src/index.ts",
|
|
7
|
-
"scripts": {
|
|
8
|
-
"
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"
|
|
15
|
-
"
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
23
|
-
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"express": "^
|
|
29
|
-
"express-
|
|
30
|
-
"fs-extra": "^11.1.1",
|
|
31
|
-
"glob": "^10.3.1",
|
|
32
|
-
"masmysql": "^2.0.53",
|
|
33
|
-
"module-alias": "^2.2.2",
|
|
34
|
-
"moment": "^2.29.4",
|
|
35
|
-
"
|
|
36
|
-
"
|
|
37
|
-
"
|
|
38
|
-
"
|
|
39
|
-
"
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"@types/
|
|
49
|
-
|
|
50
|
-
|
|
1
|
+
{
|
|
2
|
+
"name": "mas-server",
|
|
3
|
+
"version": "2.0.55",
|
|
4
|
+
"description": "一款基于express面向中小型项目的后端框架",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"typings": "src/index.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "npx ts-node ./test/scripts/beforeCreated.ts -end && npx ts-node ./test/scripts/run.ts",
|
|
9
|
+
"testmon": "npx nodemon ./test/scripts/run.ts",
|
|
10
|
+
"testTS": "npx nodemon ./test/test.ts",
|
|
11
|
+
"createSql": "npx ts-node ./test/scripts/createSqlForm.ts",
|
|
12
|
+
"createApi": "npx ts-node ./test/scripts/createApiFile.ts",
|
|
13
|
+
"createApis": "npx ts-node ./test/scripts/createApis.ts",
|
|
14
|
+
"init": "npx nodemon ./test/scripts/beforeCreated.ts -end",
|
|
15
|
+
"gsft": "npx nodemon ./test/scripts/getSqlFormType.ts",
|
|
16
|
+
"build": "tsc --project ./tsconfig-build.json",
|
|
17
|
+
"sync": "npm run build && node ./beforebuild.js && npm publish && cnpm sync mas-server && explorer \"https://npmmirror.com/package/mas-server\""
|
|
18
|
+
},
|
|
19
|
+
"keywords": [],
|
|
20
|
+
"author": "tingxi8087",
|
|
21
|
+
"license": "ISC",
|
|
22
|
+
"dependencies": {
|
|
23
|
+
"@types/express": "^4.17.17",
|
|
24
|
+
"ansi-colors": "^4.1.3",
|
|
25
|
+
"axios": "^1.3.6",
|
|
26
|
+
"cors": "^2.8.5",
|
|
27
|
+
"express": "^4.18.2",
|
|
28
|
+
"express-ip": "^1.0.4",
|
|
29
|
+
"express-rate-limit": "^7.1.4",
|
|
30
|
+
"fs-extra": "^11.1.1",
|
|
31
|
+
"glob": "^10.3.1",
|
|
32
|
+
"masmysql": "^2.0.53",
|
|
33
|
+
"module-alias": "^2.2.2",
|
|
34
|
+
"moment": "^2.29.4",
|
|
35
|
+
"morgan": "^1.10.0",
|
|
36
|
+
"mysql": "^2.18.1",
|
|
37
|
+
"nodemon": "^2.0.22",
|
|
38
|
+
"rotating-file-stream": "^3.1.1",
|
|
39
|
+
"ts-node": "^10.9.1",
|
|
40
|
+
"ts-node-dev": "^2.0.0",
|
|
41
|
+
"typescript": "^5.0.4"
|
|
42
|
+
},
|
|
43
|
+
"_moduleAliases": {
|
|
44
|
+
"@": "src/",
|
|
45
|
+
"@@": "."
|
|
46
|
+
},
|
|
47
|
+
"devDependencies": {
|
|
48
|
+
"@types/cors": "^2.8.13",
|
|
49
|
+
"@types/express": "^4.17.17",
|
|
50
|
+
"@types/moment": "^2.13.0"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{_ as e}from"./index-
|
|
1
|
+
import{_ as e}from"./index-7aebccaa.js";const r={};function t(c,n){return null}const o=e(r,[["render",t]]);export{o as default};
|