feat: 后端 SQLite→PostgreSQL 迁移(双驱动)——codes/showcase/sync/chibi/payment/security 全链路支持 pg,修复 grant 保留字,schema 双 DDL 导出

This commit is contained in:
Sisyphus committed 2026-08-19 19:12:26 +08:00
1 parent 346f41d391
commit 605af4cdd3
17 files changed
+1516 -2202

No files matched your search

+7 -14
View File
@@ -1,6 +1,6 @@
import { Router } from "express";
import type { Request, Response } from "express";
import type Database from "better-sqlite3";
import type { Db } from "../db/index.js";
import { getDb } from "../db/index.js";
import { generateChibiImage, ChibiError, type GenerateRequest } from "./generate.js";
import { findTemplate, type ChibiCategory, type Composition } from "./prompts.js";
@@ -9,8 +9,8 @@ import { createRateLimiter, RATE_LIMITS } from "../security/rate-limit.js";
const CATEGORIES: readonly ChibiCategory[] = ["landscape", "person", "pet", "object", "food"];
const COMPOSITIONS: readonly Composition[] = ["主体特写", "半幅构图", "完整构图"];
/** Resolve the database instance: injected DB for tests, else global singleton. */
function resolveDb(injected?: Database.Database): Database.Database {
/** Resolve the database instance. */
function resolveDb(injected?: Db): Db {
return injected ?? getDb();
}
@@ -21,22 +21,18 @@ function resolveDb(injected?: Database.Database): Database.Database {
* Request: { code, image, referenceImage?, category, subtype, gridSize,
* colorCount, composition, detailTags, customPrompt? }
* Response: 200 { image, access, downloadGrant }
* 400 参数错误 (中文提示)
* 402 { error: "生成次数不足" }
* 500 服务器内部错误
* 502 AI 图像生成失败
* 400 / 402 / 500 / 502
*/
export function createChibiRouter(injectedDb?: Database.Database): Router {
export function createChibiRouter(injectedDb?: Db): Router {
const router = Router();
// 防刷限流:AI 生成 10 次/小时/IP(每路由实例独立计数,测试互不干扰)
// 防刷限流
const chibiLimiter = createRateLimiter(RATE_LIMITS.chibiGenerate)("chibi_generate");
router.post("/generate", chibiLimiter, async (req: Request, res: Response): Promise<void> => {
try {
const body = (req.body ?? {}) as Record<string, unknown>;
// ---- 基础字段校验 ----
const code = typeof body.code === "string" ? body.code : "";
if (!code) {
res.status(400).json({ error: "请输入兑换码" });
@@ -52,7 +48,6 @@ export function createChibiRouter(injectedDb?: Database.Database): Router {
? body.referenceImage
: undefined;
// ---- 分类 / 细分模板 ----
const category = body.category as ChibiCategory;
if (!CATEGORIES.includes(category)) {
res.status(400).json({ error: "未知的分类" });
@@ -66,7 +61,6 @@ export function createChibiRouter(injectedDb?: Database.Database): Router {
return;
}
// ---- 构图 / 网格 / 颜色 ----
const composition = body.composition as Composition;
if (!COMPOSITIONS.includes(composition)) {
res.status(400).json({ error: "未知的构图方式" });
@@ -83,7 +77,6 @@ export function createChibiRouter(injectedDb?: Database.Database): Router {
return;
}
// ---- 细节标签 / 自定义提示词 ----
const detailTags = Array.isArray(body.detailTags)
? body.detailTags.filter((t): t is string => typeof t === "string")
: [];
@@ -122,4 +115,4 @@ export function createChibiRouter(injectedDb?: Database.Database): Router {
});
return router;
}
}