1+ class AppBootHook {
2+ private app ;
3+ constructor ( app ) {
4+ this . app = app ;
5+ }
6+
7+ configWillLoad ( ) {
8+ // 此时 config 文件已经被读取并合并,但是还并未生效
9+ // 这是应用层修改配置的最后时机
10+ // 注意:此函数只支持同步调用
11+
12+ // 例如:参数中的密码是加密的,在此处进行解密
13+ // this.app.config.mysql.password = decrypt(this.app.config.mysql.password);
14+ // 例如:插入一个中间件到框架的 coreMiddleware 之间
15+ // const statusIdx = this.app.config.coreMiddleware.indexOf('status');
16+ // this.app.config.coreMiddleware.splice(statusIdx + 1, 0, 'limit');
17+ }
18+
19+ async didLoad ( ) {
20+ // 所有的配置已经加载完毕
21+ // 可以用来加载应用自定义的文件,启动自定义的服务
22+
23+ // 例如:创建自定义应用的示例
24+ // this.app.queue = new Queue(this.app.config.queue);
25+ // await this.app.queue.init();
26+
27+ // 例如:加载自定义的目录
28+ // this.app.loader.loadToContext(path.join(__dirname, 'app/tasks'), 'tasks', {
29+ // fieldClass: 'tasksClasses',
30+ // });
31+ }
32+
33+ async willReady ( ) {
34+ // 所有的插件都已启动完毕,但是应用整体还未 ready
35+ // 可以做一些数据初始化等操作,这些操作成功才会启动应用
36+
37+ // 例如:从数据库加载数据到内存缓存
38+ // this.app.cacheData = await this.app.model.query(QUERY_CACHE_SQL);
39+ }
40+
41+ async didReady ( ) {
42+ // 应用已经启动完毕
43+
44+ // const ctx = await this.app.createAnonymousContext();
45+ // await ctx.service.Biz.request();
46+ }
47+
48+ async serverDidReady ( ) {
49+ console . log ( 'app start' ) ;
50+ // http / https server 已启动,开始接受外部请求
51+ // 此时可以从 app.server 拿到 server 的实例
52+
53+ this . app . server . on ( 'timeout' , socket => {
54+ // handle socket timeout
55+ console . log ( socket . connecting ) ;
56+ } ) ;
57+ }
58+ }
59+
60+ export default AppBootHook ;
0 commit comments