Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: CI

on:
pull_request:
push:
branches: [main]
workflow_dispatch:

permissions:
contents: read

concurrency:
group: ci-${{ github.ref }}
cancel-in-progress: true

jobs:
check:
name: Validate
runs-on: ubuntu-latest
env:
WRANGLER_SEND_METRICS: "false"
steps:
- uses: actions/checkout@v7

- uses: pnpm/action-setup@v6

- uses: actions/setup-node@v7
with:
node-version: 24
cache: pnpm

- name: Install dependencies
run: pnpm install --frozen-lockfile

- name: Lint
run: pnpm lint

- name: Format check
run: pnpm format:check

- name: Type check
run: pnpm typecheck

- name: Build
run: pnpm build

- name: Test
run: pnpm test
50 changes: 50 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
name: Deploy
run-name: ${{ github.event_name == 'workflow_run' && github.event.workflow_run.display_title || format('Manual deploy by {0}', github.actor) }}

# ONE-167: 合并即自动部署 Cloudflare,合并/部署关口归用户。
# Deploy only runs after CI on main is green, or by manual dispatch.

on:
workflow_dispatch:
workflow_run:
workflows: ["CI"]
types: [completed]

permissions:
contents: read

jobs:
deploy:
if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'main') }}
runs-on: ubuntu-latest
env:
WRANGLER_SEND_METRICS: "false"
steps:
- uses: actions/checkout@v7

- uses: pnpm/action-setup@v6

- uses: actions/setup-node@v7
with:
node-version: 24
cache: pnpm

- run: pnpm install --frozen-lockfile

- name: Build web
run: pnpm build:web

# Secrets are written to a private temp file and passed to wrangler as a
# secrets file — they never appear in the repo, logs, or wrangler.toml.
- name: Materialize secrets
env:
SESSION_SECRET: ${{ secrets.SESSION_SECRET }}
SETUP_SECRET: ${{ secrets.SETUP_SECRET }}
run: node scripts/materialize-secrets.mjs

- name: Deploy Worker
working-directory: worker
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
run: pnpm exec wrangler deploy --secrets-file "$RUNNER_TEMP/secrets.json"
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules
dist
.wrangler
*.local
.DS_Store
worker/src/db/schema.generated.ts
.dev.vars
*.tsbuildinfo
web/dist
pr-body.md
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# nav

单人个人导航 / 浏览器起始页(公开 + 私有双层链接)。Cloudflare Workers + D1,域名 `nav.oneboy.app`。

Monorepo:
- `worker/` — Hono Worker 后端(D1 schema 与迁移、鉴权、三组 API、setup 流程)
- `web/` — React + Vite 前端(子单 B 开发中,目前为占位)

## 本地开发

```bash
pnpm install
cp worker/.dev.vars.example worker/.dev.vars # 填入本地 SESSION_SECRET / SETUP_SECRET
pnpm dev # 同时起 worker (wrangler dev) 与 web (vite)
```

`worker/.dev.vars` 与真实密钥永不入库(`.gitignore` 已排除)。

## 测试 / 校验

```bash
pnpm typecheck # web + worker
pnpm test # worker 单测(vitest,真 SQLite 内存库跑迁移与 API 契约)
pnpm lint && pnpm format:check
```

## 部署

合并 `main` 后 GitHub Actions(`.github/workflows/deploy.yml`)自动部署 Cloudflare。
需要在仓库 Secrets 配置(见 PR 说明,凭证不写入代码 / issue / 评论):

| Secret | 用途 |
| ----------------------- | -------------------------- |
| `CLOUDFLARE_API_TOKEN` | wrangler 部署 |
| `CLOUDFLARE_ACCOUNT_ID` | Cloudflare 账户 |
| `SESSION_SECRET` | 会话签名密钥(≥16 位随机) |
| `SETUP_SECRET` | setup 初始化令牌 |

`worker/wrangler.toml` 中的 `CLOUDFLARE_ACCOUNT_ID_PLACEHOLDER`、`D1_DATABASE_ID_PLACEHOLDER` 需替换为实际值(或改用 wrangler 部署时的环境注入)。

