feat(home): link the homepage of every local project in the workspace
- server: GET /api/local-projects scans sibling folders for index.html (title from <title>/<h1>, generic titles fall back to the folder name, sorted by newest edit) and GET /projects/<folder>/ serves that folder - both endpoints are loopback-only; node_modules/.git/.hermes blocked and path traversal rejected; WORKSPACE_DIR=none disables the feature - home: new 'โปรเจคในเครื่อง' list (2 columns >=700px) that hides itself when the API is absent, so the live static site never shows fake rows - SW skips /projects/* so other projects' files are never cached; version 2026.11.19.5 - docs: README/AGENTS/DESIGN cover the endpoints, the loopback rule and the layout
This commit is contained in:
1 parent
b27acd40ee
commit
db7724b237
9 files changed
+417
-5
No files matched your search
@@ -69,6 +69,16 @@ PUBLISH_PUSH=0 node server/publish.mjs # เขียน+commit แต่ไ
|
||||
|
||||
**ข้อจำกัดที่ต้องรู้:** การทำ hash กันไม่ให้รหัสโผล่ใน source แต่ไม่ได้กันคนที่เปิด devtools (แก้ JS ให้ `isAuthed()` คืน true ได้เลย) และ verifier ที่ได้ก็ถูกเดารหัส offline ได้ถ้ารหัสอ่อน — การป้องกันจริงคือ `ADMIN_TOKEN` ของ `server/publish.mjs` ซึ่งต้องตั้งทุกครั้งที่เปิดเซิร์ฟเวอร์ให้เครื่องอื่นเข้าถึง
|
||||
|
||||
## โปรเจคในเครื่อง (`/projects/*`)
|
||||
|
||||
`server/publish.mjs` สแกนโฟลเดอร์ข้างเคียง (ค่าเริ่มต้น `D:\Git\workspace`) แล้วเปิดหน้า `index.html` ของแต่ละโปรเจคให้คลิกดูได้จากหน้าแรก
|
||||
|
||||
- `GET /api/local-projects` — รายการโฟลเดอร์ที่พบ (เรียงตามเวลาแก้ล่าสุด), `GET /projects/<โฟลเดอร์>/…` — เสิร์ฟไฟล์
|
||||
- **รับเฉพาะ loopback** ทั้งสอง endpoint (`isLoopback`) ห้าม loosen เป็น 0.0.0.0 เด็ดขาด เพราะจะเปิดไฟล์ทั้งเครื่องให้ LAN
|
||||
- เพิ่มโฟลเดอร์เข้า `SKIP_NAMES` เมื่อมีของที่ไม่ควรถูกสแกน; ชื่อ `<title>` ที่เป็นคำลอย ๆ ถูกแทนด้วยชื่อโฟลเดอร์ผ่าน `GENERIC_TITLES`
|
||||
- `ui/views/home.js` ซ่อนส่วนนี้เมื่อ API ไม่ตอบ (static host) — อย่าทำให้มันแสดงรายการปลอม
|
||||
- `sw.js` ข้าม `/projects/*` (ไม่แคชไฟล์ของโปรเจคอื่น)
|
||||
|
||||
## ขั้นตอนตรวจก่อนส่งงาน
|
||||
|
||||
```bash
|
||||
|
||||
@@ -54,6 +54,8 @@
|
||||
- **ฮีโร่**: พื้นไล่เฉดอุ่นอ่อน + การ์ดลอย 3 ใบ **สุ่มจากโปรเจคทั้งหมดใหม่ทุกครั้งที่โหลดหน้า** (Fisher–Yates, ไม่ซ้ำ) แสดงเฉพาะ ≥1080px, `aria-hidden`
|
||||
- **หลังบ้าน**: พาเนล + การ์ดรายการ, ปุ่มเลื่อนขึ้น/ลง/ลบ, แถบบันทึกแบบ sticky ด้านล่าง, `<dialog>` ยืนยันการลบ/ทิ้งการแก้ไข
|
||||
|
||||
- **โปรเจคในเครื่อง (localhost)**: รายการ 2 คอลัมน์ (≥700px) / 1 คอลัมน์ (มือถือ) — ไอคอนโฟลเดอร์ 34px, ชื่อโปรเจค 15px ตัวกลาง, บรรทัดล่างเป็น `<folder> · index.html · แก้ล่าสุด <วันที่ไทย>` (12.5px, โมโนสเปซเฉพาะชื่อโฟลเดอร์), ปุ่มขวา “เปิดหน้าแรก →” (ซ่อนข้อความเมื่อ <560px เหลือลูกศร) · แสดงเฉพาะเมื่อเปิดผ่านเซิร์ฟเวอร์ในเครื่อง
|
||||
|
||||
## 6. สิ่งที่ห้าม
|
||||
|
||||
- ห้ามใช้อิโมจิเป็นไอคอน (ใช้ SVG เส้น 1.7px ใน `ui/ui.js`)
|
||||
|
||||
@@ -45,6 +45,25 @@ server/publish.mjs
|
||||
|
||||
ถ้าไม่ได้ตั้ง `ADMIN_TOKEN` เซิร์ฟเวอร์จะอนุญาตเฉพาะเครื่องที่รันมันอยู่ — เปิดให้เครื่องอื่นแก้ต้องตั้งรหัสเสมอ
|
||||
|
||||
## โปรเจคในเครื่อง (เห็นเฉพาะ localhost)
|
||||
|
||||
หน้าแรกมีส่วน “โปรเจคในเครื่อง” ที่สแกนโฟลเดอร์ข้างเคียง (`D:\Git\workspace`) แล้วลิงก์ไป **หน้า index.html ของแต่ละโปรเจค** โดยไม่ต้องหาโฟลเดอร์เอง
|
||||
|
||||
```
|
||||
GET /api/local-projects → รายการ {folder, entry, url, title, updated_at}
|
||||
GET /projects/<โฟลเดอร์>/ → เสิร์ฟหน้าแรกของโฟลเดอร์นั้น
|
||||
```
|
||||
|
||||
| ตัวแปร | ค่าเริ่มต้น | ความหมาย |
|
||||
|---|---|---|
|
||||
| `WORKSPACE_DIR` | โฟลเดอร์แม่ของ repo นี้ (`D:\Git\workspace`) | ที่สแกนหาโปรเจค |
|
||||
| `WORKSPACE_DIR=none` | — | ปิดฟีเจอร์นี้ |
|
||||
|
||||
- รับเฉพาะ request จาก **เครื่องที่รันเซิร์ฟเวอร์ (loopback)** — เปิดให้เครื่องอื่นดูเว็บได้ แต่ดูไฟล์ในเครื่องไม่ได้
|
||||
- บล็อก `node_modules`, `.git`, `.hermes` และกัน path traversal
|
||||
- โฟลเดอร์ที่ไม่มี `index.html` (เช่น โปรเจคที่ต้องรัน `npm run dev`) จะไม่ถูกใส่ในรายการ
|
||||
- บนเว็บจริง (static host) API นี้ไม่มี → ส่วนนี้ซ่อนตัวเองอัตโนมัติ
|
||||
|
||||
## เนื้อหาเริ่มต้น (มาจากตารางรายชื่อโปรเจค)
|
||||
|
||||
| ชื่อโปรเจค | ซับโดเมน | อีเมล |
|
||||
|
||||
@@ -400,6 +400,123 @@
|
||||
cursor: grab;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------ โปรเจคในเครื่อง (localhost) */
|
||||
.local-section {
|
||||
margin-top: var(--space-4);
|
||||
}
|
||||
.local-section[hidden] {
|
||||
display: none;
|
||||
}
|
||||
.local-list {
|
||||
list-style: none;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 8px;
|
||||
}
|
||||
.local-row__link {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: var(--space-3);
|
||||
min-height: 64px;
|
||||
padding: 12px var(--space-4);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: var(--radius-md);
|
||||
background: var(--surface);
|
||||
color: inherit;
|
||||
text-decoration: none;
|
||||
transition:
|
||||
border-color 160ms var(--ease),
|
||||
background 160ms var(--ease),
|
||||
translate 160ms var(--ease);
|
||||
}
|
||||
.local-row__link:hover {
|
||||
border-color: var(--brand-300);
|
||||
background: var(--brand-50);
|
||||
translate: 0 -1px;
|
||||
text-decoration: none;
|
||||
}
|
||||
.local-row__icon {
|
||||
flex: none;
|
||||
display: grid;
|
||||
place-items: center;
|
||||
width: 34px;
|
||||
height: 34px;
|
||||
border-radius: 10px;
|
||||
background: var(--brand-50);
|
||||
border: 1px solid var(--brand-200);
|
||||
color: var(--brand-700);
|
||||
}
|
||||
.local-row__icon svg {
|
||||
width: 18px;
|
||||
height: 18px;
|
||||
}
|
||||
.local-row__body {
|
||||
flex: 1 1 auto;
|
||||
min-width: 0;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 2px;
|
||||
}
|
||||
.local-row__title {
|
||||
font-weight: 500;
|
||||
color: var(--brand-900);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.local-row__meta {
|
||||
font-size: 12.5px;
|
||||
color: var(--muted-foreground);
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
white-space: nowrap;
|
||||
}
|
||||
.local-row__meta code {
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
|
||||
font-size: 11.5px;
|
||||
background: var(--brand-50);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 5px;
|
||||
padding: 0 5px;
|
||||
}
|
||||
.local-row__go {
|
||||
flex: none;
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
font-size: 13.5px;
|
||||
font-weight: 500;
|
||||
color: var(--brand-700);
|
||||
}
|
||||
.local-row__go svg {
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
transition: transform 160ms var(--ease);
|
||||
}
|
||||
.local-row__link:hover .local-row__go svg {
|
||||
transform: translateX(3px);
|
||||
}
|
||||
@media (max-width: 560px) {
|
||||
.local-row__go-text {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
@media (min-width: 700px) {
|
||||
.local-list {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||||
}
|
||||
}
|
||||
|
||||
@media (prefers-reduced-motion: reduce) {
|
||||
.local-row__link:hover {
|
||||
translate: none;
|
||||
}
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------ empty state */
|
||||
.empty {
|
||||
margin-top: var(--space-4);
|
||||
|
||||
+1
-1
@@ -22,7 +22,7 @@
|
||||
content="รายชื่อโปรเจคอดิเรกของเรา เลือกโปรเจคที่อยากดูได้เลย"
|
||||
/>
|
||||
<meta name="theme-color" content="#8A4722" />
|
||||
<meta name="app-version" content="2026.11.19.4" />
|
||||
<meta name="app-version" content="2026.11.19.5" />
|
||||
<link rel="manifest" href="manifest.json" />
|
||||
<link rel="icon" type="image/png" sizes="192x192" href="icons/icon-192.png" />
|
||||
<link rel="icon" type="image/png" sizes="64x64" href="icons/favicon-64.png" />
|
||||
|
||||
+191
-1
@@ -4,20 +4,26 @@
|
||||
*
|
||||
* GET /api/projects อ่านข้อมูลปัจจุบัน
|
||||
* PUT /api/projects เขียน data/projects.json แล้ว commit + push ขึ้น Gitea
|
||||
* GET /api/local-projects รายชื่อโปรเจคในโฟลเดอร์ข้างเคียง (เครื่องตัวเองเท่านั้น)
|
||||
* GET /projects/<โฟลเดอร์>/… เสิร์ฟหน้า index.html ของโปรเจคใน D:\Git\workspace (เครื่องตัวเองเท่านั้น)
|
||||
* static เสิร์ฟไฟล์หน้าเว็บจาก root ของโปรเจค
|
||||
*
|
||||
* ใช้งาน:
|
||||
* node server/publish.mjs # http://localhost:8090
|
||||
* PORT=8090 ADMIN_TOKEN=xxxx node server/publish.mjs
|
||||
* PUBLISH_PUSH=0 node server/publish.mjs # เขียนไฟล์ + commit แต่ไม่ push
|
||||
* WORKSPACE_DIR=D:/Git/workspace node server/publish.mjs # เปลี่ยนที่สแกนโปรเจคข้างเคียง
|
||||
* WORKSPACE_DIR=none node server/publish.mjs # ปิดการเสิร์ฟโฟลเดอร์ข้างเคียง
|
||||
*
|
||||
* ความปลอดภัย:
|
||||
* - ถ้าตั้ง ADMIN_TOKEN ไว้ ต้องส่ง header x-admin-token ให้ตรงทุกครั้งที่เขียน
|
||||
* - ถ้าไม่ได้ตั้ง อนุญาตเฉพาะ request ที่มาจากเครื่องเดียวกัน (loopback)
|
||||
* - /api/local-projects และ /projects/* รับเฉพาะ request จากเครื่องเดียวกันเสมอ
|
||||
* (เปิดเซิร์ฟเวอร์ให้เครื่องอื่นดูเว็บได้ แต่เปิดดูไฟล์ในเครื่องไม่ได้)
|
||||
*/
|
||||
|
||||
import { createServer } from "node:http";
|
||||
import { readFile, stat, writeFile, mkdir } from "node:fs/promises";
|
||||
import { mkdir, readFile, readdir, stat, writeFile } from "node:fs/promises";
|
||||
import { execFile } from "node:child_process";
|
||||
import { promisify } from "node:util";
|
||||
import { timingSafeEqual } from "node:crypto";
|
||||
@@ -33,6 +39,26 @@ const HOST = process.env.HOST || "0.0.0.0";
|
||||
const ADMIN_TOKEN = (process.env.ADMIN_TOKEN || "").trim();
|
||||
const SHOULD_PUSH = process.env.PUBLISH_PUSH !== "0";
|
||||
|
||||
/* โฟลเดอร์โปรเจคข้างเคียง (ค่าเริ่มต้น: โฟลเดอร์แม่ของ repo นี้ = D:\Git\workspace) */
|
||||
const WORKSPACE_DIR =
|
||||
process.env.WORKSPACE_DIR === "none"
|
||||
? null
|
||||
: path.resolve(process.env.WORKSPACE_DIR || path.join(ROOT, ".."));
|
||||
|
||||
const ENTRY_CANDIDATES = [
|
||||
"index.html",
|
||||
"public/index.html",
|
||||
"ui/index.html",
|
||||
"dist/index.html",
|
||||
"frontend/dist/index.html",
|
||||
"frontend/index.html",
|
||||
];
|
||||
|
||||
const SKIP_NAMES = new Set(["node_modules", ".git", ".hermes", "archive", "Library", "Local Settings"]);
|
||||
|
||||
/** ชื่อที่ไม่มีข้อมูล (index.html ของบางโปรเจคตั้งไว้ลอย ๆ) → ใช้ชื่อโฟลเดอร์แทน */
|
||||
const GENERIC_TITLES = new Set(["หน้าแรก", "home", "index", "untitled", "document", "new page", ""]);
|
||||
|
||||
const MIME = {
|
||||
".html": "text/html; charset=utf-8",
|
||||
".css": "text/css; charset=utf-8",
|
||||
@@ -175,7 +201,158 @@ async function readData() {
|
||||
return JSON.parse(raw);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------ โปรเจคในเครื่อง (workspace) */
|
||||
|
||||
async function readEntryTitle(file) {
|
||||
try {
|
||||
const html = await readFile(file, "utf8");
|
||||
const head = html.slice(0, 8192);
|
||||
const title = head.match(/<title[^>]*>([^<]+)<\/title>/i);
|
||||
if (title) return title[1].trim().replace(/\s+/g, " ");
|
||||
const h1 = head.match(/<h1[^>]*>([\s\S]{1,160}?)<\/h1>/i);
|
||||
if (h1) return h1[1].replace(/<[^>]+>/g, "").trim().replace(/\s+/g, " ");
|
||||
} catch {
|
||||
/* อ่านไม่ได้ก็ใช้ชื่อโฟลเดอร์แทน */
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
async function findEntry(dir) {
|
||||
for (const rel of ENTRY_CANDIDATES) {
|
||||
try {
|
||||
const info = await stat(path.join(dir, rel));
|
||||
if (info.isFile()) return { rel, mtime: info.mtimeMs };
|
||||
} catch {
|
||||
/* ลองตัวถัดไป */
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
async function scanLocalProjects() {
|
||||
if (!WORKSPACE_DIR) return [];
|
||||
let dirents = [];
|
||||
try {
|
||||
dirents = await readdir(WORKSPACE_DIR, { withFileTypes: true });
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
const items = [];
|
||||
for (const dirent of dirents) {
|
||||
if (!dirent.isDirectory()) continue;
|
||||
const folder = dirent.name;
|
||||
if (folder.startsWith(".") || folder.startsWith("~") || SKIP_NAMES.has(folder)) continue;
|
||||
const dir = path.join(WORKSPACE_DIR, folder);
|
||||
if (dir === ROOT) continue;
|
||||
const entry = await findEntry(dir);
|
||||
if (!entry) continue;
|
||||
const rawTitle = (await readEntryTitle(path.join(dir, entry.rel))).trim();
|
||||
items.push({
|
||||
folder,
|
||||
entry: entry.rel,
|
||||
url: `/projects/${encodeURIComponent(folder)}/${entry.rel.replace(/index\.html$/, "")}`,
|
||||
title: GENERIC_TITLES.has(rawTitle.toLowerCase()) ? folder : rawTitle || folder,
|
||||
updated_at: new Date(entry.mtime).toISOString(),
|
||||
});
|
||||
}
|
||||
items.sort((a, b) => b.updated_at.localeCompare(a.updated_at));
|
||||
return items;
|
||||
}
|
||||
|
||||
async function sendFile(res, filePath) {
|
||||
const ext = path.extname(filePath).toLowerCase();
|
||||
const headers = { "content-type": MIME[ext] || "application/octet-stream" };
|
||||
if ([".html", ".css", ".js", ".mjs", ".json", ""].includes(ext)) {
|
||||
Object.assign(headers, NO_CACHE);
|
||||
} else {
|
||||
headers["cache-control"] = "public, max-age=300";
|
||||
}
|
||||
res.writeHead(200, headers);
|
||||
res.end(await readFile(filePath));
|
||||
}
|
||||
|
||||
function denyWorkspace(res, reason) {
|
||||
const payload = `<!doctype html><meta charset="utf-8"><title>เปิดไม่ได้</title>
|
||||
<div style="font-family:sans-serif;padding:40px"><h1>เปิดไม่ได้</h1><p>${reason}</p></div>`;
|
||||
res.writeHead(403, { "content-type": "text/html; charset=utf-8" });
|
||||
res.end(payload);
|
||||
}
|
||||
|
||||
async function serveWorkspace(req, res, pathname) {
|
||||
if (!WORKSPACE_DIR) {
|
||||
res.writeHead(404, { "content-type": "text/html; charset=utf-8" });
|
||||
res.end("<!doctype html><meta charset=utf-8><p>ปิดการเสิร์ฟโฟลเดอร์โปรเจค (WORKSPACE_DIR=none)</p>");
|
||||
return;
|
||||
}
|
||||
if (!isLoopback(req)) {
|
||||
denyWorkspace(res, "ลิงก์นี้เปิดได้เฉพาะเครื่องที่รันเซิร์ฟเวอร์ (localhost)");
|
||||
return;
|
||||
}
|
||||
|
||||
const rel = decodeURIComponent(pathname.replace(/^\/projects\/?/, ""));
|
||||
const target = path.resolve(WORKSPACE_DIR, `./${rel}`);
|
||||
const withinWorkspace = target === WORKSPACE_DIR || target.startsWith(WORKSPACE_DIR + path.sep);
|
||||
if (!withinWorkspace || /(^|[\\/])(node_modules|\.git|\.hermes)([\\/]|$)/i.test(target)) {
|
||||
denyWorkspace(res, "เส้นทางนี้ไม่ได้รับอนุญาต");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const info = await stat(target);
|
||||
if (info.isDirectory()) {
|
||||
if (!pathname.endsWith("/")) {
|
||||
res.writeHead(301, { location: `${pathname}/` });
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
const indexFile = path.join(target, "index.html");
|
||||
try {
|
||||
const indexInfo = await stat(indexFile);
|
||||
if (!indexInfo.isFile()) throw new Error("not a file");
|
||||
} catch {
|
||||
res.writeHead(404, { "content-type": "text/html; charset=utf-8" });
|
||||
res.end(
|
||||
`<!doctype html><meta charset="utf-8"><title>ไม่มี index.html</title>
|
||||
<div style="font-family:sans-serif;padding:40px">
|
||||
<h1>โฟลเดอร์นี้ไม่มี index.html</h1>
|
||||
<p>โปรเจคนี้ต้องรัน dev server แยก (เช่น <code>npm run dev</code>)</p>
|
||||
</div>`,
|
||||
);
|
||||
return;
|
||||
}
|
||||
await sendFile(res, indexFile);
|
||||
return;
|
||||
}
|
||||
await sendFile(res, target);
|
||||
} catch {
|
||||
res.writeHead(404, { "content-type": "text/html; charset=utf-8" });
|
||||
res.end(
|
||||
`<!doctype html><meta charset="utf-8"><title>ไม่พบไฟล์</title>
|
||||
<div style="font-family:sans-serif;padding:40px"><h1>ไม่พบไฟล์นี้</h1>
|
||||
<p><a href="/">กลับไปหน้ารายชื่อโปรเจค</a></p></div>`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleApi(req, res, pathname) {
|
||||
if (pathname === "/api/local-projects" && req.method === "GET") {
|
||||
if (!canWrite(req)) {
|
||||
sendJson(res, 403, { ok: false, error: "รายชื่อโปรเจคในเครื่องเปิดได้เฉพาะเครื่องที่รันเซิร์ฟเวอร์" });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const projects = await scanLocalProjects();
|
||||
sendJson(res, 200, {
|
||||
ok: true,
|
||||
workspace: WORKSPACE_DIR ? path.basename(WORKSPACE_DIR) : null,
|
||||
projects,
|
||||
});
|
||||
} catch (error) {
|
||||
sendJson(res, 500, { ok: false, error: `สแกนโฟลเดอร์ไม่สำเร็จ: ${error.message}` });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (pathname === "/api/projects" && req.method === "GET") {
|
||||
try {
|
||||
const data = await readData();
|
||||
@@ -287,6 +464,16 @@ const server = createServer((req, res) => {
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// ลิงก์ไปหน้าแรกของแต่ละโปรเจคในโฟลเดอร์ข้างเคียง (เฉพาะเครื่องนี้)
|
||||
if (url.pathname === "/projects" || url.pathname.startsWith("/projects/")) {
|
||||
serveWorkspace(req, res, url.pathname).catch((error) => {
|
||||
res.writeHead(500, { "content-type": "text/plain; charset=utf-8" });
|
||||
res.end(`อ่านโฟลเดอร์โปรเจคไม่สำเร็จ: ${error.message}`);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
handleApi(req, res, url.pathname).catch((error) => {
|
||||
sendJson(res, 500, { ok: false, error: error.message });
|
||||
});
|
||||
@@ -297,4 +484,7 @@ server.listen(PORT, HOST, () => {
|
||||
console.log(`[hobbypj] เซิร์ฟเวอร์ทำงานที่ http://localhost:${PORT}`);
|
||||
console.log(`[hobbypj] เขียนข้อมูลได้: ${writable}`);
|
||||
console.log(`[hobbypj] ไฟล์ข้อมูล: ${path.relative(ROOT, DATA_FILE)} · push: ${SHOULD_PUSH ? "เปิด" : "ปิด"}`);
|
||||
console.log(
|
||||
`[hobbypj] โปรเจคในเครื่อง: ${WORKSPACE_DIR ? `${WORKSPACE_DIR} → /projects/<โฟลเดอร์>/` : "ปิด"} (เฉพาะ localhost)`,
|
||||
);
|
||||
});
|
||||
@@ -2,7 +2,7 @@
|
||||
กติกา: bump VERSION เฉพาะเมื่อแก้ไฟล์นี้หรือรายการ PRECACHE
|
||||
เนื้อหา (HTML/CSS/JS/JSON) ใช้ network-first จึงไม่ต้อง bump */
|
||||
|
||||
const VERSION = "2026.11.19.4";
|
||||
const VERSION = "2026.11.19.5";
|
||||
const CACHE = `hobbypj-${VERSION}`;
|
||||
|
||||
const PRECACHE = [
|
||||
@@ -66,8 +66,8 @@ self.addEventListener("fetch", (event) => {
|
||||
const url = new URL(request.url);
|
||||
if (url.origin !== self.location.origin) return;
|
||||
|
||||
// อย่าแตะ API: คำขอเผยแพร่/อ่านข้อมูลสดต้องถึงเซิร์ฟเวอร์เสมอ
|
||||
if (url.pathname.includes("/api/")) return;
|
||||
// อย่าแตะ API และหน้าโปรเจคข้างเคียง: ต้องถึงเซิร์ฟเวอร์เสมอ (ไม่ควรเอาไฟล์โปรเจคอื่นมาแคช)
|
||||
if (url.pathname.includes("/api/") || url.pathname.startsWith("/projects/")) return;
|
||||
|
||||
const isContent =
|
||||
request.destination === "document" ||
|
||||
|
||||
+16
@@ -189,6 +189,22 @@ export async function probePublishApi() {
|
||||
}
|
||||
}
|
||||
|
||||
/** รายชื่อโปรเจคในโฟลเดอร์ข้างเคียง (มีเฉพาะเมื่อเปิดผ่านเซิร์ฟเวอร์หลังบ้านในเครื่องนี้) */
|
||||
export async function fetchLocalProjects() {
|
||||
try {
|
||||
const res = await fetch("api/local-projects", { cache: "no-store" });
|
||||
if (!res.ok) return { available: false, workspace: null, projects: [] };
|
||||
const body = await res.json();
|
||||
return {
|
||||
available: true,
|
||||
workspace: body.workspace || null,
|
||||
projects: Array.isArray(body.projects) ? body.projects : [],
|
||||
};
|
||||
} catch {
|
||||
return { available: false, workspace: null, projects: [] };
|
||||
}
|
||||
}
|
||||
|
||||
export function downloadJson(data) {
|
||||
const payload = toPublishPayload(data);
|
||||
const blob = new Blob([`${JSON.stringify(payload, null, 2)}\n`], {
|
||||
|
||||
@@ -2,6 +2,16 @@
|
||||
|
||||
import { escapeHtml, hostOf, icon, normalizeUrl } from "../ui.js";
|
||||
import { initReveal, splitWordsHtml } from "../motion.js";
|
||||
import { fetchLocalProjects } from "../data.js";
|
||||
|
||||
const TH_DATE = new Intl.DateTimeFormat("th-TH", { day: "numeric", month: "short", year: "numeric" });
|
||||
function formatDate(iso) {
|
||||
try {
|
||||
return TH_DATE.format(new Date(iso));
|
||||
} catch {
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/** สุ่มลำดับด้วย Fisher–Yates แล้วหยิบมา count ใบ (ไม่ซ้ำ) */
|
||||
function sampleRandom(list, count) {
|
||||
@@ -86,6 +96,40 @@ export function homeView(ctx) {
|
||||
initReveal(gridEl);
|
||||
}
|
||||
|
||||
function localRowHtml(item) {
|
||||
const folder = escapeHtml(item.folder);
|
||||
const entry = escapeHtml(item.entry || "index.html");
|
||||
const title = escapeHtml(item.title || item.folder);
|
||||
const date = formatDate(item.updated_at);
|
||||
return `
|
||||
<li class="local-row">
|
||||
<a class="local-row__link" href="${escapeHtml(item.url || "#")}" target="_blank" rel="noopener noreferrer">
|
||||
<span class="local-row__icon" aria-hidden="true">${icon("folder")}</span>
|
||||
<span class="local-row__body">
|
||||
<span class="local-row__title">${title}</span>
|
||||
<span class="local-row__meta"><code>${folder}</code> · ${entry}${
|
||||
date ? ` · แก้ล่าสุด ${date}` : ""
|
||||
}</span>
|
||||
</span>
|
||||
<span class="local-row__go"><span class="local-row__go-text">เปิดหน้าแรก</span>${icon("arrowRight")}</span>
|
||||
</a>
|
||||
</li>`;
|
||||
}
|
||||
|
||||
/** โปรเจคในเครื่อง — มีเฉพาะเมื่อเปิดผ่านเซิร์ฟเวอร์หลังบ้าน (ไม่ขึ้นบนเว็บจริง) */
|
||||
async function loadLocalProjects() {
|
||||
const box = root?.querySelector("[data-local]");
|
||||
const list = root?.querySelector("[data-local-list]");
|
||||
const countEl = root?.querySelector("[data-local-count]");
|
||||
if (!box || !list) return;
|
||||
const result = await fetchLocalProjects();
|
||||
if (!root || !result.projects.length) return;
|
||||
if (countEl) countEl.textContent = `${result.projects.length} โฟลเดอร์`;
|
||||
list.innerHTML = result.projects.map(localRowHtml).join("");
|
||||
box.hidden = false;
|
||||
initReveal(box);
|
||||
}
|
||||
|
||||
return {
|
||||
mount(container) {
|
||||
root = container;
|
||||
@@ -150,6 +194,19 @@ export function homeView(ctx) {
|
||||
</div>
|
||||
|
||||
<div data-grid aria-labelledby="projects-title"></div>
|
||||
|
||||
<section class="local-section" data-local hidden aria-labelledby="local-title">
|
||||
<div class="section-head" data-reveal="fade">
|
||||
<div>
|
||||
<h2 class="section-head__title" id="local-title">โปรเจคในเครื่อง</h2>
|
||||
<p class="section-head__sub">
|
||||
เปิดหน้าแรกของแต่ละโฟลเดอร์ในเครื่องนี้โดยตรง — เห็นเฉพาะเครื่องที่รันเซิร์ฟเวอร์ ไม่ขึ้นบนเว็บจริง
|
||||
</p>
|
||||
</div>
|
||||
<p class="section-head__count" data-local-count></p>
|
||||
</div>
|
||||
<ul class="local-list" data-local-list></ul>
|
||||
</section>
|
||||
</div>`;
|
||||
|
||||
gridEl = root.querySelector("[data-grid]");
|
||||
@@ -176,6 +233,7 @@ export function homeView(ctx) {
|
||||
});
|
||||
|
||||
renderGrid();
|
||||
loadLocalProjects();
|
||||
},
|
||||
destroy() {
|
||||
root = null;
|
||||
|
||||
Reference in new issue
Block a user