feat: 完善安装部署与数字溯源码

This commit is contained in:
Nixevol
2026-07-06 23:26:21 +08:00
parent 7480e4d256
commit 7c6b0bff4c
59 changed files with 2174 additions and 166 deletions
+48 -25
View File
@@ -1,24 +1,35 @@
import 'reflect-metadata';
import { NestFactory } from '@nestjs/core';
import { ValidationPipe, RequestMethod, Logger } from '@nestjs/common';
import { Logger, RequestMethod, ValidationPipe } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express';
import { ConfigService } from '@nestjs/config';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { existsSync, mkdirSync } from 'fs';
import { resolve } from 'path';
import { AppModule } from './app.module';
import { join, resolve } from 'path';
import { InstallAppModule } from './install-app.module';
import { AllExceptionsFilter } from './common/filters/all-exceptions.filter';
import { TransformInterceptor } from './common/interceptors/transform.interceptor';
import { OperationLogInterceptor } from './operation-log/operation-log.interceptor';
import { STATIC_ASSET_CACHE_CONTROL } from './storage/storage.service';
import { isInstallMode } from './config/runtime-config';
// 让 BigInt 可被 JSON 序列化为字符串,避免精度问题。
(BigInt.prototype as unknown as { toJSON: () => string }).toJSON = function () {
return this.toString();
};
function mountSpa(app: NestExpressApplication, prefix: string, root: string): void {
if (!existsSync(root)) return;
app.useStaticAssets(root, { prefix });
const http = app.getHttpAdapter().getInstance();
const index = resolve(root, 'index.html');
http.get(prefix, (_req, res) => res.sendFile(index));
http.get(`${prefix}/*`, (_req, res) => res.sendFile(index));
}
async function bootstrap(): Promise<void> {
const app = await NestFactory.create<NestExpressApplication>(AppModule, {
const installing = isInstallMode();
const rootModule = installing ? InstallAppModule : (await import('./app.module')).AppModule;
const app = await NestFactory.create<NestExpressApplication>(rootModule, {
bodyParser: true,
});
const config = app.get(ConfigService);
@@ -46,28 +57,40 @@ async function bootstrap(): Promise<void> {
}),
);
app.useGlobalFilters(new AllExceptionsFilter());
app.useGlobalInterceptors(app.get(OperationLogInterceptor), new TransformInterceptor());
if (installing) {
app.useGlobalInterceptors(new TransformInterceptor());
} else {
app.useGlobalInterceptors(app.get(OperationLogInterceptor), new TransformInterceptor());
}
// 本地文件存储静态访问
const localDir = resolve(config.get<string>('storage.localDir') || './storage');
if (!existsSync(localDir)) {
mkdirSync(localDir, { recursive: true });
const publicRoot = resolve(process.env.ARTVERIS_PUBLIC_DIR || join(process.cwd(), 'public'));
const http = app.getHttpAdapter().getInstance();
if (installing) {
mountSpa(app, '/install', resolve(publicRoot, 'install'));
http.get('/', (_req, res) => res.redirect('/install'));
} else {
const localDir = resolve(config.get<string>('storage.localDir') || './storage');
if (!existsSync(localDir)) mkdirSync(localDir, { recursive: true });
app.useStaticAssets(localDir, {
prefix: config.get<string>('storage.publicPrefix') || '/uploads',
etag: true,
lastModified: true,
maxAge: '1y',
immutable: true,
setHeaders: (res) => {
res.setHeader('Cache-Control', STATIC_ASSET_CACHE_CONTROL);
},
});
mountSpa(app, '/admin', resolve(publicRoot, 'admin'));
mountSpa(app, '/trace', resolve(publicRoot, 'trace'));
if (existsSync(resolve(publicRoot, 'admin', 'index.html'))) {
http.get('/', (_req, res) => res.redirect('/admin'));
}
}
app.useStaticAssets(localDir, {
prefix: config.get<string>('storage.publicPrefix') || '/uploads',
etag: true,
lastModified: true,
maxAge: '1y',
immutable: true,
setHeaders: (res) => {
res.setHeader('Cache-Control', STATIC_ASSET_CACHE_CONTROL);
},
});
// Swagger(开发文档)
const swaggerConfig = new DocumentBuilder()
.setTitle('artveris API')
.setDescription('艺术品可信溯源与认证系统接口文档')
.setTitle('Artveris API')
.setDescription('Artveris traceability and certification API')
.setVersion('0.1.0')
.addBearerAuth()
.build();
@@ -76,8 +99,8 @@ async function bootstrap(): Promise<void> {
const port = config.get<number>('port') || 3000;
await app.listen(port);
logger.log(`API 已启动: http://localhost:${port}/api`);
logger.log(`接口文档: http://localhost:${port}/api/docs`);
logger.log(`${installing ? 'Install server' : 'API'} started: http://localhost:${port}/api`);
logger.log(`Swagger: http://localhost:${port}/api/docs`);
}
void bootstrap();