diff --git a/CLAUDE.md b/CLAUDE.md index ddeaf22..3f435fc 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -244,7 +244,7 @@ Two files in `apps/worker/migrations/`: - `0002_token_version.sql` — adds `users.token_version` for session revocation **HTTP setup route (production):** -`POST /api/setup` (legacy alias `/setup`) with a JSON body `{"secret": ""}` runs migrations via the shared `lib/migrations-runner.ts` (`runMigrations`). Two migrations: +The public initialization page lives at `/setup` (an SPA route). `POST /api/setup` with a JSON body `{"secret": ""}` runs migrations via the shared `lib/migrations-runner.ts` (`runMigrations`). Two migrations: - `schema_v1` — identical DDL as `0001_init.sql`, but parameterized by `TABLE_PREFIX` and tracked in `_schema_migrations` table - `schema_v2` — adds `token_version` to `users` (mirrors `0002_token_version.sql`) diff --git a/README.md b/README.md index 3c93932..72b0d78 100644 --- a/README.md +++ b/README.md @@ -136,6 +136,8 @@ setup page, it does not disable the `POST` migration channel. When logged in as migrations can also be run from the admin panel via `POST /api/admin/setup` — no setup secret needed, not IP rate-limited, and each run is written to the audit log. +The initialization page itself is served by the SPA at `/setup` (not an API route). + After deploying, the workflow first confirms through the Cloudflare API that the uploaded version serves 100% of production traffic, then probes the public URL. A `403` carrying `cf-mitigated: challenge` (e.g. Bot Fight Mode challenging the CI runner) only produces a diff --git a/README.zh-CN.md b/README.zh-CN.md index 5952149..3b74f57 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -130,6 +130,8 @@ curl -X POST https://your-worker.workers.dev/api/setup \ `{ "enabled": true | false }`,反映 `setup_page_enabled` 设置(未设置时默认启用), 它只隐藏 setup 页面、不会关闭 `POST` 迁移通道。以管理员身份登录后,也可在管理面板 通过 `POST /api/admin/setup` 触发迁移 — 无需 setup 密钥、不受 IP 限流,且每次执行都会写入审计日志。 + +初始化页面由 SPA 提供,路径为 `/setup`(非 API 路由)。 部署后还会通过 Cloudflare API 核对本次上传的版本承载 100% 流量,再检查公开 URL。 公开 URL 返回带 `cf-mitigated: challenge` 的 403 时仅警告(HTTP/数据库检查未能验证), 其余非 2xx 或错误的 JSON 均失败;无需关闭 Bot Fight Mode。 diff --git a/apps/worker/src/index.ts b/apps/worker/src/index.ts index bf0853b..25b8472 100644 --- a/apps/worker/src/index.ts +++ b/apps/worker/src/index.ts @@ -116,10 +116,9 @@ app.route('/api/analytics', analyticsRoutes); app.route('/api/admin', adminRoutes); // ─── Setup / migration endpoint ─────────────────────────────────────────────── -// Mounted at /api/setup to match the rest of the public API; /setup is kept as a -// backward-compatible alias for callers that predate the /api prefix. +// Mounted at /api/setup only. /setup is the public SPA page path (served by the +// SPA fallback below); it is no longer an API alias. app.route('/api/setup', setupRoutes); -app.route('/setup', setupRoutes); // ─── Health check ───────────────────────────────────────────────────────────── app.get('/api/health', (c) => c.json({ status: 'ok', timestamp: Date.now() })); diff --git a/apps/worker/test/setup.test.ts b/apps/worker/test/setup.test.ts index c606261..29f7fce 100644 --- a/apps/worker/test/setup.test.ts +++ b/apps/worker/test/setup.test.ts @@ -1,6 +1,7 @@ // Setup endpoint tests — GET /api/setup/state (public), POST /api/setup (JSON -// body + legacy header, per-IP rate limit), GET /api/setup/* (405), and the -// admin keyless upgrade POST /api/admin/setup (shared migration runner + audit). +// body + legacy header, per-IP rate limit), GET /api/setup/* (405), the SPA +// page at GET /setup, and the admin keyless upgrade POST /api/admin/setup +// (shared migration runner + audit). import assert from 'node:assert/strict'; import { test } from 'node:test'; import { app } from '../src/index.ts'; @@ -70,11 +71,22 @@ test('GET /api/setup/*: 返回 405 + Allow: POST', async () => { assert.match(body.error, /POST \/api\/setup/); }); -test('GET /setup/*: 旧路径别名同样返回 405 + Allow: POST', async () => { - const ctx = makeEnv(); - const res = await request(app, '/setup/anything', { method: 'GET' }, ctx); - assert.equal(res.status, 405); - assert.equal(res.headers.get('Allow'), 'POST'); +test('GET /setup: 落到 SPA 页面(200 + text/html),不再被 API 405 兜底', async () => { + const ctx = makeEnv({ + ASSETS: { + fetch: async () => + new Response('', { + headers: { 'content-type': 'text/html; charset=utf-8' }, + }), + }, + }); + const res = await request(app, '/setup', { method: 'GET' }, ctx); + assert.equal(res.status, 200); + assert.match(res.headers.get('content-type') ?? '', /text\/html/); + assert.doesNotMatch( + res.headers.get('content-type') ?? '', + /application\/json/, + ); }); // ─── POST /api/setup ────────────────────────────────────────────────────────── @@ -163,24 +175,6 @@ test('POST /api/setup: 旧 X-Setup-Secret 头方式仍可用', async () => { assert.ok(Array.isArray((await res.json()).migrations)); }); -test('POST /setup: 旧路径别名 + X-Setup-Secret 头仍可用', async () => { - const ctx = makeEnv({ DB: new D1Mock(false) as unknown as Env['DB'] }); - const res = await request( - app, - '/setup', - { - method: 'POST', - headers: { - 'X-Setup-Secret': 'test-setup-secret', - 'cf-connecting-ip': '5.5.5.6', - }, - }, - ctx, - ); - assert.equal(res.status, 200); - assert.ok(Array.isArray((await res.json()).migrations)); -}); - test('POST /api/setup: 每 IP 每 10 分钟限 3 次,第 4 次 429 + Retry-After(计数先于密钥校验)', async () => { const ctx = makeEnv(); const headers = {