/
singleProcess.ts
187 lines (159 loc) · 4.8 KB
/
singleProcess.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import {
HTTP_SERVER_KEY,
IMidwayBootstrapOptions,
IMidwayContainer,
IMidwayFramework,
MidwayFrameworkType,
PathFileUtil,
} from '@midwayjs/core';
import {
IMidwayWebConfigurationOptions,
IMidwayWebApplication,
} from '../interface';
import { resolve } from 'path';
import { Server } from 'net';
import { LoggerOptions } from '@midwayjs/logger';
import { MidwayKoaContextLogger } from '@midwayjs/koa';
export class MidwayWebSingleProcessFramework
implements
IMidwayFramework<IMidwayWebApplication, IMidwayWebConfigurationOptions>
{
public app: IMidwayWebApplication;
public agent;
public configurationOptions: IMidwayWebConfigurationOptions;
private isTsMode: boolean;
private server: Server;
public getApplication() {
return this.app;
}
public getFrameworkType(): MidwayFrameworkType {
return MidwayFrameworkType.WEB;
}
public async run(): Promise<void> {
// trigger server didReady
this.app.messenger.emit('egg-ready');
if (this.configurationOptions.port) {
new Promise<void>(resolve => {
const args: any[] = [this.configurationOptions.port];
if (this.configurationOptions.hostname) {
args.push(this.configurationOptions.hostname);
}
args.push(() => {
resolve();
});
this.server.listen(...args);
});
}
}
configure(
options: IMidwayWebConfigurationOptions = {}
): MidwayWebSingleProcessFramework {
this.configurationOptions = options;
return this;
}
getApplicationContext(): IMidwayContainer {
return this.app.getApplicationContext();
}
getConfiguration(key?: string): any {
return this.app.getConfig(key);
}
getCurrentEnvironment(): string {
return this.app.getEnv();
}
async initialize(options: Partial<IMidwayBootstrapOptions>) {
const opts = {
baseDir: options.appDir,
framework: resolve(__dirname, '../application'),
plugins: this.configurationOptions.plugins,
mode: 'single',
isTsMode: this.isTsMode || true,
applicationContext: options.applicationContext,
midwaySingleton: true,
};
const Agent = require(opts.framework).Agent;
const Application = require(opts.framework).Application;
const agent = (this.agent = new Agent(Object.assign({}, opts)));
await agent.ready();
const application = (this.app = new Application(Object.assign({}, opts)));
application.agent = agent;
agent.application = application;
// https config
if (this.configurationOptions.key && this.configurationOptions.cert) {
this.configurationOptions.key = PathFileUtil.getFileContentSync(
this.configurationOptions.key
);
this.configurationOptions.cert = PathFileUtil.getFileContentSync(
this.configurationOptions.cert
);
this.configurationOptions.ca = PathFileUtil.getFileContentSync(
this.configurationOptions.ca
);
if (this.configurationOptions.http2) {
this.server = require('http2').createSecureServer(
this.configurationOptions,
this.app.callback()
);
} else {
this.server = require('https').createServer(
this.configurationOptions,
this.app.callback()
);
}
} else {
if (this.configurationOptions.http2) {
this.server = require('http2').createServer(this.app.callback());
} else {
this.server = require('http').createServer(this.app.callback());
}
}
if (options.isMainFramework === undefined) {
await this.loadExtension();
}
}
async loadExtension() {
// 延迟加载 egg 的 load 方法
await (this.app.loader as any).loadOrigin();
await this.app.ready();
// emit egg-ready message in agent and application
this.app.messenger.broadcast('egg-ready', undefined);
// emit `server` event in app
this.app.emit('server', this.server);
// register httpServer to applicationContext
this.getApplicationContext().registerObject(HTTP_SERVER_KEY, this.server);
}
async stop(): Promise<void> {
await new Promise(resolve => {
this.server.close(resolve);
});
await this.app.close();
await this.agent.close();
}
getBaseDir(): string {
return this.app.getBaseDir();
}
getAppDir(): string {
return this.app.getAppDir();
}
public getLogger(name?: string) {
return this.app.getLogger(name);
}
getCoreLogger() {
return this.app.coreLogger;
}
getProjectName(): string {
return this.app.getProjectName();
}
createLogger(name: string, options?: LoggerOptions) {
return this.app.createLogger(name, options);
}
public getServer() {
return this.server;
}
public getFrameworkName() {
return 'midway:web';
}
public getDefaultContextLoggerClass() {
return MidwayKoaContextLogger;
}
public loadLifeCycles() {}
}