完善初始化逻辑

This commit is contained in:
cool 2024-03-20 23:03:08 +08:00
parent 651ffe8b76
commit 83f3faed5b
4 changed files with 59 additions and 10 deletions

View File

@ -4,7 +4,7 @@
"description": "一个项目用COOL就够了", "description": "一个项目用COOL就够了",
"private": true, "private": true,
"dependencies": { "dependencies": {
"@cool-midway/core": "^7.1.11", "@cool-midway/core": "^7.1.12",
"@cool-midway/rpc": "^7.0.0", "@cool-midway/rpc": "^7.0.0",
"@cool-midway/task": "^7.0.0", "@cool-midway/task": "^7.0.0",
"@midwayjs/bootstrap": "^3.15.0", "@midwayjs/bootstrap": "^3.15.0",
@ -60,7 +60,7 @@
"lint:fix": "mwts fix", "lint:fix": "mwts fix",
"ci": "npm run cov", "ci": "npm run cov",
"build": "mwtsc --cleanOutDir", "build": "mwtsc --cleanOutDir",
"pm2:start": "pm2 start ./bootstrap.js -i max --name cool-admin", "pm2:start": "pm2 start ./bootstrap.js -i 2 --name cool-admin",
"pm2:stop": "pm2 stop cool-admin & pm2 delete cool-admin" "pm2:stop": "pm2 stop cool-admin & pm2 delete cool-admin"
}, },
"midway-bin-clean": [ "midway-bin-clean": [

View File

@ -7,7 +7,7 @@ import { CoolCacheStore } from '@cool-midway/core';
export default { export default {
// use for cookie sign key, should change to your own and keep security // use for cookie sign key, should change to your own and keep security
keys: 'cool-admin for node', keys: 'cool-admin-keys-xxxxxx',
koa: { koa: {
port: 8001, port: 8001,
}, },

View File

@ -16,12 +16,30 @@ export class BaseAppEvent {
@Config('module') @Config('module')
config; config;
@Config('keys')
configKeys;
@Config('koa.port')
port;
@App() @App()
app: IMidwayKoaApplication; app: IMidwayKoaApplication;
@Event('onDBInit')
async onDBInit() {
this.checkConfig();
this.checkKeys();
}
@Event('onServerReady') @Event('onServerReady')
async onServerReady() { async onServerReady() {
this.checkConfig(); setTimeout(() => {
this.coreLogger.info(
`
使 cool-admin 官网: https://cool-js.com
服务访问地址: http://127.0.0.1:${this.port}`
);
}, 2000);
} }
/** /**
@ -30,7 +48,7 @@ export class BaseAppEvent {
async checkConfig() { async checkConfig() {
if (this.config.base.jwt.secret == 'cool-admin-xxxxxx') { if (this.config.base.jwt.secret == 'cool-admin-xxxxxx') {
this.coreLogger.warn( this.coreLogger.warn(
'检测到模块[base] jwt.secret 配置是默认值,即将自动修改...' '检测到模块[base] jwt.secret 配置是默认值,请不要关闭!即将自动修改...'
); );
setTimeout(() => { setTimeout(() => {
const filePath = path.join( const filePath = path.join(
@ -52,7 +70,38 @@ export class BaseAppEvent {
this.coreLogger.info( this.coreLogger.info(
'\x1B[36m [cool:module:base] midwayjs cool module base auto modify jwt.secret\x1B[0m' '\x1B[36m [cool:module:base] midwayjs cool module base auto modify jwt.secret\x1B[0m'
); );
}, 6000); }, 15000);
}
}
/**
* keys
*/
async checkKeys() {
if (this.configKeys == 'cool-admin-keys-xxxxxx') {
this.coreLogger.warn(
'检测到配置Keys是默认值请不要关闭即将自动修改...'
);
setTimeout(() => {
const filePath = path.join(
this.app.getBaseDir(),
'..',
'src',
'config',
'config.default.ts'
);
// 替换文件内容
let fileData = fs.readFileSync(filePath, 'utf8');
const secret = uuid().replace(/-/g, '');
this.config.base.jwt.secret = secret;
fs.writeFileSync(
filePath,
fileData.replace('cool-admin-keys-xxxxxx', secret)
);
this.coreLogger.info(
'\x1B[36m [cool:module:base] midwayjs cool keys auto modify \x1B[0m'
);
}, 15000);
} }
} }
} }

View File

@ -19,8 +19,8 @@ export class UserAppEvent {
@App() @App()
app: IMidwayKoaApplication; app: IMidwayKoaApplication;
@Event('onServerReady') @Event('onDBInit')
async onServerReady() { async onDBInit() {
this.checkConfig(); this.checkConfig();
} }
@ -30,7 +30,7 @@ export class UserAppEvent {
async checkConfig() { async checkConfig() {
if (this.config.user.jwt.secret == 'cool-app-xxxxxx') { if (this.config.user.jwt.secret == 'cool-app-xxxxxx') {
this.coreLogger.warn( this.coreLogger.warn(
'检测到模块[user] jwt.secret 配置是默认值,即将自动修改...' '检测到模块[user] jwt.secret 配置是默认值,请不要关闭!即将自动修改...'
); );
setTimeout(() => { setTimeout(() => {
const filePath = path.join( const filePath = path.join(
@ -49,7 +49,7 @@ export class UserAppEvent {
this.coreLogger.info( this.coreLogger.info(
'\x1B[36m [cool:module:user] midwayjs cool module user auto modify jwt.secret\x1B[0m' '\x1B[36m [cool:module:user] midwayjs cool module user auto modify jwt.secret\x1B[0m'
); );
}, 8000); }, 15000);
} }
} }
} }