midway-fatcms 0.0.16 → 0.0.17

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.
Files changed (119) hide show
  1. package/dist/config/config.default.js +5 -3
  2. package/dist/configuration.d.ts +0 -3
  3. package/dist/configuration.js +0 -19
  4. package/dist/controller/gateway/ProxyApiGatewayController.d.ts +0 -2
  5. package/dist/controller/gateway/ProxyApiGatewayController.js +0 -18
  6. package/dist/controller/helpers.controller.d.ts +4 -5
  7. package/dist/controller/helpers.controller.js +56 -14
  8. package/dist/controller/manage/AccountRoleManageApi.js +3 -5
  9. package/dist/controller/manage/AnyApiMangeApi.js +4 -3
  10. package/dist/controller/manage/AppMangeApi.js +3 -4
  11. package/dist/controller/manage/AppPageMangeApi.js +4 -3
  12. package/dist/controller/manage/CrudMethodsMangeApi.js +3 -4
  13. package/dist/controller/manage/DataDictManageApi.js +6 -7
  14. package/dist/controller/manage/DeployManageApi.js +4 -5
  15. package/dist/controller/manage/FlowConfigManageApi.js +3 -5
  16. package/dist/controller/manage/MenuManageApi.js +3 -4
  17. package/dist/controller/manage/ProxyApiMangeApi.js +3 -4
  18. package/dist/controller/manage/SysConfigMangeApi.js +3 -3
  19. package/dist/controller/manage/SystemInfoManageApi.js +3 -1
  20. package/dist/controller/manage/WorkbenchMangeApi.js +3 -4
  21. package/dist/index.d.ts +3 -0
  22. package/dist/index.js +3 -0
  23. package/dist/libs/crud-pro/models/TransactionSqlServer.js +27 -4
  24. package/dist/libs/crud-pro/services/CrudProCachedCfgService.d.ts +5 -8
  25. package/dist/libs/crud-pro/services/CrudProCachedCfgService.js +25 -42
  26. package/dist/libs/crud-pro/utils/CrudMonitor.d.ts +0 -9
  27. package/dist/libs/crud-pro/utils/CrudMonitor.js +0 -21
  28. package/dist/libs/crud-pro/utils/pool/SqlServerUtils.js +26 -0
  29. package/dist/middleware/cacherefresh.middleware.d.ts +3 -46
  30. package/dist/middleware/cacherefresh.middleware.js +9 -67
  31. package/dist/models/CacheRefreshCategory.d.ts +1 -18
  32. package/dist/models/CacheRefreshCategory.js +0 -17
  33. package/dist/models/SystemEntities.d.ts +2 -0
  34. package/dist/models/bizmodels.d.ts +0 -8
  35. package/dist/models/bizmodels.js +0 -8
  36. package/dist/schedule/scheduleNames.d.ts +2 -0
  37. package/dist/schedule/scheduleNames.js +3 -1
  38. package/dist/service/SysAppService.d.ts +2 -5
  39. package/dist/service/SysAppService.js +15 -69
  40. package/dist/service/SysConfigService.d.ts +1 -6
  41. package/dist/service/SysConfigService.js +7 -46
  42. package/dist/service/SysDictDataService.d.ts +2 -5
  43. package/dist/service/SysDictDataService.js +13 -59
  44. package/dist/service/SysMenuService.d.ts +1 -4
  45. package/dist/service/SysMenuService.js +6 -42
  46. package/dist/service/WorkbenchService.d.ts +1 -5
  47. package/dist/service/WorkbenchService.js +10 -61
  48. package/dist/service/anyapi/AnyApiService.d.ts +1 -1
  49. package/dist/service/anyapi/AnyApiService.js +6 -16
  50. package/dist/service/base/bizmemcache/BizMemCacheService.d.ts +16 -0
  51. package/dist/service/base/bizmemcache/BizMemCacheService.js +239 -0
  52. package/dist/service/base/bizmemcache/BizMemCacheStore.d.ts +34 -0
  53. package/dist/service/base/bizmemcache/BizMemCacheStore.js +118 -0
  54. package/dist/service/base/bizmemcache/BizMemCacheTypes.d.ts +60 -0
  55. package/dist/service/base/bizmemcache/BizMemCacheTypes.js +18 -0
  56. package/dist/service/base/cache-refresh/CacheRefreshHub.d.ts +0 -22
  57. package/dist/service/base/cache-refresh/CacheRefreshHub.js +0 -22
  58. package/dist/service/base/cache-refresh/CacheRefreshHubScheduleService.d.ts +0 -6
  59. package/dist/service/base/cache-refresh/CacheRefreshHubScheduleService.js +0 -6
  60. package/dist/service/curd/CurdMixByDictService.d.ts +0 -4
  61. package/dist/service/curd/CurdMixByDictService.js +9 -55
  62. package/dist/service/curd/CurdMixBySysConfigService.d.ts +0 -5
  63. package/dist/service/curd/CurdMixBySysConfigService.js +9 -54
  64. package/dist/service/curd/CurdMixByWorkbenchService.d.ts +0 -1
  65. package/dist/service/curd/CurdMixByWorkbenchService.js +9 -36
  66. package/dist/service/curd/CurdProService.d.ts +0 -1
  67. package/dist/service/curd/CurdProService.js +0 -4
  68. package/dist/service/proxyapi/ProxyApiLoadService.d.ts +3 -8
  69. package/dist/service/proxyapi/ProxyApiLoadService.js +40 -68
  70. package/dist/service/proxyapi/ProxyApiService.d.ts +2 -0
  71. package/dist/service/proxyapi/ProxyApiService.js +32 -9
  72. package/dist/service/proxyapi/RouteTrie.d.ts +1 -7
  73. package/dist/service/proxyapi/RouteTrie.js +1 -17
  74. package/package.json +1 -1
  75. package/src/config/config.default.ts +4 -2
  76. package/src/configuration.ts +0 -23
  77. package/src/controller/gateway/ProxyApiGatewayController.ts +1 -9
  78. package/src/controller/helpers.controller.ts +66 -15
  79. package/src/controller/manage/AccountRoleManageApi.ts +3 -5
  80. package/src/controller/manage/AnyApiMangeApi.ts +4 -3
  81. package/src/controller/manage/AppMangeApi.ts +4 -5
  82. package/src/controller/manage/AppPageMangeApi.ts +4 -3
  83. package/src/controller/manage/CrudMethodsMangeApi.ts +4 -5
  84. package/src/controller/manage/DataDictManageApi.ts +7 -8
  85. package/src/controller/manage/DeployManageApi.ts +2 -3
  86. package/src/controller/manage/FlowConfigManageApi.ts +3 -5
  87. package/src/controller/manage/MenuManageApi.ts +4 -5
  88. package/src/controller/manage/ProxyApiMangeApi.ts +4 -5
  89. package/src/controller/manage/SysConfigMangeApi.ts +5 -5
  90. package/src/controller/manage/SystemInfoManageApi.ts +3 -1
  91. package/src/controller/manage/WorkbenchMangeApi.ts +4 -5
  92. package/src/index.ts +3 -0
  93. package/src/libs/crud-pro/models/TransactionSqlServer.ts +26 -4
  94. package/src/libs/crud-pro/services/CrudProCachedCfgService.ts +25 -43
  95. package/src/libs/crud-pro/utils/CrudMonitor.ts +0 -23
  96. package/src/libs/crud-pro/utils/pool/SqlServerUtils.ts +31 -0
  97. package/src/middleware/cacherefresh.middleware.ts +10 -71
  98. package/src/models/CacheRefreshCategory.ts +0 -17
  99. package/src/models/SystemEntities.ts +2 -0
  100. package/src/models/bizmodels.ts +0 -8
  101. package/src/schedule/scheduleNames.ts +5 -3
  102. package/src/service/SysAppService.ts +13 -66
  103. package/src/service/SysConfigService.ts +6 -39
  104. package/src/service/SysDictDataService.ts +11 -56
  105. package/src/service/SysMenuService.ts +4 -37
  106. package/src/service/WorkbenchService.ts +10 -68
  107. package/src/service/anyapi/AnyApiService.ts +5 -17
  108. package/src/service/base/bizmemcache/BizMemCacheService.ts +288 -0
  109. package/src/service/base/bizmemcache/BizMemCacheStore.ts +158 -0
  110. package/src/service/base/bizmemcache/BizMemCacheTypes.ts +76 -0
  111. package/src/service/base/cache-refresh/CacheRefreshHub.ts +0 -22
  112. package/src/service/base/cache-refresh/CacheRefreshHubScheduleService.ts +0 -6
  113. package/src/service/curd/CurdMixByDictService.ts +10 -61
  114. package/src/service/curd/CurdMixBySysConfigService.ts +9 -62
  115. package/src/service/curd/CurdMixByWorkbenchService.ts +7 -32
  116. package/src/service/curd/CurdProService.ts +0 -6
  117. package/src/service/proxyapi/ProxyApiLoadService.ts +39 -77
  118. package/src/service/proxyapi/ProxyApiService.ts +45 -9
  119. package/src/service/proxyapi/RouteTrie.ts +1 -18
