Files
ZYZ/gm-server/app.ts
luying 2d45b2c66c Revert " feat(db): web和gm添加只读库支持"
This reverts commit 5564a8363de57d8082b60a3a2255709aab957019.
2023-05-22 19:41:30 +08:00

113 lines
3.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'reflect-metadata'
import * as mongoose from 'mongoose';
import { Application, IBoot } from 'egg';
import { loadGmDb, loadSubDb } from '@db/index';
import { connectRedis } from '@pubUtils/redis';
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;
if(this.app.config.env == 'local') {
this.app.config.realEnv = 'development';
}
// 如果gm使用的就是本机代理host不转发到target而只把path替换
if(this.app.config.httpProxy && this.app.config.httpProxy[`/api/${this.app.config.realEnv}/`]) {
this.app.config.httpProxy[`/api/${this.app.config.realEnv}/`].changeOrigin = false;
}
}
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();
}
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) {
const connection = await mongoose.connect(url, options)
console.log('******connectDB suc', url, options)
app.context.connection = connection
}
} catch(e) {
console.log(e)
}
}
public async connectGMDB(app: Application) {
const { url, options } = app.config.gmmongoose
try {
if (url) {
const connection = await mongoose.createConnection(url, options)
app.context.connectionGM = connection;
loadGmDb(connection);
console.log('******connectGMDB suc', url, options)
}
} catch(e) {
console.log(e)
}
}
public async connectSubDB(app: Application) {
const { url, options } = app.config.submongoose||app.config.mongoose
try {
if (url) {
const connection = await mongoose.createConnection(url, options)
app.context.connectionGM = connection;
loadSubDb(connection);
console.log('******connectSubDB suc', url, options)
}
} catch(e) {
console.log(e)
}
}
public async connectRedis(app: Application) {
const { url, pw } = app.config.redis
if (url) {
const redisClient = connectRedis(url, pw);
app.context.redisClient = redisClient;
}
}
//#endregion
}
module.exports = FooBoot;