164 lines
4.8 KiB
TypeScript
164 lines
4.8 KiB
TypeScript
import 'reflect-metadata'
|
||
import * as mongoose from 'mongoose';
|
||
import { Application, IBoot } from 'egg';
|
||
import { connectRedis } from '../shared/pubUtils/redis';
|
||
import { loadGmDb, loadSubDb } from '../shared/db/index';
|
||
import { SDK_TA_CONST, THINKING_DATA_MODE, THINKING_DATA_MODE_LIST } from '../shared/consts';
|
||
const ThinkingAnalytics = require("thinkingdata-node");
|
||
|
||
export default class FooBoot implements IBoot {
|
||
private readonly app: Application;
|
||
|
||
constructor(app: Application) {
|
||
this.app = app;
|
||
}
|
||
|
||
async configWillLoad() {
|
||
// Ready to call configDidLoad,`
|
||
// Config, plugin files are referred,`
|
||
// this is the last chance to modify the config.
|
||
await this.connectDB(this.app);
|
||
await this.connectGMDB(this.app);
|
||
await this.connectSubDB(this.app);
|
||
await this.connectRedis(this.app);
|
||
|
||
this.app.config.realEnv = this.app.config.env;
|
||
console.log('****** config.env:', this.app.config.env);
|
||
if(this.app.config.env == 'local') {
|
||
this.app.config.realEnv = 'development';
|
||
}
|
||
console.log('****** config.realEnv:', this.app.config.realEnv);
|
||
// 如果gm使用的就是本机代理,host不转发到target而只把path替换
|
||
if(this.app.config.httpProxy && this.app.config.httpProxy[`/web/${this.app.config.realEnv}/`]) {
|
||
this.app.config.httpProxy[`/web/${this.app.config.realEnv}/`].changeOrigin = false;
|
||
}
|
||
this.connectThinkingData(this.app);
|
||
}
|
||
|
||
configDidLoad() {
|
||
// Config, plugin files have loaded.
|
||
}
|
||
|
||
async didLoad() {
|
||
// All files have loaded, start plugin here.
|
||
}
|
||
|
||
async willReady() {
|
||
// All plugins have started, can do some thing before app ready.
|
||
// await this.customLoadModel();
|
||
// const ctx = await this.app.createAnonymousContext();
|
||
// await ctx.service.game.readDbMaintenance();
|
||
}
|
||
|
||
async didReady() {
|
||
// Worker is ready, can do some things
|
||
// don't need to block the app boot.
|
||
}
|
||
|
||
async serverDidReady() {
|
||
// Server is listening.
|
||
}
|
||
|
||
async beforeClose() {
|
||
// Do some thing before app close.
|
||
}
|
||
|
||
//#region 手动挂载model,测试需要ctx.model
|
||
|
||
public async connectDB(app: Application) {
|
||
const { url, options } = app.config.mongoose
|
||
try {
|
||
if (url) {
|
||
// @ts-ignore
|
||
const connection = await mongoose.connect(url, options)
|
||
// @ts-ignore
|
||
console.log('******connectDB suc', url, options)
|
||
// @ts-ignore
|
||
app.context.connection = connection
|
||
}
|
||
} catch(e) {
|
||
// @ts-ignore
|
||
console.log(e)
|
||
}
|
||
}
|
||
|
||
public async connectGMDB(app: Application) {
|
||
const { url, options } = app.config.gmmongoose
|
||
try {
|
||
if (url) {
|
||
// @ts-ignore
|
||
const connection = await mongoose.createConnection(url, options)
|
||
// @ts-ignore
|
||
app.context.connectionGM = connection;
|
||
// @ts-ignore
|
||
loadGmDb(connection);
|
||
// @ts-ignore
|
||
console.log('******connectGMDB suc', url, options)
|
||
}
|
||
} catch(e) {
|
||
// @ts-ignore
|
||
console.log(e)
|
||
}
|
||
}
|
||
|
||
public async connectSubDB(app: Application) {
|
||
const { url, options } = app.config.submongoose||app.config.mongoose
|
||
try {
|
||
if (url) {
|
||
// @ts-ignore
|
||
const connection = await mongoose.createConnection(url, options)
|
||
// @ts-ignore
|
||
app.context.connectionGM = connection;
|
||
// @ts-ignore
|
||
loadSubDb(connection);
|
||
// @ts-ignore
|
||
console.log('******connectSubDB suc', url, options)
|
||
}
|
||
} catch(e) {
|
||
// @ts-ignore
|
||
console.log(e)
|
||
}
|
||
}
|
||
|
||
public async connectRedis(app: Application) {
|
||
const { url, pw } = app.config.redis
|
||
if (url) {
|
||
// @ts-ignore
|
||
const redisClient = connectRedis(url, pw);
|
||
// @ts-ignore
|
||
app.context.redisClient = redisClient;
|
||
}
|
||
}
|
||
|
||
public connectThinkingData(app: Application) {
|
||
let ta;
|
||
if(app.config.realEnv != 'development') {
|
||
// @ts-ignore
|
||
if(THINKING_DATA_MODE == THINKING_DATA_MODE_LIST.DEBUG) {
|
||
// @ts-ignore
|
||
ta = ThinkingAnalytics.initWithDebugMode(SDK_TA_CONST.APPID, SDK_TA_CONST.SERVER_URL);
|
||
} else if (THINKING_DATA_MODE == THINKING_DATA_MODE_LIST.BATCH) {
|
||
// @ts-ignore
|
||
ta = ThinkingAnalytics.initWithBatchMode(SDK_TA_CONST.APPID, SDK_TA_CONST.SERVER_URL);
|
||
} else if (THINKING_DATA_MODE == THINKING_DATA_MODE_LIST.LOGGING) {
|
||
// @ts-ignore
|
||
ta = ThinkingAnalytics.initWithLoggingMode(SDK_TA_CONST.LOG_PATH, {
|
||
pm2: true
|
||
});
|
||
}
|
||
// ta.setDynamicSuperProperties(() => {
|
||
// return {
|
||
// env: app.get('env'),
|
||
// server: app.getServerId(),
|
||
// mode: THINKING_DATA_MODE
|
||
// };
|
||
// });
|
||
// @ts-ignore
|
||
app.context.ta = ta;
|
||
}
|
||
}
|
||
|
||
//#endregion
|
||
}
|
||
|
||
module.exports = FooBoot; |