certd/packages/ui/certd-server/src/configuration.ts

105 lines
2.9 KiB
TypeScript
Raw Normal View History

2024-08-13 12:30:42 +00:00
import { App, Configuration } from '@midwayjs/core';
2023-01-29 05:44:19 +00:00
import * as koa from '@midwayjs/koa';
import { IMidwayKoaContext, NextFunction } from '@midwayjs/koa';
2023-05-25 02:33:42 +00:00
import * as orm from '@midwayjs/typeorm';
import * as cache from '@midwayjs/cache';
2024-07-14 16:30:33 +00:00
import * as validate from '@midwayjs/validate';
import * as info from '@midwayjs/info';
2023-05-25 04:38:29 +00:00
import * as staticFile from '@midwayjs/static-file';
import * as cron from './modules/cron/index.js';
2024-07-14 16:30:33 +00:00
import * as flyway from '@certd/midway-flyway-js';
import cors from '@koa/cors';
import { GlobalExceptionMiddleware } from './middleware/global-exception.js';
import { PreviewMiddleware } from './middleware/preview.js';
import { AuthorityMiddleware } from './middleware/authority.js';
2024-11-04 08:39:02 +00:00
import { logger } from '@certd/basic';
2024-07-14 16:30:33 +00:00
import { ResetPasswdMiddleware } from './middleware/reset-passwd/middleware.js';
import DefaultConfig from './config/config.default.js';
2024-10-03 14:03:49 +00:00
import * as libServer from '@certd/lib-server';
import * as commercial from '@certd/commercial-core';
2024-10-04 17:46:25 +00:00
import * as upload from '@midwayjs/upload';
2024-10-09 18:15:05 +00:00
import { setLogger } from '@certd/acme-client';
2024-07-18 03:17:13 +00:00
process.on('uncaughtException', error => {
console.error('未捕获的异常:', error);
// 在这里可以添加日志记录、发送错误通知等操作
});
2023-01-29 05:44:19 +00:00
@Configuration({
2024-07-14 16:30:33 +00:00
imports: [
koa,
orm,
cache,
flyway,
cron,
staticFile,
validate,
2024-10-04 17:46:25 +00:00
upload,
libServer,
commercial,
2024-07-14 16:30:33 +00:00
{
component: info,
enabledEnvironment: ['local'],
},
],
2023-01-29 05:44:19 +00:00
importConfigs: [
{
2024-07-14 16:30:33 +00:00
default: DefaultConfig,
2023-01-29 05:44:19 +00:00
},
],
})
2024-07-14 16:30:33 +00:00
export class MainConfiguration {
@App('koa')
2023-01-29 05:44:19 +00:00
app: koa.Application;
async onReady() {
2024-07-14 16:30:33 +00:00
// add middleware
2024-07-18 03:17:13 +00:00
// this.app.useMiddleware([ReportMiddleware]);
2024-07-14 16:30:33 +00:00
// add filter
// this.app.useFilter([NotFoundFilter, DefaultErrorFilter]);
2023-01-29 05:44:19 +00:00
//跨域
this.app.use(
cors({
origin: '*',
})
);
2024-10-10 16:05:51 +00:00
//
// this.app.use(async (ctx, next) => {
// // 只在返回 'index.html' 的时候设置 maxAge
// if (ctx.path === '/') {
// // ctx.response.redirect('/index.html');
// ctx.send(file)
// return;
// }
// });
2023-01-29 05:44:19 +00:00
// bodyparser options see https://github.com/koajs/bodyparser
//this.app.use(bodyParser());
//请求日志打印
this.app.useMiddleware([
//统一异常处理
GlobalExceptionMiddleware,
//预览模式限制修改id<1000的数据
PreviewMiddleware,
//授权处理
AuthorityMiddleware,
//resetPasswd,重置密码模式下不提供服务
ResetPasswdMiddleware,
2023-01-29 05:44:19 +00:00
]);
2023-06-29 01:31:26 +00:00
2024-10-10 16:05:51 +00:00
this.app.getMiddleware().insertFirst(async (ctx: IMidwayKoaContext, next: NextFunction) => {
await next();
if (ctx.path === '/' || ctx.path === '/index.html') {
2024-10-18 11:04:13 +00:00
ctx.response.set('Cache-Control', 'public,max-age=0');
2024-10-10 16:05:51 +00:00
}
});
2024-10-09 18:15:05 +00:00
//acme setlogger
setLogger((text: string) => {
logger.info(text);
});
2023-06-29 01:31:26 +00:00
logger.info('当前环境:', this.app.getEnv()); // prod
2023-01-29 05:44:19 +00:00
}
}