feat(deploy): production deployment setup and fixes

- Debian-based api image (bookworm-slim), docker/debian mirrors, prisma
  binaryTargets for openssl 3.0
- nginx: admin SPA under /admin, TLS via acme.sh (ZeroSSL) + auto-renewal
  cron, http->https redirect
- prisma: add origin_goods.delisted migration, sync missing schema
  (good_image/tag_font_color/good_tags), fix users.createdAt Timestamptz
- api: CORS wildcard reflection, helmet CORP cross-origin, price
  backfill in persistProductDetail, categoryIcon ancestor fallback,
  mediaByColor per-color gallery in public goods detail
- admin: /admin base path (vite + router)
- import-data.mjs: udt_name casting, serial sequence advance fix
This commit is contained in:
yeuimu
2026-08-26 14:23:09 +08:00
parent be0b90e68f
commit 6c61a4e871
982 changed files with 74156 additions and 179393 deletions
@@ -1,13 +1,13 @@
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
/**
* Pulls the authenticated user out of the request, as populated by
* the JWT strategy.
*/
export const CurrentUser = createParamDecorator(
(data: keyof { id: bigint; username: string } | undefined, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest<{ user?: { id: bigint; username: string } }>();
const user = request.user;
return data ? user?.[data] : user;
},
);
import { createParamDecorator, ExecutionContext } from '@nestjs/common';
/**
* Pulls the authenticated user out of the request, as populated by
* the JWT strategy.
*/
export const CurrentUser = createParamDecorator(
(data: keyof { id: bigint; username: string } | undefined, ctx: ExecutionContext) => {
const request = ctx.switchToHttp().getRequest<{ user?: { id: bigint; username: string } }>();
const user = request.user;
return data ? user?.[data] : user;
},
);
@@ -1,92 +1,92 @@
import {
ArgumentsHost,
Catch,
ExceptionFilter,
HttpException,
HttpStatus,
Logger,
} from '@nestjs/common';
import { Request, Response } from 'express';
/**
* Global HTTP exception filter.
*
* Normalizes the error envelope to:
* ```json
* {
* "statusCode": 400,
* "message": "...",
* "error": "...",
* "timestamp": "ISO-8601",
* "path": "..."
* }
* ```
*/
@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
private readonly logger = new Logger(HttpExceptionFilter.name);
catch(exception: unknown, host: ArgumentsHost): void {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status =
exception instanceof HttpException ? exception.getStatus() : HttpStatus.INTERNAL_SERVER_ERROR;
// Malformed bigint/number inputs (e.g. `BigInt("abc")`) are client
// errors — map them to 400 instead of leaking a 500.
if (
status === HttpStatus.INTERNAL_SERVER_ERROR &&
exception instanceof Error &&
/Cannot convert .+ to (a BigInt|number)/i.test(exception.message)
) {
response.status(HttpStatus.BAD_REQUEST).json({
statusCode: HttpStatus.BAD_REQUEST,
message: 'Invalid numeric identifier',
error: 'BadRequestError',
timestamp: new Date().toISOString(),
path: request.url,
});
return;
}
let message: string | string[] = 'Internal server error';
let error = 'InternalServerError';
if (exception instanceof HttpException) {
const resp = exception.getResponse();
if (typeof resp === 'string') {
message = resp;
} else if (typeof resp === 'object' && resp !== null) {
const obj = resp as Record<string, unknown>;
message = (obj.message as string | string[]) ?? exception.message;
error = (obj.error as string) ?? exception.name;
} else {
message = exception.message;
}
} else if (exception instanceof Error) {
// Unexpected errors (Prisma, driver, ...) may contain SQL or
// connection details — never send them to the client.
this.logger.error(
`${request.method} ${request.url} -> ${status} ${exception.message}`,
exception.stack,
);
}
if (status >= 500 && exception instanceof HttpException) {
this.logger.error(
`${request.method} ${request.url} -> ${status} ${message}`,
exception instanceof Error ? exception.stack : undefined,
);
}
response.status(status).json({
statusCode: status,
message,
error,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}
import {
ArgumentsHost,
Catch,
ExceptionFilter,
HttpException,
HttpStatus,
Logger,
} from '@nestjs/common';
import { Request, Response } from 'express';
/**
* Global HTTP exception filter.
*
* Normalizes the error envelope to:
* ```json
* {
* "statusCode": 400,
* "message": "...",
* "error": "...",
* "timestamp": "ISO-8601",
* "path": "..."
* }
* ```
*/
@Catch()
export class HttpExceptionFilter implements ExceptionFilter {
private readonly logger = new Logger(HttpExceptionFilter.name);
catch(exception: unknown, host: ArgumentsHost): void {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status =
exception instanceof HttpException ? exception.getStatus() : HttpStatus.INTERNAL_SERVER_ERROR;
// Malformed bigint/number inputs (e.g. `BigInt("abc")`) are client
// errors — map them to 400 instead of leaking a 500.
if (
status === HttpStatus.INTERNAL_SERVER_ERROR &&
exception instanceof Error &&
/Cannot convert .+ to (a BigInt|number)/i.test(exception.message)
) {
response.status(HttpStatus.BAD_REQUEST).json({
statusCode: HttpStatus.BAD_REQUEST,
message: 'Invalid numeric identifier',
error: 'BadRequestError',
timestamp: new Date().toISOString(),
path: request.url,
});
return;
}
let message: string | string[] = 'Internal server error';
let error = 'InternalServerError';
if (exception instanceof HttpException) {
const resp = exception.getResponse();
if (typeof resp === 'string') {
message = resp;
} else if (typeof resp === 'object' && resp !== null) {
const obj = resp as Record<string, unknown>;
message = (obj.message as string | string[]) ?? exception.message;
error = (obj.error as string) ?? exception.name;
} else {
message = exception.message;
}
} else if (exception instanceof Error) {
// Unexpected errors (Prisma, driver, ...) may contain SQL or
// connection details — never send them to the client.
this.logger.error(
`${request.method} ${request.url} -> ${status} ${exception.message}`,
exception.stack,
);
}
if (status >= 500 && exception instanceof HttpException) {
this.logger.error(
`${request.method} ${request.url} -> ${status} ${message}`,
exception instanceof Error ? exception.stack : undefined,
);
}
response.status(status).json({
statusCode: status,
message,
error,
timestamp: new Date().toISOString(),
path: request.url,
});
}
}
@@ -1,19 +1,19 @@
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common';
import { Observable, map } from 'rxjs';
/**
* Wraps every successful response into `{ data, success: true }`.
*
* Exceptions still flow through the global filter and are not wrapped.
*/
@Injectable()
export class TransformInterceptor implements NestInterceptor {
intercept(_context: ExecutionContext, next: CallHandler): Observable<unknown> {
return next.handle().pipe(map((data) => ({ data, success: true })));
}
}
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from '@nestjs/common';
import { Observable, map } from 'rxjs';
/**
* Wraps every successful response into `{ data, success: true }`.
*
* Exceptions still flow through the global filter and are not wrapped.
*/
@Injectable()
export class TransformInterceptor implements NestInterceptor {
intercept(_context: ExecutionContext, next: CallHandler): Observable<unknown> {
return next.handle().pipe(map((data) => ({ data, success: true })));
}
}