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/src/apiDoc/index.html
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
<link rel="icon" href="./favicon.ico">
|
|
6
6
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
7
7
|
<title>Vite App</title>
|
|
8
|
-
<script type="module" crossorigin src="./assets/index-
|
|
9
|
-
<link rel="stylesheet" href="./assets/index-
|
|
8
|
+
<script type="module" crossorigin src="./assets/index-7aebccaa.js"></script>
|
|
9
|
+
<link rel="stylesheet" href="./assets/index-a3168eed.css">
|
|
10
10
|
</head>
|
|
11
11
|
<body>
|
|
12
12
|
<div id="app"></div>
|
package/src/index.ts
CHANGED
|
@@ -5,11 +5,10 @@ import cors from "cors";
|
|
|
5
5
|
import path from "path";
|
|
6
6
|
import * as fs from "fs";
|
|
7
7
|
import { state } from "./state";
|
|
8
|
-
import
|
|
8
|
+
import printLog from "./utils/printLog";
|
|
9
9
|
import quickSend from "./utils/quickSend";
|
|
10
10
|
import readApi from "./utils/readApi";
|
|
11
11
|
import validRouteData from "./utils/validRouteData";
|
|
12
|
-
|
|
13
12
|
import validTokenUse from "./utils/validToken";
|
|
14
13
|
import type {
|
|
15
14
|
masReq,
|
|
@@ -40,6 +39,8 @@ import { createToken, validToken } from "./utils/meaToken";
|
|
|
40
39
|
import validType from "./utils/validType";
|
|
41
40
|
import createRouter from "./utils/createRouter";
|
|
42
41
|
import getRouterInfo from "./utils/getRouterInfo";
|
|
42
|
+
import getLogs from "./utils/logs";
|
|
43
|
+
import apiLimit from "./utils/apiLimit";
|
|
43
44
|
|
|
44
45
|
// =================================================================
|
|
45
46
|
export function getApp(
|
|
@@ -58,6 +59,10 @@ export function getApp(
|
|
|
58
59
|
state.apiCN = [];
|
|
59
60
|
// new express
|
|
60
61
|
const app = express();
|
|
62
|
+
// ip限制
|
|
63
|
+
if (config.apiLimit?.open) {
|
|
64
|
+
apiLimit(app, config.apiLimit || {});
|
|
65
|
+
}
|
|
61
66
|
// 是否允许跨域
|
|
62
67
|
config.cors && app.use(cors());
|
|
63
68
|
if (beforeMounted) {
|
|
@@ -75,11 +80,17 @@ export function getApp(
|
|
|
75
80
|
});
|
|
76
81
|
//快速return
|
|
77
82
|
app.use(quickSend);
|
|
78
|
-
//
|
|
79
|
-
if (config.logs.
|
|
83
|
+
// 打印日志
|
|
84
|
+
if (config.logs.debug) {
|
|
80
85
|
app.use(require("express-ip")().getIpInfoMiddleware);
|
|
81
|
-
app.use(
|
|
86
|
+
app.use(printLog);
|
|
82
87
|
}
|
|
88
|
+
// 存储日志
|
|
89
|
+
if (config.logs.open) {
|
|
90
|
+
// app.use(printLog);
|
|
91
|
+
getLogs(app, path.join(DIRNAME + `/logs/`));
|
|
92
|
+
}
|
|
93
|
+
|
|
83
94
|
// 权限管理
|
|
84
95
|
if (config.token.open) {
|
|
85
96
|
app.use(validTokenUse);
|
package/src/typings/index.ts
CHANGED
|
@@ -85,13 +85,21 @@ export type configType = {
|
|
|
85
85
|
};
|
|
86
86
|
/** 是否允许跨域 */
|
|
87
87
|
cors: boolean;
|
|
88
|
-
|
|
89
88
|
logs: {
|
|
90
89
|
/** 是否保存日志 */
|
|
91
90
|
open: boolean;
|
|
92
91
|
/** 是否打印访问日志 */
|
|
93
92
|
debug: boolean;
|
|
94
93
|
};
|
|
94
|
+
/** ip请求限制 */
|
|
95
|
+
apiLimit: {
|
|
96
|
+
/** 是否开启 */
|
|
97
|
+
open?: boolean;
|
|
98
|
+
/** 同个 ip windowMs ms之内 */
|
|
99
|
+
windowMs?: number;
|
|
100
|
+
/** 最多max个请求 */
|
|
101
|
+
max?: number; //
|
|
102
|
+
};
|
|
95
103
|
token: {
|
|
96
104
|
/** 是否使用token */
|
|
97
105
|
open: boolean;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import rateLimit from "express-rate-limit";
|
|
2
|
+
const apiLimit = (app: any, config?: { windowMs: number; max: number }) => {
|
|
3
|
+
// 创建一个速率限制器,限制每个IP地址在一分钟内最多可以发送10个请求
|
|
4
|
+
const limiter = rateLimit({
|
|
5
|
+
windowMs: config?.windowMs || 60 * 1000, // 1分钟
|
|
6
|
+
max: config?.max || 20, // 最多10个请求
|
|
7
|
+
// 当达到限制时调用的处理函数
|
|
8
|
+
handler: function (req, res) {
|
|
9
|
+
res.status(502).end();
|
|
10
|
+
},
|
|
11
|
+
});
|
|
12
|
+
// 将速率限制器应用于所有请求
|
|
13
|
+
app.use(limiter);
|
|
14
|
+
};
|
|
15
|
+
export default apiLimit;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import rateLimit from "express-rate-limit";
|
|
2
|
+
import morgan from "morgan";
|
|
3
|
+
import fs from "fs";
|
|
4
|
+
import * as rfs from "rotating-file-stream";
|
|
5
|
+
const getLogs = (app: any, dir: string) => {
|
|
6
|
+
// 创建一个日志目录(如果它不存在的话)
|
|
7
|
+
const logDirectory = dir;
|
|
8
|
+
fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory);
|
|
9
|
+
// 创建一个循环日志流
|
|
10
|
+
const accessLogStream = rfs.createStream("access.log", {
|
|
11
|
+
size: "50M", // 单个文件的最大大小
|
|
12
|
+
interval: "1d", // 每天滚动日志
|
|
13
|
+
path: logDirectory,
|
|
14
|
+
});
|
|
15
|
+
app.use(morgan("combined", { stream: accessLogStream }));
|
|
16
|
+
};
|
|
17
|
+
export default getLogs;
|
|
@@ -3,18 +3,10 @@ import { state } from "../state";
|
|
|
3
3
|
import moment from "moment";
|
|
4
4
|
|
|
5
5
|
export default (req, res, next) => {
|
|
6
|
-
const { config } = state;
|
|
7
6
|
const date = moment().format("YYYY-MM-DD HH:mm:ss");
|
|
8
7
|
const data = `${date} ${req.method} ${req.ipInfo.ip} ${req.url} ${
|
|
9
8
|
req.body ? JSON.stringify(req.body) : ""
|
|
10
9
|
}\n`;
|
|
11
|
-
|
|
12
|
-
state.DIRNAME + `/logs/${moment().format("YYYY-MM-DD")}.log`,
|
|
13
|
-
data
|
|
14
|
-
);
|
|
15
|
-
|
|
16
|
-
if (config.logs.debug) {
|
|
17
|
-
console.info(data);
|
|
18
|
-
}
|
|
10
|
+
console.info(data);
|
|
19
11
|
next();
|
|
20
12
|
};
|
package/test/config/config.ts
CHANGED
|
@@ -12,12 +12,18 @@ export default <configType>{
|
|
|
12
12
|
open: true,
|
|
13
13
|
debug: false,
|
|
14
14
|
},
|
|
15
|
+
apiLimit: {
|
|
16
|
+
open: true,
|
|
17
|
+
windowMs: 60 * 1000,
|
|
18
|
+
// windowMs: 60 * 1000,
|
|
19
|
+
max: 1000,
|
|
20
|
+
},
|
|
15
21
|
token: {
|
|
16
22
|
open: true,
|
|
17
23
|
pwd: "8087",
|
|
18
24
|
headerParams: "token",
|
|
19
25
|
},
|
|
20
|
-
useSql:
|
|
26
|
+
useSql: false,
|
|
21
27
|
debugTime: 100,
|
|
22
28
|
sql: {
|
|
23
29
|
host: "127.0.0.1",
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
约定式接口读取 完成
|
|
2
|
-
数据效验 完成
|
|
3
|
-
masmysql引入 (massql set方法开发) 完成
|
|
4
|
-
权限管理,token生成 完成
|
|
5
|
-
接口文档生成 完成
|
|
6
|
-
快捷创建接口 完成
|
|
7
|
-
生成sql类型 完成
|
|
8
|
-
错误处理 完成
|
|
9
|
-
cookie管理
|
|
10
|
-
路由导航汉化
|
|
11
|
-
接口文档体验优化
|
|
12
|
-
.vscode配置
|
|
13
|
-
一键导出前端axios代码
|
|
14
|
-
接口加载状态
|
|
15
|
-
导航点击跳转 调试->调试
|
|
16
|
-
特别说明:
|
|
17
|
-
get请求和请求头的number参数会被转为string,get请求无法发送和接收数组
|
|
18
|
-
接口改名配置丢失
|
|
19
|
-
只能接收{}或[]
|
|
20
|
-
请求参数必须是对象或数组
|