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:
1 parent
c2a2733172
commit
0fa0406770
4 files changed
+35
-9
No files matched your search
@@ -107,6 +107,8 @@ curl -s localhost:8090/api/projects | head -5
|
||||
- **ครั้งแรกที่รัน** ถ้าฐานข้อมูลว่าง `seedFromJson` จะย้ายข้อมูลจาก `data/projects.json` เข้าให้ (เก็บสำเนาไว้ที่ `data/projects.migrated.json`) · ถ้าฐานข้อมูลมีข้อมูลแล้วจะไม่ทับ — แก้ JSON มือเปล่าจึงไม่มีผลกับหน้าเว็บ
|
||||
- **Node 22 ยังติดป้าย experimental ให้ `node:sqlite`** จึงต้องมี `--disable-warning=ExperimentalWarning` (ใส่ไว้ใน `npm start` และ `CMD` ของ Dockerfile แล้ว) ไม่งั้น log จะมีคำเตือนทุกครั้ง
|
||||
- `data/hobbypj.sqlite` + `-wal` + `-shm` ไม่ขึ้น git — ย้ายเครื่องให้ commit `data/projects.json` แล้วปล่อยให้ seed ทำงาน หรือคัดลอกไฟล์ `.sqlite` ไปเอง
|
||||
- `GET /api/projects` คืน `writable` ตามกติกาเดียวกับ `PUT` (ไม่ใช่ “มาจาก loopback ก็เขียนได้”) และมี `token_required` บอกว่าตั้ง `ADMIN_TOKEN` ไว้ไหม — หน้าหลังบ้านใช้สองค่านี้เขียนข้อความสถานะ
|
||||
- `PUT` ตอบกลับ `database` + `backup` (path แบบ `/`) เพื่อตรวจย้อนหลังได้ว่าบันทึกลงไฟล์ไหนและสำรองไว้ที่ไหน
|
||||
- กดเผยแพร่ซ้ำโดยไม่แก้อะไรจะได้ commit ใหม่ (เพราะ `updated_at` เปลี่ยน) — ปกติ ไม่ใช่บั๊ก
|
||||
- ถ้าเปิดเว็บด้วย `python -m http.server` API จะ 404 → หน้าหลังบ้านจะขึ้นสถานะ “เปิดแบบไฟล์นิ่ง” และใช้ดาวน์โหลด JSON แทน
|
||||
- **Motion**: ทุกครั้งที่สร้าง DOM ใหม่ (กรองคำค้นหา, `mount()` ของ admin) ต้องเรียก `initReveal(...)` ใหม่ ไม่งั้น element ค้าง `opacity: 0`; hover ยกการ์ดต้องใช้ `translate` ไม่ใช่ `transform`; ถ้าวัดผลในแท็บเบื้องหลัง IO/rAF จะไม่ทำงาน ให้ `Page.bringToFront` ก่อนสรุปว่าโค้ดพัง (รายละเอียดใน `DESIGN.md` §9)
|
||||
+25
-7
@@ -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
|
||||
|
||||
+5
-1
@@ -202,7 +202,11 @@ export async function probePublishApi() {
|
||||
const res = await fetch(API_URL, { method: "GET", cache: "no-store" });
|
||||
if (!res.ok) return { available: false, status: res.status, message: `HTTP ${res.status}` };
|
||||
const body = await res.json();
|
||||
return { available: true, writable: Boolean(body?.writable) };
|
||||
return {
|
||||
available: true,
|
||||
writable: Boolean(body?.writable),
|
||||
tokenRequired: Boolean(body?.token_required),
|
||||
};
|
||||
} catch (error) {
|
||||
return { available: false, status: 0, message: "เชื่อมต่อเซิร์ฟเวอร์ไม่ได้" };
|
||||
}
|
||||
|
||||
+3
-1
@@ -513,7 +513,9 @@ export function adminView(ctx) {
|
||||
textEl.textContent = "พร้อมเผยแพร่ผ่านเซิร์ฟเวอร์";
|
||||
extraEl.textContent = apiState.writable
|
||||
? "บันทึกลงฐานข้อมูล SQLite ได้ทันที"
|
||||
: "อ่านได้ แต่เขียนไม่ได้ (ตรวจรหัสเผยแพร่)";
|
||||
: apiState.tokenRequired
|
||||
? "ต้องกรอกรหัสเผยแพร่ก่อนบันทึก"
|
||||
: "อ่านได้ แต่เขียนไม่ได้";
|
||||
hintEl.innerHTML =
|
||||
"กด “เผยแพร่” แล้วข้อมูลจะถูกบันทึกลงฐานข้อมูล SQLite (<code>data/hobbypj.sqlite</code>) พร้อม export <code>data/projects.json</code> และ commit ขึ้น Git ให้อัตโนมัติ";
|
||||
} else if (ctx.source === "cache") {
|
||||
|
||||
Reference in new issue
Block a user