## 初始化(setup)

线上首次部署、库中无账号时开放 setup:`POST /api/auth/setup`(header `X-Setup-Secret`)创建唯一所有者账号,创建后 setup 永久关闭,不设注册接口。

## API 契约

见 `docs/api-contract.md`(与前端冻结的 DTO 清单)。
29 changes: 29 additions & 0 deletions biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://biomejs.dev/schemas/2.6.2/schema.json",
"files": {
"includes": [
"**",
"!**/node_modules",
"!**/dist",
"!**/.wrangler",
"!**/web/public",
"!**/web/dist",
"!**/worker/.wrangler",
"!**/worker/src/db/schema.generated.ts"
]
},
"formatter": {
"enabled": true,
"indentStyle": "space"
},
"assist": { "actions": { "source": { "organizeImports": "on" } } },
"linter": {
"enabled": true,
"rules": {
"preset": "recommended",
"suspicious": {
"noExplicitAny": "off"
}
}
}
}
121 changes: 121 additions & 0 deletions docs/api-contract.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# nav API 契约(冻结,ONE-168)

字段命名 camelCase。变更须回父单 ONE-167 由 Mike 裁决。

## 公开

### GET /api/public

200 JSON。只含 `visibility=public` 的分类与链接(分类内嵌其公开链接);私有分类、私有链接、以及「private 分类下的 public 链接」(可见性取更严一方)完全不出现。响应 `Cache-Control: public, max-age=60`。

```json
{
"categories": [
{
"id": "uuid",
"name": "分类名",
"icon": null,
"sortOrder": 0,
"links": [
{
"id": "uuid",
"categoryId": "uuid",
"title": "标题",
"url": "https://…",
"iconUrl": null,
"description": null,
"sortOrder": 0
}
]
}
],
"searchEngines": [
{
"id": "uuid",
"name": "Google",
"urlTemplate": "https://www.google.com/search?q={q}",
"icon": null,
"sortOrder": 0,
"isDefault": true
}
],
"settings": {
"siteTitle": "nav",
"backgroundUrl": "",
"theme": "system",
"columns": 0
}
}
```

## 私有

### GET /api/private

需有效会话 Cookie(`nav_session`,HttpOnly / Secure / SameSite=Lax)。未登录一律:
- 状态 `401`
- Body `{"error":"unauthorized"}`
- Header `Cache-Control: no-store`

已登录返回与 `/api/public` **同结构**的全量数据(公开 + 私有),`Cache-Control: no-store`。

## 鉴权(/api/auth/*)

| 方法 | 路径 | 说明 |
| ---- | ---------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| GET | `/api/auth/setup` | `{"enabled":true}` 表示可创建所有者 |
| POST | `/api/auth/setup` | header `X-Setup-Secret`;body `{username, password}`(password ≥ 8)→ 201 并下发会话;已有账号 → 403 |
| POST | `/api/auth/login` | body `{username, password}` → `{success, user}`;若已启用 TOTP → `{requires2fa:true, tempToken}`;失败 401 `{"error":"invalid credentials","remainingAttempts":N}`;连续失败 5 次锁定 15 分钟 → 429 |
| POST | `/api/auth/2fa/verify` | body `{tempToken, code}`(TOTP 6 位)→ 200 并下发会话;错误码 401 |
| POST | `/api/auth/logout` | 吊销当前会话 |
| GET | `/api/auth/session` | 会话探测:`{"authenticated":true,"userId":…}`;未登录 401 JSON |

