refactor: global register
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
const isTEST = !!process.env.TEST
|
||||
const isDev = process.env.NODE_ENV === 'development'
|
||||
import cluster from 'cluster'
|
||||
import { argv } from 'zx'
|
||||
import { isDev, isTest } from './global/env.global'
|
||||
|
||||
export const PORT = argv.port || process.env.PORT || 2333
|
||||
export const API_VERSION = 2
|
||||
@@ -28,7 +27,7 @@ export const MONGO_DB = {
|
||||
port: argv.db_port || 27017,
|
||||
get uri() {
|
||||
return `mongodb://${this.host}:${this.port}/${
|
||||
isTEST ? 'mx-space_unitest' : this.dbName
|
||||
isTest ? 'mx-space_unitest' : this.dbName
|
||||
}`
|
||||
},
|
||||
}
|
||||
@@ -55,7 +54,7 @@ export const SECURITY = {
|
||||
jwtSecret: argv.jwt_secret || argv.jwtSecret,
|
||||
jwtExpire: '7d',
|
||||
// 跳过登陆鉴权
|
||||
skipAuth: isTEST ? true : false,
|
||||
skipAuth: isTest ? true : false,
|
||||
}
|
||||
|
||||
export const CLUSTER = {
|
||||
|
||||
@@ -3,22 +3,21 @@ import { NestFactory } from '@nestjs/core'
|
||||
import { NestFastifyApplication } from '@nestjs/platform-fastify'
|
||||
import cluster from 'cluster'
|
||||
import { performance } from 'perf_hooks'
|
||||
import { API_VERSION, CLUSTER, CROSS_DOMAIN, PORT } from './app.config'
|
||||
import { API_VERSION, CROSS_DOMAIN, PORT } from './app.config'
|
||||
import { AppModule } from './app.module'
|
||||
import { Cluster } from './cluster'
|
||||
import { fastifyApp } from './common/adapters/fastify.adapter'
|
||||
import { RedisIoAdapter } from './common/adapters/socket.adapter'
|
||||
import { SpiderGuard } from './common/guard/spider.guard'
|
||||
import { LoggingInterceptor } from './common/interceptors/logging.interceptor'
|
||||
import { isTest } from './global/env.global'
|
||||
import { MyLogger } from './processors/logger/logger.service'
|
||||
import { isTest } from './utils'
|
||||
const Origin = Array.isArray(CROSS_DOMAIN.allowedOrigins)
|
||||
? CROSS_DOMAIN.allowedOrigins
|
||||
: false
|
||||
|
||||
declare const module: any
|
||||
|
||||
async function bootstrap() {
|
||||
export async function bootstrap() {
|
||||
const app = await NestFactory.create<NestFastifyApplication>(
|
||||
AppModule,
|
||||
fastifyApp,
|
||||
@@ -100,9 +99,3 @@ async function bootstrap() {
|
||||
module.hot.dispose(() => app.close())
|
||||
}
|
||||
}
|
||||
|
||||
if (CLUSTER.enable) {
|
||||
Cluster.register(parseInt(CLUSTER.workers) || os.cpus().length, bootstrap)
|
||||
} else {
|
||||
bootstrap()
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import { resolve } from 'path'
|
||||
import { HTTP_REQUEST_TIME } from '~/constants/meta.constant'
|
||||
import { LOG_DIR } from '~/constants/path.constant'
|
||||
import { REFLECTOR } from '~/constants/system.constant'
|
||||
import { isDev } from '~/utils'
|
||||
import { isDev } from '~/global/env.global'
|
||||
import { getIp } from '../../utils/ip.util'
|
||||
import { LoggingInterceptor } from '../interceptors/logging.interceptor'
|
||||
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { CanActivate, ExecutionContext, Injectable } from '@nestjs/common'
|
||||
import { AuthGuard as _AuthGuard } from '@nestjs/passport'
|
||||
import { isTest } from '~/global/env.global'
|
||||
import { mockUser1 } from '~/mock/user.mock'
|
||||
import { isTest } from '~/utils'
|
||||
import { getNestExecutionContextRequest } from '~/utils/nest.util'
|
||||
|
||||
/**
|
||||
|
||||
@@ -10,7 +10,7 @@ import {
|
||||
Injectable,
|
||||
} from '@nestjs/common'
|
||||
import { Observable } from 'rxjs'
|
||||
import { isDev } from '~/utils'
|
||||
import { isDev } from '~/global/env.global'
|
||||
import { getNestExecutionContextRequest } from '~/utils/nest.util'
|
||||
|
||||
@Injectable()
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import { homedir } from 'os'
|
||||
import { join } from 'path'
|
||||
import { isDev } from '~/utils'
|
||||
import { isDev } from '~/global/env.global'
|
||||
|
||||
export const HOME = homedir()
|
||||
|
||||
|
||||
83
src/global/consola.global.ts
Normal file
83
src/global/consola.global.ts
Normal file
@@ -0,0 +1,83 @@
|
||||
import { CronExpression } from '@nestjs/schedule'
|
||||
import consola_, {
|
||||
ConsolaReporterArgs,
|
||||
ConsolaReporterLogObject,
|
||||
FancyReporter,
|
||||
LogLevel,
|
||||
} from 'consola'
|
||||
import { CronJob } from 'cron'
|
||||
import { createWriteStream, WriteStream } from 'fs'
|
||||
import { resolve } from 'path'
|
||||
import { argv } from 'zx'
|
||||
import { LOG_DIR } from '~/constants/path.constant'
|
||||
import type { RedisSubPub } from '../utils/redis-subpub.util'
|
||||
import { getShortDate, getShortTime } from '../utils/time.util'
|
||||
import { isDev, isTest } from './env.global'
|
||||
|
||||
export const getTodayLogFilePath = () =>
|
||||
resolve(LOG_DIR, 'stdout_' + getShortDate(new Date()) + '.log')
|
||||
class DateTimeReporter extends FancyReporter {
|
||||
private fs: WriteStream
|
||||
private job: CronJob
|
||||
constructor() {
|
||||
super()
|
||||
|
||||
this.fs = createWriteStream(getTodayLogFilePath(), {
|
||||
encoding: 'utf-8',
|
||||
flags: 'a+',
|
||||
})
|
||||
|
||||
this.fs.write(
|
||||
'\n========================================================\n',
|
||||
)
|
||||
|
||||
this.job = new CronJob(CronExpression.EVERY_DAY_AT_MIDNIGHT, () => {
|
||||
this.fs.close()
|
||||
|
||||
this.fs = createWriteStream(getTodayLogFilePath(), {
|
||||
encoding: 'utf-8',
|
||||
flags: 'a+',
|
||||
})
|
||||
this.fs.write(
|
||||
'\n========================================================\n',
|
||||
)
|
||||
})
|
||||
}
|
||||
formatDate(date: Date) {
|
||||
return date.toLocaleString(undefined, {
|
||||
hour12: false,
|
||||
timeStyle: 'medium',
|
||||
dateStyle: 'short',
|
||||
})
|
||||
}
|
||||
|
||||
subpub: RedisSubPub
|
||||
public log(logObj: ConsolaReporterLogObject, args: ConsolaReporterArgs) {
|
||||
super.log(logObj, args)
|
||||
|
||||
if (!isTest) {
|
||||
;(async () => {
|
||||
this.subpub =
|
||||
this.subpub ||
|
||||
(await import('../utils/redis-subpub.util')).redisSubPub
|
||||
|
||||
const formatOutput =
|
||||
`${chalk.gray(getShortTime(new Date()))} ` +
|
||||
// @ts-expect-error
|
||||
super.formatLogObj(logObj, { width: args.columns || 0 }) +
|
||||
'\n'
|
||||
if (this.fs) {
|
||||
this.fs.write(formatOutput)
|
||||
}
|
||||
this.subpub.publish('log', formatOutput)
|
||||
})()
|
||||
}
|
||||
}
|
||||
}
|
||||
const consola = consola_.create({
|
||||
reporters: [new DateTimeReporter()],
|
||||
level: isDev || argv.verbose ? LogLevel.Trace : LogLevel.Info,
|
||||
})
|
||||
// HINT: must be called before any other log calls, export it in the end of your file
|
||||
consola.wrapAll()
|
||||
export { consola }
|
||||
3
src/global/env.global.ts
Normal file
3
src/global/env.global.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
export const isDev = process.env.NODE_ENV == 'development'
|
||||
|
||||
export const isTest = !!process.env.TEST
|
||||
@@ -1,6 +1,9 @@
|
||||
import { consola } from './consola.util'
|
||||
import './dayjs.util'
|
||||
import { isDev } from './tool.util'
|
||||
import 'zx/globals'
|
||||
import { consola } from './consola.global'
|
||||
import './dayjs.global'
|
||||
import { isDev } from './env.global'
|
||||
|
||||
$.verbose = isDev
|
||||
|
||||
console.debug = (...rest) => {
|
||||
if (isDev) {
|
||||
22
src/main.ts
22
src/main.ts
@@ -1,4 +1,18 @@
|
||||
import './utils/global.util'
|
||||
import './zx.global'
|
||||
// organize-imports-ignore
|
||||
import './bootstrap'
|
||||
// register global
|
||||
import './global/index.global'
|
||||
|
||||
async function main() {
|
||||
const [{ bootstrap }, { CLUSTER }, { Cluster }] = await Promise.all([
|
||||
import('./bootstrap'),
|
||||
import('./app.config'),
|
||||
import('./cluster'),
|
||||
])
|
||||
|
||||
if (CLUSTER.enable) {
|
||||
Cluster.register(parseInt(CLUSTER.workers) || os.cpus().length, bootstrap)
|
||||
} else {
|
||||
bootstrap()
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import { BadRequestException, Injectable, Logger } from '@nestjs/common'
|
||||
import { InjectModel } from 'nestjs-typegoose'
|
||||
import { isDev } from '~/global/env.global'
|
||||
import { AdminEventsGateway } from '~/processors/gateway/admin/events.gateway'
|
||||
import { EventTypes } from '~/processors/gateway/events.types'
|
||||
import {
|
||||
@@ -7,7 +8,6 @@ import {
|
||||
LinkApplyEmailType,
|
||||
} from '~/processors/helper/helper.email.service'
|
||||
import { HttpService } from '~/processors/helper/helper.http.service'
|
||||
import { isDev } from '~/utils'
|
||||
import { ConfigsService } from '../configs/configs.service'
|
||||
import { LinkModel, LinkState, LinkType } from './link.model'
|
||||
|
||||
|
||||
@@ -10,9 +10,9 @@ import { createWriteStream, WriteStream } from 'fs'
|
||||
import { resolve } from 'path'
|
||||
import { argv } from 'zx'
|
||||
import { LOG_DIR } from '~/constants/path.constant'
|
||||
import { isDev, isTest } from '../global/env.global'
|
||||
import type { RedisSubPub } from './redis-subpub.util'
|
||||
import { getShortDate, getShortTime } from './time.util'
|
||||
import { isDev, isTest } from './tool.util'
|
||||
|
||||
export const getTodayLogFilePath = () =>
|
||||
resolve(LOG_DIR, 'stdout_' + getShortDate(new Date()) + '.log')
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
export * from './crud.util'
|
||||
export * from './dayjs.util'
|
||||
export * from './ip.util'
|
||||
export * from './nest.util'
|
||||
export * from './query.util'
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { Logger } from '@nestjs/common'
|
||||
import IORedis from 'ioredis'
|
||||
import { REDIS } from '~/app.config'
|
||||
import { isTest } from './tool.util'
|
||||
import { isTest } from '../global/env.global'
|
||||
class RedisSubPub {
|
||||
public pubClient: IORedis.Redis
|
||||
public subClient: IORedis.Redis
|
||||
|
||||
@@ -1,9 +1,5 @@
|
||||
import { isObject } from 'lodash'
|
||||
|
||||
export const isDev = process.env.NODE_ENV == 'development'
|
||||
|
||||
export const isTest = !!process.env.TEST
|
||||
|
||||
export const md5 = (text: string) =>
|
||||
require('crypto').createHash('md5').update(text).digest('hex')
|
||||
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
import 'zx/globals'
|
||||
|
||||
$.verbose = isDev
|
||||
Reference in New Issue
Block a user