/
configuration.ts
59 lines (51 loc) 路 1.39 KB
/
configuration.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {
ILifeCycle,
IMidwayContainer,
MidwayApplicationManager,
MidwayConfigService,
Inject,
Configuration,
} from '@midwayjs/core';
import { SwaggerExplorer, SwaggerMiddleware } from '.';
import * as DefaultConfig from './config/config.default';
@Configuration({
importConfigs: [
{
default: DefaultConfig,
},
],
namespace: 'swagger',
})
export class SwaggerConfiguration implements ILifeCycle {
@Inject()
applicationManager: MidwayApplicationManager;
@Inject()
configService: MidwayConfigService;
async onReady(container: IMidwayContainer) {
const apps = this.applicationManager.getApplications([
'express',
'koa',
'egg',
'faas',
]);
if (apps.length) {
const explorer = await container.getAsync(SwaggerExplorer);
explorer.scanApp();
// 娣诲姞缁熶竴鍓嶇紑
let globalPrefix =
this.configService.getConfiguration('globalPrefix') ||
this.configService.getConfiguration('koa.globalPrefix') ||
this.configService.getConfiguration('express.globalPrefix') ||
this.configService.getConfiguration('egg.globalPrefix');
if (globalPrefix) {
if (!/^\//.test(globalPrefix)) {
globalPrefix = '/' + globalPrefix;
}
explorer.addGlobalPrefix(globalPrefix);
}
apps.forEach(app => {
app.useMiddleware(SwaggerMiddleware);
});
}
}
}