Passkey(WebAuthn,/api/auth/passkey/*,@simplewebauthn/server v14):

| 方法 | 路径 | 说明 |
| ---------- | ------------------------------------------------------------ | ------------------------------------------------- |
| POST | `/register/options` | 登录后注册 passkey 的 options(rpID = 请求 host) |
| POST | `/register/verify` | 校验注册响应,落库 `passkey_credentials` |
| POST | `/authenticate/options` | 免登录取断言 options(可带 `{username}`) |
| POST | `/authenticate/verify` | 校验断言,成功即建会话 |
| GET/DELETE | `/credentials` | 查看 / 清除 passkey |
| GET | `/totp`、POST `/totp/setup`、`/totp/enable`、`/totp/disable` | TOTP 管理(disable 需再输密码) |

登录失败锁定:`login_attempts` 表按 username+IP 计数,5 次失败锁定 15 分钟;`/2fa/verify` 对同一 tempToken 连续 5 次错误即作废。

## 后台(/api/admin/*,全部需会话,未登录 401 JSON `{"error":"unauthorized"}`)

### 分类

- `GET /api/admin/categories` → `{categories:[{id,name,icon,sortOrder,visibility,createdAt,updatedAt}]}`
- `POST /api/admin/categories` body `{name, icon?, sortOrder?, visibility?}`(visibility: `public`|`private`)→ 201 `{id,…}`
- `PUT /api/admin/categories/:id`(字段同上,部分更新)→ 更新后对象
- `DELETE /api/admin/categories/:id` → `{success:true}`(级联删除其链接)
- `POST /api/admin/categories/reorder` body `{ids:[…]}`(顺序即 sortOrder)→ `{success:true}`

### 链接

- `GET /api/admin/links` → `{links:[{id,categoryId,title,url,iconUrl,description,sortOrder,visibility,createdAt,updatedAt}]}`
- `POST /api/admin/links` body `{title, url, categoryId, iconUrl?, description?, sortOrder?, visibility?}` → 201 `{id}`
- `PUT /api/admin/links/:id`(部分更新)→ 更新后对象
- `DELETE /api/admin/links/:id` → `{success:true}`
- `POST /api/admin/links/reorder` body `{ids:[…]}` → `{success:true}`

### 搜索引擎

- `GET /api/admin/search-engines` → `{searchEngines:[{id,name,urlTemplate,icon,sortOrder,isDefault}]}`
- `POST /api/admin/search-engines` body `{name, urlTemplate(含 {q}), icon?, sortOrder?, isDefault?}` → 201 `{id}`
- `PUT /api/admin/search-engines/:id` → `{id}`(设 `isDefault:true` 会自动清掉其他默认)
- `DELETE /api/admin/search-engines/:id` → `{success:true}`
- `POST /api/admin/search-engines/reorder` body `{ids:[…]}` → `{success:true}`

### 站点设置

- `GET /api/admin/settings` → `{siteTitle, backgroundUrl, theme, columns}`
- `PUT /api/admin/settings` body 可含 `{siteTitle, backgroundUrl, theme(light|dark|system), columns(1–12)}` → 更新后对象

## 说明

- favicon 自动抓取不在后端做:前端直接请求目标站 / 公共 favicon 服务,失败回退首字圆;后端只存 `iconUrl` 字段。
- 点击计数、服务健康状态为预留字段,v1 不实现(SPEC 第 5 节)。
- 会话 Cookie `nav_session`:HS256 JWT 签名 + `sessions` 表吊销,7 天有效。
29 changes: 29 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "nav",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {
"dev": "concurrently \"pnpm dev:worker\" \"pnpm dev:web\"",
"dev:web": "pnpm --filter web dev",
"dev:worker": "pnpm --filter worker dev",
"lint": "biome lint .",
"format:check": "biome format .",
"format": "biome format --write .",
"build": "pnpm --filter web build && pnpm --filter worker build",
"build:web": "pnpm --filter web build",
"build:worker": "pnpm --filter worker build",
"test": "pnpm --filter worker test",
"deploy": "pnpm build:web && pnpm --filter worker deploy",
"typecheck": "pnpm --filter web typecheck && pnpm --filter worker typecheck"
},
"devDependencies": {
"@biomejs/biome": "^2.5.0",
"concurrently": "^10.0.3",
"typescript": "^7.0.0"
},
"engines": {
"node": ">=22.0.0"
},
"packageManager": "pnpm@12.14.0"
}
Loading
Loading