@@ -1,20 +1,16 @@
1
1
  import { Inject, Provide } from '@midwayjs/core';
2
- import { Context } from '@midwayjs/koa';
3
2
  import * as _ from 'lodash';
4
3
  import { BaseService } from '../base/BaseService';
5
- import { setRouteTrie, RouteTrie } from './RouteTrie';
4
+ import { RouteTrie } from './RouteTrie';
6
5
  import { RouteHandler } from './RouteHandler';
7
- import { CurdProService } from '../curd/CurdProService';
8
- import { SystemTables } from '../../models/SystemTables';
9
- import { KeysOfSimpleSQL } from '../../libs/crud-pro/models/keys';
10
6
  import { IProxyApiEntity, IUpstreamInfo } from '../../models/SystemEntities';
11
7
  import { isEntityOK, parseJsonObject } from '../../libs/utils/functions';
12
8
  import { BALANCE_STRATEGY } from './ProxyApiUtils';
13
9
  import { WeightedRandom } from './WeightedRandom';
14
10
  import { WeightedRoundRobin } from './WeightedRoundRobin';
11
+ import { bizMemCacheStore } from '@/service/base/bizmemcache/BizMemCacheStore';
15
12
  import { WorkbenchService } from '../WorkbenchService';
16
13
  import { CommonException } from '../../libs/crud-pro/exceptions';
