/
starter.ts
75 lines (63 loc) 路 2 KB
/
starter.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
import { ServerlessStarterOptions } from './interface';
import {
IMidwayBootstrapOptions,
initializeGlobalApplicationContext,
MidwayFrameworkService,
} from '@midwayjs/core';
import { MidwayFaaSFramework } from './framework';
import { join } from 'path';
import { isTypeScriptEnvironment } from '@midwayjs/core';
export abstract class AbstractBootstrapStarter {
protected applicationContext;
protected framework: MidwayFaaSFramework;
constructor(protected options: ServerlessStarterOptions = {}) {}
private startedExports: Record<string, any>;
public getApplicationContext() {
return this.applicationContext;
}
public async close() {
await this.onClose();
}
protected getBaseDir() {
if (this.options.baseDir) {
return this.options.baseDir;
}
if (isTypeScriptEnvironment()) {
return join(this.options.appDir, 'src');
} else {
return join(this.options.appDir, 'dist');
}
}
public start(options?: ServerlessStarterOptions) {
if (!this.startedExports) {
this.options = Object.assign(this.options, options);
if (this.options.appDir && !this.options.baseDir) {
this.options.baseDir = this.getBaseDir();
}
this.startedExports = this.onStart();
}
if (this.startedExports) {
this.startedExports['getStarter'] = () => {
return this;
};
return this.startedExports;
}
}
public async initFramework(bootstrapOptions: IMidwayBootstrapOptions = {}) {
// init midway
this.applicationContext = await initializeGlobalApplicationContext(
bootstrapOptions
);
const midwayFrameworkService = this.applicationContext.get(
MidwayFrameworkService
);
this.framework = midwayFrameworkService.getMainFramework();
}
protected createDefaultMockHttpEvent() {}
protected createDefaultMockEvent() {}
protected createDefaultMockContext() {}
abstract onStart(): unknown;
abstract onInit(...args: unknown[]);
abstract onRequest(...args: unknown[]);
abstract onClose();
}