cloudflare-chat/README.md

66 lines
1.8 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Cloudflare 在线聊天室D1 + KV + R2
`readme.txt` 需求实现的最小可用在线聊天室D1 存用户/房间/消息KV 做会话与房间访问令牌缓存R2 存图片。
## 功能概览
- 房间模式:开放/私密
- 当前版本只有一个房间:大厅(可选设置密码)
- 开放:所有人可浏览;必须注册登录才能发言
- 私密:需要密码进入;可选“允许游客匿名发言”
- 用户等级:匿名游客 / 注册会员 / 认证会员 / 管理员
- 注册会员:可发文字/表情;不可上传图片
- 认证会员可上传图片R2
- 管理员:可编辑用户资料与等级、封禁用户、删除消息
- 页面:注册、登录、找回密码、房间列表、聊天窗口(左右气泡)
- 实时SSEEventSource推送简化实现适合 MVP
## 本地开发
1) 安装依赖
```bash
npm i
```
2) 创建并迁移 D1
```bash
npx wrangler d1 create chat_db
# 把输出的 database_id 填到 wrangler.toml
npx wrangler d1 execute chat_db --file=./schema.sql
```
3) 创建 KV / R2并填入 `wrangler.toml`
```bash
npx wrangler kv namespace create CACHE
npx wrangler r2 bucket create chat-bucket
```
4) 启动
```bash
npm run dev
```
## 部署
```bash
npm run deploy
```
## 管理员
生产/开发环境:第一个注册成功的用户会自动提升为管理员(后续用户为注册会员)。
开发环境也可用接口创建管理员(`wrangler.toml` 的 `DEV_MODE = "true"`
```bash
curl -X POST http://localhost:8787/api/admin/debug/create-admin ^
-H "content-type: application/json" ^
-d "{\"username\":\"admin\",\"password\":\"admin123\"}"
```
管理员可在页面右侧“管理 → 大厅密码”设置/取消大厅密码:设置后游客登录会要求输入该密码;取消后游客登录直接进入。