# Cloudflare Pages 部署

這個 repo 適合部署到 Cloudflare Pages：

- 靜態內容直接由 Pages 提供
- LINE webhook 使用 `functions/api/line-review.js`
- GitHub Actions、Threads 發文腳本與 repo 內容維持不變

## 檔案對應

- 靜態網站根目錄：repo 根目錄
- Cloudflare Pages Function：[functions/api/line-review.js](/Users/chunchun/文件/project/ai-knowledge/functions/api/line-review.js)
- Wrangler 設定：[wrangler.toml](/Users/chunchun/文件/project/ai-knowledge/wrangler.toml)
- 舊版 Netlify webhook：`netlify/functions/line-review-webhook.mjs`

Netlify 檔案目前保留作為過渡相容，不再是主要部署目標。

## 建立 Pages 專案

依 Cloudflare 官方文件，Pages Functions 會自動讀取 repo 根目錄下的 `/functions` 目錄，並把檔案路徑對應成路由。這個專案的 webhook 路由會是：

```text
POST /api/line-review
```

建議做法：

1. 到 Cloudflare Dashboard 建立一個 Pages 專案。
2. 連接 GitHub repo：`chunchun1213/ai-knowledge`
3. Build command 留空
4. Build output directory 設為 repo 根目錄，或由 `wrangler.toml` 的 `pages_build_output_dir = "."` 控制

## 本機預覽

若本機已安裝或可使用 `wrangler`，可先預覽：

```bash
npx wrangler pages dev .
```

預設可測：

```text
http://127.0.0.1:8788/api/line-review
```

這一步主要用來驗證：

- 靜態檔案是否可正確提供
- `functions/api/line-review.js` 是否可被 Pages Functions 載入
- webhook 路由是否存在

## Pages 環境變數

到 Cloudflare Pages 專案設定：

```text
Settings -> Environment variables
```

Production / Preview 都至少要設：

```text
LINE_CHANNEL_SECRET
GITHUB_TOKEN
GITHUB_OWNER=chunchun1213
GITHUB_REPO=ai-knowledge
GITHUB_REF=main
GITHUB_WORKFLOW_FILE=review-inspiration-post.yml
```

注意：

- `GITHUB_TOKEN` 這裡指的是你自己建立的 GitHub Personal Access Token，不是 GitHub Actions 內建 token。
- 需要有 `repo` 與 `workflow` 權限，因為 webhook 會 dispatch GitHub Actions workflow。

## GitHub Actions 配合設定

LINE webhook 只負責把核准事件送到 GitHub Actions。真正的審核、建發布包與 Threads 發文仍由：

```text
.github/workflows/review-inspiration-post.yml
```

執行。

另外請在 GitHub repo 設定：

- Repository secrets

```text
THREADS_USER_ID
THREADS_ACCESS_TOKEN
THREADS_APP_SECRET
```

- Repository variable

```text
PUBLIC_ASSET_BASE_URL=https://你的-cloudflare-domain
```

例如：

```text
PUBLIC_ASSET_BASE_URL=https://ai-knowledge-chunchun.pages.dev
```

如果你有綁自訂網域，請填自訂網域。

## 驗證方式

部署完成後，可做 3 個檢查：

1. 圖片能否公開讀取

```text
https://你的-domain/assets/generated/2026-06-06/inspiration-2026-06-06.png
```

2. webhook 未帶 LINE 簽章時是否回 `401`

```text
POST /api/line-review
```

3. LINE 核准後，是否成功觸發：

```text
Review Inspiration Post
Publish Inspiration Post
```

## 官方參考

- Cloudflare Pages Functions Get Started：
  https://developers.cloudflare.com/pages/functions/get-started/
- Cloudflare 靜態網站部署：
  https://developers.cloudflare.com/pages/framework-guides/deploy-a-static-html-site/
