fix(api): report writable the same way PUT decides, return backup path

- GET /api/projects now returns writable: canWrite(req) + token_required, so the
  backoffice no longer claims it can save when ADMIN_TOKEN is set (PUT answered 403)
- PUT response exposes database + backup (forward-slash relative paths), drop the
  dead payload.backup assignment
- fail fast with a Thai hint when the database cannot be opened (permissions or
  corrupt file) instead of an unhandled stack trace
- AGENTS.md: document the two response fields
This commit is contained in:
Bkk1234 committed 2026-11-23 00:33:30 +07:00
1 parent c2a2733172
commit 0fa0406770
4 files changed
+35 -9

No files matched your search

+25 -7
View File
@@ -42,6 +42,8 @@ const HERE = path.dirname(fileURLToPath(import.meta.url));
const ROOT = path.resolve(HERE, "..");
const DATA_FILE = path.join(ROOT, "data", "projects.json");
const DB_FILE = resolveDbPath(ROOT);
/** path แบบ relative + / เสมอ (log สวยทั้ง Windows/Linux) */
const relPath = (target) => path.relative(ROOT, target).split(path.sep).join("/");
const PORT = Number(process.env.PORT || 8090);
const HOST = process.env.HOST || "0.0.0.0";
const ADMIN_TOKEN = (process.env.ADMIN_TOKEN || "").trim();
@@ -130,7 +132,7 @@ function validate(payload) {
}
async function gitPublish() {
const rel = path.relative(ROOT, DATA_FILE);
const rel = relPath(DATA_FILE);
const result = { committed: false, pushed: false, commit: null, message: "" };
try {
await execFileAsync("git", ["-C", ROOT, "add", rel]);
@@ -170,7 +172,14 @@ async function gitPublish() {
async function handleApi(req, res, pathname) {
if (pathname === "/api/projects" && req.method === "GET") {
try {
sendJson(res, 200, { ok: true, writable: canWrite(req) || isLoopback(req), data: readData(db) });
const data = readData(db);
sendJson(res, 200, {
ok: true,
// writable = เขียนได้ด้วย request นี้จริง ๆ (ตรงกับกติกาของ PUT)
writable: canWrite(req),
token_required: Boolean(ADMIN_TOKEN),
data,
});
} catch (error) {
sendJson(res, 500, { ok: false, error: `อ่านฐานข้อมูลไม่ได้: ${error.message}` });
}
@@ -205,12 +214,12 @@ async function handleApi(req, res, pathname) {
sendJson(res, 422, { ok: false, error: errors.join(" · "), errors });
return;
}
let backup = null;
try {
// สำรองสภาพก่อนแก้ (ถ้ามีข้อมูลอยู่) แล้วค่อยเขียนทับในทรานแซกชันเดียว
const backup = countProjects(db) > 0 ? await backupDatabase(db, DB_FILE).catch(() => null) : null;
backup = countProjects(db) > 0 ? await backupDatabase(db, DB_FILE).catch(() => null) : null;
payload.updated_at = saveData(db, payload);
await exportJson(DATA_FILE, { ...payload, updated_at: payload.updated_at });
payload.backup = backup ? path.relative(ROOT, backup) : null;
} catch (error) {
sendJson(res, 500, { ok: false, error: `เขียนฐานข้อมูลไม่ได้: ${error.message}` });
return;
@@ -220,7 +229,8 @@ async function handleApi(req, res, pathname) {
ok: true,
updated_at: payload.updated_at,
projects: payload.projects.length,
database: path.relative(ROOT, DB_FILE),
database: relPath(DB_FILE),
backup: backup ? relPath(backup) : null,
git,
});
return;
@@ -270,7 +280,15 @@ async function serveStatic(req, res, pathname) {
}
// เปิดฐานข้อมูล + ย้ายข้อมูลจาก data/projects.json ครั้งแรก (ถ้าตารางว่าง)
const db = await openDatabase(DB_FILE);
let db;
try {
db = await openDatabase(DB_FILE);
} catch (error) {
console.error(`[hobbypj] เปิดฐานข้อมูลไม่ได้: ${DB_FILE}`);
console.error(`[hobbypj] ${error.message}`);
console.error("[hobbypj] ตรวจสิทธิ์เขียนโฟลเดอร์ data/ · ตั้ง DB_PATH ให้ชี้ที่เขียนได้ · ถ้าไฟล์เสียให้กู้จาก data/backups/");
process.exit(1);
}
const seed = await seedFromJson(db, DATA_FILE);
const server = createServer((req, res) => {
@@ -295,7 +313,7 @@ server.listen(PORT, HOST, () => {
console.log(`[hobbypj] เซิร์ฟเวอร์ทำงานที่ http://localhost:${PORT}`);
console.log(`[hobbypj] เขียนข้อมูลได้: ${writable}`);
console.log(
`[hobbypj] ฐานข้อมูล: ${path.relative(ROOT, DB_FILE)} (SQLite) · สำเนา JSON: ${path.relative(ROOT, DATA_FILE)} · push: ${SHOULD_PUSH ? "เปิด" : "ปิด"}`,
`[hobbypj] ฐานข้อมูล: ${relPath(DB_FILE)} (SQLite) · สำเนา JSON: ${relPath(DATA_FILE)} · push: ${SHOULD_PUSH ? "เปิด" : "ปิด"}`,
);
console.log(
seed.seeded