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

+8 -12
View File
@@ -1,6 +1,6 @@
import express from "express";
import type { Express, Request } from "express";
import type Database from "better-sqlite3";
import type { Db } from "./db/index.js";
import { initDatabase } from "./db/index.js";
import { createCodesRouter } from "./codes/routes.js";
import { createPaymentRouter } from "./payment/routes.js";
@@ -17,13 +17,10 @@ const PORT = parseInt(process.env["PORT"] ?? "8788", 10);
* Create the Express app. Accepts an optional injected database so tests can
* use an in-memory DB; production uses the singleton from initDatabase().
*/
export function createApp(db?: Database.Database): Express {
export function createApp(db?: Db): Express {
const app = express();
// ---- CORS middleware ----
// Manual middleware instead of the `cors` package: the only browser client
// is the Vite dev server on http://localhost:5173, so an explicit allowlist
// (no wildcard) is simpler and safer than a new dependency.
const ALLOWED_ORIGINS: readonly string[] = ["http://localhost:5173", "http://127.0.0.1:5173"];
app.use((req, res, next) => {
const origin = req.headers.origin;
@@ -42,9 +39,6 @@ export function createApp(db?: Database.Database): Express {
});
// ---- Middleware ----
// 通过 verify 捕获原始请求体:微信支付回调验签必须基于未经解析的原始
// body 字节(Wechatpay-Signature 是对原始报文的签名)。
// limit 放宽到 20mb:图案导出需要承载 512×512 的大网格 JSON(约 8MB)。
app.use(
express.json({
limit: "20mb",
@@ -80,10 +74,12 @@ export function createApp(db?: Database.Database): Express {
// Only auto-start when run directly (not when imported by tests)
const isMain = process.argv[1] !== undefined && process.argv[1]!.endsWith("server.ts");
if (isMain) {
initDatabase();
createApp().listen(PORT, () => {
console.log(`PERLER LAB server running on http://localhost:${PORT}`);
});
void (async (): Promise<void> => {
await initDatabase();
createApp().listen(PORT, () => {
console.log(`PERLER LAB server running on http://localhost:${PORT}`);
});
})();
}
export default createApp;