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

141 lines
4.1 KiB
TypeScript
Raw Normal View History

2026-05-31 01:41:33 +08:00
import { App, Configuration } from "@midwayjs/core";
import * as koa from "@midwayjs/koa";
import { IMidwayKoaContext, NextFunction } from "@midwayjs/koa";
import * as orm from "@midwayjs/typeorm";
import * as cache from "@midwayjs/cache";
import * as validate from "@midwayjs/validate";
import * as info from "@midwayjs/info";
import * as staticFile from "@midwayjs/static-file";
import * as cron from "./modules/cron/index.js";
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";
import { logger } from "@certd/basic";
import { ResetPasswdMiddleware } from "./middleware/reset-passwd/middleware.js";
import DefaultConfig from "./config/config.default.js";
import * as libServer from "@certd/lib-server";
import * as commercial from "@certd/commercial-core";
import * as upload from "@midwayjs/upload";
import { setLogger } from "@certd/acme-client";
import { HiddenMiddleware } from "./middleware/hidden.js";
import { shouldSetDefaultNoCache } from "./configuration-cache.js";
// import * as swagger from '@midwayjs/swagger';
2025-12-10 14:15:39 +08:00
//@ts-ignore
// process.env.UV_THREADPOOL_SIZE = 2
2026-05-31 01:41:33 +08:00
process.on("uncaughtException", error => {
console.error("未捕获的异常:", error);
2024-07-18 11:17:13 +08:00
// 在这里可以添加日志记录、发送错误通知等操作
2026-05-31 01:41:33 +08:00
if (error?.message?.includes("address family not supported")) {
2025-10-11 16:59:28 +08:00
logger.error("您的服务器不支持监听IPV6格式的地址(::),请配置环境变量: certd_koa_hostname=0.0.0.0");
}
2024-07-18 11:17:13 +08:00
});
2025-12-25 18:56:27 +08:00
// function startHeapLog() {
// function format(bytes: any) {
// return (bytes / 1024 / 1024).toFixed(2) + ' MB';
// }
// function log() {
// const mu = process.memoryUsage();
// logger.info(`rss:${format(mu.rss)},heapUsed: ${format(mu.heapUsed)},heapTotal: ${format(mu.heapTotal)},external: ${format(mu.external)}`);
// }
// setInterval(log, 200);
// log()
// }
// startHeapLog();
2023-01-29 13:44:19 +08:00
@Configuration({
2025-07-10 23:30:33 +08:00
detectorOptions: {
2026-05-31 01:41:33 +08:00
ignore: ["**/plugins/**"],
2025-07-10 23:30:33 +08:00
},
2024-07-15 00:30:33 +08:00
imports: [
koa,
orm,
cache,
flyway,
cron,
staticFile,
validate,
2024-10-05 01:46:25 +08:00
upload,
libServer,
commercial,
// {
// component: swagger,
// enabledEnvironment: ['local']
// },
2024-07-15 00:30:33 +08:00
{
component: info,
2026-05-31 01:41:33 +08:00
enabledEnvironment: ["local"],
2024-07-15 00:30:33 +08:00
},
],
2023-01-29 13:44:19 +08:00
importConfigs: [
{
2024-07-15 00:30:33 +08:00
default: DefaultConfig,
2023-01-29 13:44:19 +08:00
},
],
})
2024-07-15 00:30:33 +08:00
export class MainConfiguration {
2026-05-31 01:41:33 +08:00
@App("koa")
2023-01-29 13:44:19 +08:00
app: koa.Application;
async onReady() {
// 设置flyway logger
2025-12-25 18:56:27 +08:00
2024-07-15 00:30:33 +08:00
// add middleware
2024-07-18 11:17:13 +08:00
// this.app.useMiddleware([ReportMiddleware]);
2024-07-15 00:30:33 +08:00
// add filter
// this.app.useFilter([NotFoundFilter, DefaultErrorFilter]);
2023-01-29 13:44:19 +08:00
//跨域
this.app.use(
cors({
2026-05-31 01:41:33 +08:00
origin: "*",
2023-01-29 13:44:19 +08:00
})
);
2024-10-11 00:05:51 +08: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 13:44:19 +08:00
// bodyparser options see https://github.com/koajs/bodyparser
//this.app.use(bodyParser());
//请求日志打印
this.app.useMiddleware([
//统一异常处理
GlobalExceptionMiddleware,
//站点隐藏
HiddenMiddleware,
2023-01-29 13:44:19 +08:00
//预览模式限制修改id<1000的数据
PreviewMiddleware,
//授权处理
AuthorityMiddleware,
//resetPasswd,重置密码模式下不提供服务
ResetPasswdMiddleware,
2023-01-29 13:44:19 +08:00
]);
2023-06-29 09:31:26 +08:00
2024-10-11 00:05:51 +08:00
this.app.getMiddleware().insertFirst(async (ctx: IMidwayKoaContext, next: NextFunction) => {
await next();
2026-05-20 00:17:29 +08:00
const path = ctx.path;
// 如果是首页则强制设置为不缓存
2026-05-31 01:41:33 +08:00
if (shouldSetDefaultNoCache(path, ctx.response.get("Cache-Control"))) {
ctx.response.set("Cache-Control", "public,max-age=0");
2024-10-11 00:05:51 +08:00
}
});
2024-10-10 02:15:05 +08:00
//acme setlogger
setLogger((text: string) => {
logger.info(text);
});
2026-05-31 01:41:33 +08:00
logger.info("当前环境:", this.app.getEnv()); // prod
2023-01-29 13:44:19 +08:00
}
}