17
- import { GLOBAL_STATIC_CONFIG } from '@/libs/global-config/global-config';
18
14
 
19
15
  function toUpstreamInfo(config_content: any): IUpstreamInfo {
20
16
  const obj = parseJsonObject(config_content);
@@ -56,55 +52,46 @@ function toUpstreamInfo(config_content: any): IUpstreamInfo {
56
52
  */
57
53
  @Provide()
58
54
  export class ProxyApiLoadService extends BaseService {
59
- @Inject()
60
- protected ctx: Context;
61
-
62
- @Inject()
63
- private curdProService: CurdProService;
64
55
 
65
56
  @Inject()
66
57
  private workbenchService: WorkbenchService;
67
58
 
68
59
  /**
69
- * 创建路由树。刷新路由缓存
70
- * 先构建全新的 RouteTrie,完成后原子替换,避免重建期间路由不可用
71
- * @param workbench_code
60
+ * 获取站点路由树;routeTrieMap 为空或缺失时懒重建
72
61
  */
73
- public async buildRouteTrie(workbench_code: string): Promise<RouteTrie> {
74
- const upstreamMap: Record<string, IUpstreamInfo> = await this.buildUpstreamMap();
75
- const newRouteTrie = new RouteTrie();
76
- const entities = await this.loadProxyApiEntity(workbench_code, upstreamMap);
62
+ public getRouteTrie(workbench_code: string): RouteTrie {
63
+ const cacheObj = bizMemCacheStore.getSysProxyApi();
64
+ if (cacheObj?.routeTrieMap?.[workbench_code]) {
65
+ return cacheObj.routeTrieMap[workbench_code];
66
+ }
67
+ return this.buildRouteTrie(workbench_code);
68
+ }
69
+
70
+ private buildRouteTrie(workbench_code: string): RouteTrie {
71
+ const upstreamMap = this.buildUpstreamMap();
72
+ const routeTrie = new RouteTrie();
73
+ const entities = this.loadProxyApiEntity(workbench_code, upstreamMap);
77
74
  for (let i = 0; i < entities.length; i++) {
78
75
  const entity = entities[i];
79
76
  if (entity.status === 1) {
80
77
  const path_prefix2 = '/' + entity.upstream_code + entity.path_prefix;
81
- newRouteTrie.addRoute(path_prefix2, new RouteHandler(entity));
78
+ routeTrie.addRoute(path_prefix2, new RouteHandler(entity));
82
79
  }
83
80
  }
84
- setRouteTrie(workbench_code, newRouteTrie);
85
- return newRouteTrie;
81
+ const cacheObj = bizMemCacheStore.getSysProxyApi();
82
+ if (cacheObj) {
83
+ if (!cacheObj.routeTrieMap) {
84
+ cacheObj.routeTrieMap = {};
85
+ }
86
+ cacheObj.routeTrieMap[workbench_code] = routeTrie;
87
+ }
88
+ return routeTrie;
86
89
  }
87
90
 
88
- private async buildUpstreamMap() {
89
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
90
-
91
- // biz_tag={'upstream'}
92
- const res = await this.curdProService.executeCrudByCfg(
93
- {
94
- condition: {
95
- biz_tag: 'upstream',
96
- deleted_at: 0,
97
- },
98
- },
99
- {
100
- sqlTable: SystemTables.sys_configs,
101
- sqlDatabase: SystemDbName,
102
- sqlDbType: SystemDbType,
103
- sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
104
- }
105
- );
91
+ private buildUpstreamMap(): Record<string, IUpstreamInfo> {
92
+ const sysConfigList = bizMemCacheStore.getSysConfig()?.sysConfigList || [];
93
+ const rows = sysConfigList.filter((row) => row.biz_tag === 'upstream');
106
94
  const upstreamMap: Record<string, IUpstreamInfo> = {};
107
- const rows = res.getResRows() as any[];
108
95
  for (let i = 0; i < rows.length; i++) {
109
96
  const row = rows[i];
110
97
  const { config_code, config_content } = row || {};
@@ -113,37 +100,24 @@ export class ProxyApiLoadService extends BaseService {
113
100
  return upstreamMap;
114
101
  }
115
102
 
116
- private async loadProxyApiEntity(workbench_code: string, upstreamMap: Record<string, IUpstreamInfo>): Promise<IProxyApiEntity[]> {
117
- const { SystemDbName, SystemDbType } = GLOBAL_STATIC_CONFIG.getConfig();
103
+ private loadProxyApiEntity(workbench_code: string, upstreamMap: Record<string, IUpstreamInfo>): IProxyApiEntity[] {
104
+ const rows0 = bizMemCacheStore.getSysProxyApi()?.sysProxyApiList || [];
105
+ const rows: IProxyApiEntity[] = [];
118
106
 
119
- const res = await this.curdProService.executeCrudByCfg(
120
- {
121
- condition: {},
122
- },
123
- {
124
- sqlTable: SystemTables.sys_proxyapi,
125
- sqlDatabase: SystemDbName,
126
- sqlDbType: SystemDbType,
127
- sqlSimpleName: KeysOfSimpleSQL.SIMPLE_QUERY,
128
- }
129
- );
130
-
131
- const rows0 = res.getResRows() as IProxyApiEntity[];
132
-
133
- const rows = rows0.filter(s => {
107
+ for (let i = 0; i < rows0.length; i++) {
108
+ const s = rows0[i];
134
109
  if (!isEntityOK(s)) {
135
- return false;
110
+ continue;
136
111
  }
137
112
  if (!s.workbench_code_array) {
138
113
  throw new CommonException('IS_SUPPORT_CURRENT_WORKBENCH', '未配置支持的站点字段');
139
114
  }
140
- return this.workbenchService.isMatchWorkbenchCode(s.workbench_code_array, workbench_code);
141
- });
142
-
143
- for (let i = 0; i < rows.length; i++) {
144
- const row = rows[i];
145
- const upstream_code = row.upstream_code;
146
- this.addUpstreamInfo(row, upstream_code, upstreamMap);
115
+ if (!this.workbenchService.isMatchWorkbenchCode(s.workbench_code_array, workbench_code)) {
116
+ continue;
117
+ }
118
+ const row = JSON.parse(JSON.stringify(s)) as IProxyApiEntity;
119
+ this.addUpstreamInfo(row, row.upstream_code, upstreamMap);
120
+ rows.push(row);
147
121
  }
148
122
  return rows;
149
123
  }
@@ -159,16 +133,4 @@ export class ProxyApiLoadService extends BaseService {
159
133
  row.weightedRandom = new WeightedRandom(upstreamInfo.targetList || []);
160
134
  row.weightedRoundRobin = new WeightedRoundRobin(upstreamInfo.targetList || []);
161
135
  }
162
-
163
- public async refreshRouteTrie(): Promise<any> {
164
- const workbenchList = await this.workbenchService.getAllWorkbenchInfoList();
165
- for (let i = 0; i < workbenchList.length; i++) {
166
- const workbenchInfo = workbenchList[i];
167
- try {
168
- await this.buildRouteTrie(workbenchInfo.workbench_code);
169
- } catch (e) {
170
- this.logError('ProxyApiLoadService_runBySchedule', e);
171
- }
172
- }
173
- }
174
136
  }
@@ -1,7 +1,6 @@
1
1
  import { Inject, Provide } from '@midwayjs/core';
2
2
  import { Context } from '@midwayjs/koa';
3
3
  import * as _ from 'lodash';
4
- import { getRouteTrie } from './RouteTrie';
5
4
  import { RouteHandler } from './RouteHandler';
6
5
  import { Exceptions } from '@/libs/crud-pro/exceptions';
7
6
  import { BizException } from '@/models/devops';
@@ -15,6 +14,7 @@ import { WeightedRoundRobin } from './WeightedRoundRobin';
15
14
  import { ProxyApiLoadService } from './ProxyApiLoadService';
16
15
  import { API_BASE_TYPE, ApiBaseService } from '../base/ApiBaseService';
17
16
  import { toBase64 } from '@/libs/utils/base64';
17
+ import { parseJsonObject } from '@/libs/utils/functions';
18
18
 
19
19
  /**
20
20
  * 接口代理
@@ -36,7 +36,7 @@ export class ProxyApiService extends ApiBaseService {
36
36
  * @param upstream_path : 形如/[upstream_code]/[api_path]
37
37
  */
38
38
  public async handleProxyRequest(current_workbench_code: string, upstream_path: string) {
39
- const routeTrie = getRouteTrie(current_workbench_code);
39
+ const routeTrie = this.proxyApiLoadService.getRouteTrie(current_workbench_code);
40
40
  const ss = routeTrie.findLongestMatch(upstream_path);
41
41
  if (!ss) {
42
42
  throw new BizException('路径没有匹配到', Exceptions.CFG_NOT_FOUND);
@@ -78,19 +78,16 @@ export class ProxyApiService extends ApiBaseService {
78
78
  const response_type: ResponseType = 'stream';
79
79
 
80
80
  try {
81
- const headers = {
82
- ...ctx.headers,
83
- host: targetUrl.host, // 设置正确的 host
84
- };
81
+ const headers = this.buildRequestHeaders(targetUrl.host, proxyApiEntity);
82
+
83
+ const requestBody = this.buildRequestBody(ctx.request.body, proxyApiEntity);
85
84
 
86
- delete headers['content-length'];
87
85
 
88
- this.setRequestHeaders(headers, proxyApiEntity);
89
86
 
90
87
  const axiosParams = {
91
88
  method: ctx.method,
92
89
  url: targetUrl.toString(),
93
- data: ctx.request.body,
90
+ data: requestBody,
94
91
  headers: headers,
95
92
  responseType: response_type,
96
93
  timeout: Number(proxyApiEntity.timeout_ms) || 5 * 60 * 1000,
@@ -242,6 +239,21 @@ export class ProxyApiService extends ApiBaseService {
242
239
  return ss ? ss.origin : null;
243
240
  }
244
241
 
242
+ private buildRequestHeaders(targetHost: string, proxyApiEntity: IProxyApiEntity): any {
243
+ const ctx = this.ctx;
244
+ const headers = {
245
+ ...ctx.headers,
246
+ host: targetHost, // 设置正确的 host
247
+ };
248
+
249
+ delete headers['content-length'];
250
+
251
+ this.setRequestHeaders(headers, proxyApiEntity);
252
+
253
+ return headers;
254
+ }
255
+
256
+
245
257
  private setRequestHeaders(headers: any, proxyApiEntity: IProxyApiEntity) {
246
258
  const ctx = this.ctx;
247
259
 
@@ -269,6 +281,30 @@ export class ProxyApiService extends ApiBaseService {
269
281
  if (proxyApiEntity.change_origin && proxyApiEntity.change_origin.length > 0) {
270
282
  headers.origin = proxyApiEntity.change_origin;
271
283
  }
284
+
285
+ // 强制设置自定义Header
286
+ const custom_header = parseJsonObject(proxyApiEntity.custom_header);
287
+ if (custom_header && typeof custom_header === 'object' && Object.keys(custom_header).length > 0) {
288
+ Object.assign(headers, custom_header);
289
+ }
290
+
291
+ }
292
+
293
+
294
+
295
+ private buildRequestBody(requestBody: any, proxyApiEntity: IProxyApiEntity): any {
296
+
297
+ // 强制设置自定义body
298
+ const custom_body = parseJsonObject(proxyApiEntity.custom_body);
299
+ if (custom_body && typeof custom_body === 'object' && Object.keys(custom_body).length > 0) {
300
+ if (requestBody && typeof requestBody === 'object') {
301
+ Object.assign(requestBody, custom_body);
302
+ return requestBody;
303
+ }
304
+ return custom_body;
305
+ }
306
+
307
+ return requestBody;
272
308
  }
273
309
 
274
310
  private buildUserContextHeader(proxyApiEntity: IProxyApiEntity): string {
@@ -63,21 +63,4 @@ class RouteTrie {
63
63
  }
64
64
  }
65
65
 
66
- const ROUTE_TRIE_MANAGER: Record<string, RouteTrie> = {};
67
-
68
- function getRouteTrie(domainCode: string): RouteTrie {
69
- if (!ROUTE_TRIE_MANAGER[domainCode]) {
70
- ROUTE_TRIE_MANAGER[domainCode] = new RouteTrie();
71
- }
72
- return ROUTE_TRIE_MANAGER[domainCode];
73
- }
74
-
75
- /**
76
- * 原子替换指定 domainCode 的 RouteTrie
77
- * 用于重建路由时先构建新树再整体替换,避免清空后重建期间路由不可用
78
- */
79
- function setRouteTrie(domainCode: string, routeTrie: RouteTrie): void {
80
- ROUTE_TRIE_MANAGER[domainCode] = routeTrie;
81
- }
82
-
83
- export { RouteTrie, getRouteTrie, setRouteTrie };
66
+ export { RouteTrie };