o2o-site-AEO/frontend/site/scripts/serve-sites.mjs
Mina Choi 6784e59ca5 최초 커밋 — 기존 코드 전체 + 문서 체계 신설
git 저장소가 없어 히스토리·협업 기반이 아예 없던 상태를 연다.
함께 문서를 재편했다. 그동안 문서가 있어도 "이 제품이 뭘 푸는가"와
"어떻게 도는가"를 담은 문서가 없어서, 목표 문장이 backend/frontend
README 두 곳에 복붙돼 있었다 — 상위 문서가 없어 아래로 샌 것이다.

신설
  README.md               레포 진입점 + 문서 지도 + 문서 규칙 4가지
  AGENTS.md               에이전트·신규 합류자용 함정 목록과 규약
                          (CLAUDE.md 는 여기로 걸린 심볼릭 링크)
  docs/PRODUCT.md         제품 정의 — 문제·사용자·원칙·**non-goals**·성공 기준
  docs/ARCHITECTURE.md    payload 경계·발행 파이프라인·서빙 결정·앱 분리 설계

이동
  backend/docs/DECISIONS.md → docs/DECISIONS.md
    백엔드만의 결정이 아니다. 게다가 코드 주석 ~25곳이 이미
    `docs/DECISIONS.md` 로 적고 있어 레포 루트 기준으로는 그게 맞다.

갱신
  docs/DEPLOY.md          서빙 결정 반영 — nginx 정적 서빙이 지금 경로(3절),
                          Azure 는 나중에 켤 때(4절)로 분리
  docs/ARCHITECTURE.md    사이트 = 한 장(2026-08-31) 구조 반영
  docs/COLLECTION_SEO_AEO_FLOW.md
                          robots.txt·sitemap.xml 은 오리진 루트에만 굽는다는 점 명시
  frontend/site/scripts/prerender.ts
                          헤더 주석의 렌더 보고서 경로가 실제(422줄)와 달라 수정

.gitignore
  ★ CLAUDE.md 를 더 이상 무시하지 않는다. 에이전트 지침은 팀과 모든
    에이전트가 공유하는 규약이라 커밋해야 한다 — 무시하면 클론한 사람이
    "배포 후 republish_all.py 필수" 같은 함정을 전달받지 못한다.
    개인용 오버라이드는 ~/.claude/CLAUDE.md 에 둔다.
2026-08-31 13:57:59 +09:00

53 lines
2.1 KiB
JavaScript

/**
* 발행 사이트 정적 서버.
*
* ★ python -m http.server 를 쓰지 않는 이유
* `/s/mmg` (끝 슬래시 없음)로 들어오면 404 를 준다. 사장님이 주소창에 치는 형태가 그건데
* 열리지 않으면 "발행했는데 안 나온다"가 된다. 여기서는 디렉토리면 index.html 로 넘긴다.
*
* ★ 이 서버는 개발용이다. 운영에서는 out/ 을 nginx·CDN 이 그대로 서빙한다 —
* 그때도 같은 규칙(디렉토리 → index.html)만 맞추면 된다.
*/
import {createReadStream, existsSync, statSync} from 'node:fs';
import {createServer} from 'node:http';
import {dirname, extname, join, normalize, resolve} from 'node:path';
import {fileURLToPath} from 'node:url';
const HERE = dirname(fileURLToPath(import.meta.url));
const OUT = resolve(HERE, '..', 'out');
const PORT = Number(process.env.PORT ?? 3001);
const MIME = {
'.html': 'text/html; charset=utf-8',
'.js': 'text/javascript; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.xml': 'application/xml; charset=utf-8',
'.txt': 'text/plain; charset=utf-8',
'.woff2': 'font/woff2',
'.svg': 'image/svg+xml',
'.png': 'image/png',
'.jpg': 'image/jpeg',
};
createServer((req, res) => {
// 한글 슬러그가 퍼센트 인코딩으로 오므로 반드시 디코드한다.
const raw = decodeURIComponent((req.url ?? '/').split('?')[0]);
// 경로 탈출(../) 차단 — 정적 서버의 기본 안전장치다.
const rel = normalize(raw).replace(/^(\.\.[/\\])+/, '');
let file = join(OUT, rel);
if (existsSync(file) && statSync(file).isDirectory()) file = join(file, 'index.html');
if (!existsSync(file)) {
res.writeHead(404, {'Content-Type': 'text/html; charset=utf-8'});
res.end('<h1>404</h1><p>발행된 사이트가 없습니다. 발행 후 자동으로 생성됩니다.</p>');
return;
}
res.writeHead(200, {'Content-Type': MIME[extname(file)] ?? 'application/octet-stream'});
createReadStream(file).pipe(res);
}).listen(PORT, () => {
console.log(`[serve] 발행 사이트 → http://localhost:${PORT}/s/<주소>`);
});