46 lines
1.8 KiB
TypeScript
46 lines
1.8 KiB
TypeScript
import './load-env';
|
|
import { NestFactory } from '@nestjs/core';
|
|
import { loadAppConfig } from '@dukang/shared-types';
|
|
import { NestExpressApplication } from '@nestjs/platform-express';
|
|
import { ValidationPipe } from '@nestjs/common';
|
|
import { json } from 'express';
|
|
import { AppModule } from './app.module';
|
|
import { HttpExceptionFilter } from './common/filters/http-exception.filter';
|
|
import { ResponseInterceptor } from './common/interceptors/response.interceptor';
|
|
import { preloadSystemConfigEnv } from './common/system-config/system-config.env';
|
|
|
|
async function bootstrap() {
|
|
const preloaded = await preloadSystemConfigEnv().catch((e) => {
|
|
console.warn('[config] system_config preload skipped:', e instanceof Error ? e.message : e);
|
|
return 0;
|
|
});
|
|
if (preloaded > 0) {
|
|
console.log(`[config] loaded ${preloaded} keys from system_config`);
|
|
}
|
|
|
|
const app = await NestFactory.create<NestExpressApplication>(AppModule, { bodyParser: false });
|
|
app.setGlobalPrefix('api/v1');
|
|
app.set('trust proxy', true);
|
|
app.enableCors({ origin: true, credentials: true });
|
|
app.use(
|
|
json({
|
|
verify: (req, _res, buf) => {
|
|
if (req.url?.includes('/callbacks/wechat/pay')) {
|
|
(req as { rawBody?: Buffer }).rawBody = buf;
|
|
}
|
|
},
|
|
}),
|
|
);
|
|
app.useGlobalPipes(new ValidationPipe({ transform: true, whitelist: true }));
|
|
app.useGlobalFilters(new HttpExceptionFilter());
|
|
app.useGlobalInterceptors(new ResponseInterceptor());
|
|
const port = process.env.PORT || 3000;
|
|
const cfg = loadAppConfig();
|
|
const smsMode = cfg.mockSms ? 'MOCK' : 'ALIYUN';
|
|
console.log(`[config] NODE_ENV=${process.env.NODE_ENV} MOCK_SMS=${cfg.mockSms} SMS=${smsMode}`);
|
|
await app.listen(port);
|
|
console.log(`dukang-api listening on http://localhost:${port}/api/v1`);
|
|
}
|
|
|
|
bootstrap();
|