Skip to content

Latest commit

 

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

feed-lite

一个用于学习的精简 Go Feed 后端。 主链路:

Gin -> Handler -> Service -> Repository -> MySQL/SQLite

功能:

  • 注册、登录、bcrypt 密码哈希、JWT 鉴权
  • 视频发布和详情查询
  • 最新 Feed 复合游标分页、Redis ZSET 热门榜(MySQL 降级)
  • 点赞事务、联合唯一索引、点赞计数原子更新
  • 评论发布和复合游标分页查询
  • 关注/取关与拉模式关注流:查询关注作者后,以复合游标聚合其最新视频
  • Outbox + RabbitMQ:发布视频在同一事务中记录事件,由 Worker 异步写扩散到粉丝 Redis 时间线;Redis 时间线不足或不可用时回退拉模式
  • Redis 视频详情 Cache Aside、singleflight 缓存击穿保护(Redis 不可用时自动回源数据库)
  • Redis Lua 固定窗口限流:按用户和写接口限制发布、点赞、评论请求

写接口默认限流为每用户、每路由 6010 次;可通过 WRITE_RATE_LIMITRATE_LIMIT_WINDOW_SECONDS 调整。

  • cmd/seed 批量生成开发数据

先用 SQLite 跑通

需要 Go 1.26+:

cd $HOME\Desktop\feed-lite
go mod tidy
go run ./cmd/server

另开窗口生成数据:

cd $HOME\Desktop\feed-lite
go run ./cmd/seed

服务地址:http://localhost:8081。登录账号:demo_user_1 / password123

服务运行后,可以执行一次完整的登录、Feed、评论、点赞演示:

powershell -ExecutionPolicy Bypass -File .\scripts\demo.ps1

关键接口

POST /api/v1/auth/register   {"username":"alice","password":"123456"}
POST /api/v1/auth/login      {"username":"alice","password":"123456"}
POST /api/v1/videos           Bearer JWT + {title,play_url,cover_url}
GET  /api/v1/feed/latest?limit=10
GET  /api/v1/feed/latest?limit=10&cursor=<next_cursor>
GET  /api/v1/feed/popular?limit=10
GET  /api/v1/feed/following?limit=10             Bearer JWT
POST /api/v1/users/:id/follow                     Bearer JWT
DELETE /api/v1/users/:id/follow                   Bearer JWT
POST /api/v1/videos/:id/likes       Bearer JWT
DELETE /api/v1/videos/:id/likes     Bearer JWT
POST /api/v1/videos/:id/comments    Bearer JWT + {"content":"hello"}
GET  /api/v1/videos/:id/comments?limit=20
GET  /api/v1/videos/:id/comments?limit=20&cursor=<next_cursor>

Docker

docker compose up -d --build

Compose 使用 MySQL、Redis、RabbitMQ 和独立 Worker;本地学习先用 SQLite 更简单。

Compose 还会启动 RabbitMQ 和独立 Worker。RabbitMQ 管理台为 http://localhost:15672,账号密码均为 guest

学习顺序

  1. 先看 cmd/server/main.go,理解启动和依赖注入。
  2. internal/httpapi/router.go 进入注册、登录链路。
  3. 再看 video.go 的发布/缓存和 feed.go 的游标。
  4. 最后看 interaction.go 的事务、唯一索引和原子更新。
  5. 运行 cmd/seed,用 Postman 或 curl 验证接口。

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages