一个基于 FastAPI + SQLite + React 的匿名聊天网站。
- 用户名 + 密码注册/登录(Session Cookie)
- 左侧建议列表与建议提交(提交后通过 SMTP 邮件通知站长)
- 右侧公共聊天室(历史消息 + WebSocket 实时广播)
conda activate base
uv sync
uv run uvicorn app.main:app --reload --host 0.0.0.0 --port 8000cd frontend
npm install
npm run dev默认前端地址:http://localhost:5173
默认后端地址:http://localhost:8000
- 复制环境变量模板:
cp .env.example .env- 编辑
.env,至少修改:SESSION_SECRETCORS_ORIGINS(设置为你的线上域名,例如https://talk.example.com)SESSION_HTTPS_ONLY=true(线上 HTTPS 建议开启)
docker compose up -d --build如果你不想把邮箱密码写入 .env,可在当前终端临时注入:
read -s -p "SMTP_PASS: " SMTP_PASS
echo
export SMTP_PASS
docker compose up -d --build
unset SMTP_PASS服务说明:
web:对外提供前端静态资源,同时转发/api和/ws到后端容器backend:FastAPI 服务,使用 SQLite 数据库文件- 数据库持久化目录:
./data - 宿主机监听:
127.0.0.1:18080
curl http://127.0.0.1:18080/api/health返回 {"status":"ok"} 即正常。
在宝塔中新建站点并绑定你的域名后:
- 申请并启用 SSL(建议强制 HTTPS)
- 配置反向代理目标为:
http://127.0.0.1:18080
- 开启 WebSocket 支持
这样外部请求路径保持不变:
https://你的域名/-> 前端页面https://你的域名/api/*-> 后端 APIwss://你的域名/ws/chat-> 聊天 WebSocket
如果你不使用宝塔,可以直接在服务器 Nginx 做反向代理,项目容器保持不变:
- 先启动容器(保持本机
127.0.0.1:18080监听):
docker compose up -d --build- 复制并启用 Nginx 站点配置:
sudo cp deploy/nginx.site.conf /etc/nginx/conf.d/anonymous-talk.conf-
按实际情况修改配置中的这些值:
server_name talk.example.com-> 你的域名ssl_certificate/ssl_certificate_key-> 你的证书路径
-
检查并重载 Nginx:
sudo nginx -t
sudo systemctl reload nginx仓库已提供示例文件:deploy/nginx.site.conf。
该配置会将以下请求转发到容器入口 127.0.0.1:18080(即 web 服务):
https://你的域名/-> 前端页面https://你的域名/api/*-> 后端 APIwss://你的域名/ws/chat-> 聊天 WebSocket
如果你暂时没有证书,可先使用下面最小配置(不建议长期线上使用):
server {
listen 80;
server_name talk.example.com;
location / {
proxy_pass http://127.0.0.1:18080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location /ws/ {
proxy_pass http://127.0.0.1:18080;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_read_timeout 600s;
}
}复制 .env.example 为 .env 并按需修改:
SESSION_SECRET: 会话签名密钥(必须修改)SESSION_HTTPS_ONLY: 是否仅通过 HTTPS 发送会话 Cookie(线上建议true)DATABASE_URL: SQLite 数据库地址(默认本地文件)CORS_ORIGINS: 允许前端来源(默认http://localhost:5173)SMTP_*,ALERT_TO_EMAIL: 建议邮件发送配置
如果未配置 SMTP,建议仍会写入数据库,但不会发送邮件。
- 认证:
POST /api/auth/registerPOST /api/auth/loginPOST /api/auth/logoutGET /api/auth/me
- 建议:
GET /api/suggestionsPOST /api/suggestions
- 聊天:
GET /api/chat/historyWS /ws/chat