chore: 项目基线提交 - 拼豆图纸生成器 V1.0 核心代码
- 前端: React+Vite 首页/AI专区, 算法库(MARD色板/量化/背景去除), API对接 - 后端: Express+SQLite, 兑换码/订单/支付/chibi生成, 风控+云端同步 - CI/CD: GitHub Actions + Docker + nginx 配置 - 文档: PRD/DESIGN/LAUNCH/ROADMAP/E2E/竞品对照等 15 份 - 工程: 任务看板(taskboard) + 健康巡检(agent-health) + Git流程(gitflow)
This commit is contained in:
commit
02762400fb
174 files changed
+28059
No files matched your search
@@ -0,0 +1,71 @@
|
||||
import express from "express";
|
||||
import type { Express } from "express";
|
||||
import type Database from "better-sqlite3";
|
||||
import { initDatabase } from "./db/index.js";
|
||||
import { createCodesRouter } from "./codes/routes.js";
|
||||
import { createPaymentRouter } from "./payment/routes.js";
|
||||
import { createChibiRouter } from "./chibi/routes.js";
|
||||
import { createSyncRouter } from "./sync/routes.js";
|
||||
|
||||
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 {
|
||||
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;
|
||||
if (origin !== undefined && ALLOWED_ORIGINS.includes(origin)) {
|
||||
res.setHeader("Access-Control-Allow-Origin", origin);
|
||||
res.setHeader("Vary", "Origin");
|
||||
res.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
|
||||
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
|
||||
res.setHeader("Access-Control-Max-Age", "86400");
|
||||
}
|
||||
if (req.method === "OPTIONS") {
|
||||
res.status(204).end();
|
||||
return;
|
||||
}
|
||||
next();
|
||||
});
|
||||
|
||||
// ---- Middleware ----
|
||||
app.use(express.json());
|
||||
|
||||
// ---- Health check ----
|
||||
app.get("/health", (_req, res) => {
|
||||
res.status(200).json({ status: "ok", timestamp: new Date().toISOString() });
|
||||
});
|
||||
|
||||
// ---- Mount routes ----
|
||||
app.use("/api/codes", createCodesRouter(db));
|
||||
app.use("/api/orders", createPaymentRouter(db));
|
||||
app.use("/api/chibi", createChibiRouter(db));
|
||||
app.use("/api/sync", createSyncRouter(db));
|
||||
|
||||
// ---- 404 handler ----
|
||||
app.use((_req, res) => {
|
||||
res.status(404).json({ error: "接口不存在" });
|
||||
});
|
||||
|
||||
return app;
|
||||
}
|
||||
|
||||
// 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}`);
|
||||
});
|
||||
}
|
||||
|
||||
export default createApp;
|
||||
Reference in new issue
Block a user