Go + PostgreSQL + Valkey backend (cmd/internal/migrations), uni-app mobile frontend, PC web (web/), admin console (admin/), Android WebView container (android_project/). Build artifacts, node_modules, local env and debug screenshots excluded.
603 lines
23 KiB
Go
603 lines
23 KiB
Go
package api
|
||
|
||
import (
|
||
"log/slog"
|
||
"net/http"
|
||
"os"
|
||
|
||
"github.com/go-chi/chi/v5"
|
||
"github.com/jackc/pgx/v5/pgxpool"
|
||
|
||
"github.com/yourorg/article-platform/internal/platform/cache"
|
||
"github.com/yourorg/article-platform/internal/platform/config"
|
||
"github.com/yourorg/article-platform/internal/platform/httpx"
|
||
"github.com/yourorg/article-platform/internal/platform/metrics"
|
||
"github.com/yourorg/article-platform/internal/platform/middleware"
|
||
"github.com/yourorg/article-platform/internal/platform/storage"
|
||
"github.com/yourorg/article-platform/internal/repo"
|
||
)
|
||
|
||
// Deps 是所有处理器的依赖集合。显式列出,不用 DI 容器。
|
||
type Deps struct {
|
||
Cfg *config.Config
|
||
Log *slog.Logger
|
||
Pool *pgxpool.Pool
|
||
Cache *cache.Cache
|
||
Auth *middleware.Auth
|
||
RateLimit *middleware.RateLimiter
|
||
|
||
Metrics *metrics.Registry
|
||
|
||
Articles *repo.ArticleRepo
|
||
Users *repo.UserRepo
|
||
Comments *repo.CommentRepo
|
||
Notifs *repo.NotificationRepo
|
||
Interacts *repo.InteractionRepo
|
||
Taxonomy *repo.TaxonomyRepo
|
||
Stats *repo.StatsRepo
|
||
Reports *repo.ReportRepo
|
||
Outbox *repo.OutboxRepo
|
||
Media *repo.MediaRepo
|
||
Presigner *storage.Presigner
|
||
Topics *repo.TopicRepo
|
||
Templates *repo.TemplateRepo
|
||
Settings *repo.SettingsRepo
|
||
}
|
||
|
||
// Mount 挂载全部路由。所有路径统一 /api/v1 前缀,不再保留双份别名。
|
||
func Mount(r chi.Router, d *Deps) {
|
||
r.Get("/healthz", healthz(d))
|
||
r.Get("/readyz", readyz(d))
|
||
|
||
// 静态文件服务:/uploads/*
|
||
_ = os.MkdirAll("./uploads", 0755)
|
||
r.Handle("/uploads/*", http.StripPrefix("/uploads/", http.FileServer(http.Dir("./uploads"))))
|
||
r.Handle("/uploads", http.RedirectHandler("/uploads/", http.StatusMovedPermanently))
|
||
|
||
// 兼容老路径 /api/assets/upload 与 /api/quota
|
||
r.Route("/api", func(r chi.Router) {
|
||
r.Post("/assets/upload", d.uploadDirectAsset)
|
||
r.Get("/quota", func(w http.ResponseWriter, r *http.Request) {
|
||
httpx.WriteJSON(w, http.StatusOK, map[string]any{
|
||
"limits": map[string]any{
|
||
"max_sections_count": 400,
|
||
"max_images_count": 250,
|
||
"max_videos_count": 100,
|
||
"max_video_duration_seconds": 14400,
|
||
"max_video_file_size_mb": 5120,
|
||
"max_audios_count": 100,
|
||
"max_votes_count": 50,
|
||
"max_text_characters_per_section": 20000,
|
||
"max_heading_characters": 200,
|
||
},
|
||
})
|
||
})
|
||
})
|
||
|
||
r.Route("/api/v1", func(r chi.Router) {
|
||
r.Get("/quota", func(w http.ResponseWriter, r *http.Request) {
|
||
httpx.WriteJSON(w, http.StatusOK, map[string]any{
|
||
"limits": map[string]any{
|
||
"max_sections_count": 400,
|
||
"max_images_count": 250,
|
||
"max_videos_count": 100,
|
||
"max_video_duration_seconds": 14400,
|
||
"max_video_file_size_mb": 5120,
|
||
"max_audios_count": 100,
|
||
"max_votes_count": 50,
|
||
"max_text_characters_per_section": 20000,
|
||
"max_heading_characters": 200,
|
||
},
|
||
})
|
||
})
|
||
r.Get("/music", func(w http.ResponseWriter, r *http.Request) {
|
||
list := []map[string]any{
|
||
{
|
||
"id": "music_sweet_love",
|
||
"name": "愉悦自由氛围 - Sweet Love",
|
||
"artist": "简篇精选",
|
||
"url": "https://epmd-volc.jianpian.info/biz/3be42d5a67954ecb99edfa10470753a1.mp3",
|
||
"cover_url": "https://images.unsplash.com/photo-1511671782779-c97d3d27a1d4?auto=format&fit=crop&w=120&q=80",
|
||
"category": "清新舒缓",
|
||
"duration": 184,
|
||
},
|
||
{
|
||
"id": "music_serene_view",
|
||
"name": "晨光微熹 - 纯净钢琴",
|
||
"artist": "Mixkit Originals",
|
||
"url": "https://assets.mixkit.co/music/preview/mixkit-serene-view-443.mp3",
|
||
"cover_url": "https://images.unsplash.com/photo-1520523839898-507121c27863?auto=format&fit=crop&w=120&q=80",
|
||
"category": "钢琴治愈",
|
||
"duration": 148,
|
||
},
|
||
{
|
||
"id": "music_valley_sunset",
|
||
"name": "林间漫步 - 悠扬吉他",
|
||
"artist": "Acoustic Melody",
|
||
"url": "https://assets.mixkit.co/music/preview/mixkit-valley-sunset-127.mp3",
|
||
"cover_url": "https://images.unsplash.com/photo-1510915361894-db8b60106cb1?auto=format&fit=crop&w=120&q=80",
|
||
"category": "民谣吉他",
|
||
"duration": 120,
|
||
},
|
||
{
|
||
"id": "music_ambient_dream",
|
||
"name": "深海微光 - 空灵氛围",
|
||
"artist": "Ambient Sound",
|
||
"url": "https://assets.mixkit.co/music/preview/mixkit-ambient-dream-143.mp3",
|
||
"cover_url": "https://images.unsplash.com/photo-1507525428034-b723cf961d3e?auto=format&fit=crop&w=120&q=80",
|
||
"category": "空灵冥想",
|
||
"duration": 165,
|
||
},
|
||
{
|
||
"id": "music_hazy_afternoon",
|
||
"name": "午后书房 - 治愈轻乐",
|
||
"artist": "Lo-Fi Beats",
|
||
"url": "https://assets.mixkit.co/music/preview/mixkit-hazy-afternoon-119.mp3",
|
||
"cover_url": "https://images.unsplash.com/photo-1501339847302-ac426a4a7cbb?auto=format&fit=crop&w=120&q=80",
|
||
"category": "轻音乐",
|
||
"duration": 156,
|
||
},
|
||
}
|
||
httpx.WriteJSON(w, http.StatusOK, list)
|
||
})
|
||
r.Get("/backdrops", func(w http.ResponseWriter, r *http.Request) {
|
||
themes := []map[string]any{
|
||
{
|
||
"id": "default",
|
||
"name": "极简纯白",
|
||
"bg_color": "#ffffff",
|
||
"card_bg": "#ffffff",
|
||
"text_color": "#2b2b2b",
|
||
"preview_color": "#ffffff",
|
||
"border_color": "#f1f5f9",
|
||
},
|
||
{
|
||
"id": "parchment",
|
||
"name": "典雅羊皮纸",
|
||
"bg_color": "#f7f3e9",
|
||
"card_bg": "rgba(255, 255, 255, 0.88)",
|
||
"text_color": "#382e25",
|
||
"preview_color": "#f7f3e9",
|
||
"border_color": "#e8dfce",
|
||
"bg_pattern": "repeating-linear-gradient(45deg, #f7f3e9 0, #f7f3e9 10px, #f4eee2 10px, #f4eee2 20px)",
|
||
},
|
||
{
|
||
"id": "ricepaper",
|
||
"name": "古风宣纸",
|
||
"bg_color": "#fcfaf2",
|
||
"card_bg": "rgba(255, 255, 255, 0.92)",
|
||
"text_color": "#2c241e",
|
||
"preview_color": "#fcfaf2",
|
||
"border_color": "#d6cbbd",
|
||
"header_decor": "https://epss-volc.jianpian.info/biz/f7ffbfdfca2b4d649a27f6a7e652ffa3.gif~tplv-3gcisvpo39-s3-cC-q:0:0:0:0:q0.gif",
|
||
},
|
||
{
|
||
"id": "mint",
|
||
"name": "清新薄荷",
|
||
"bg_color": "#f0fdf4",
|
||
"card_bg": "rgba(255, 255, 255, 0.9)",
|
||
"text_color": "#14532d",
|
||
"preview_color": "#dcfce7",
|
||
"border_color": "#bbf7d0",
|
||
},
|
||
{
|
||
"id": "warm_autumn",
|
||
"name": "暖阳麦浪",
|
||
"bg_color": "#fffbeb",
|
||
"card_bg": "rgba(255, 255, 255, 0.9)",
|
||
"text_color": "#451a03",
|
||
"preview_color": "#fef3c7",
|
||
"border_color": "#fde68a",
|
||
},
|
||
{
|
||
"id": "night",
|
||
"name": "静谧星夜",
|
||
"bg_color": "#0f172a",
|
||
"card_bg": "rgba(30, 41, 59, 0.92)",
|
||
"text_color": "#f8fafc",
|
||
"preview_color": "#1e293b",
|
||
"border_color": "#334155",
|
||
"is_dark": true,
|
||
},
|
||
{
|
||
"id": "army_red",
|
||
"name": "军旅红",
|
||
"bg_color": "#C11C1F",
|
||
"card_bg": "rgba(122, 17, 20, 0.92)",
|
||
"text_color": "#FFF5F5",
|
||
"preview_color": "#C11C1F",
|
||
"border_color": "#D93A3D",
|
||
"is_dark": true,
|
||
},
|
||
{
|
||
"id": "army_gold",
|
||
"name": "八一金",
|
||
"bg_color": "#B07A28",
|
||
"card_bg": "rgba(140, 94, 30, 0.92)",
|
||
"text_color": "#FFFAEB",
|
||
"preview_color": "#B07A28",
|
||
"border_color": "#C9963F",
|
||
"is_dark": true,
|
||
},
|
||
{
|
||
"id": "army_green",
|
||
"name": "军绿",
|
||
"bg_color": "#4A5E3A",
|
||
"card_bg": "rgba(58, 74, 46, 0.92)",
|
||
"text_color": "#F0F5EB",
|
||
"preview_color": "#4A5E3A",
|
||
"border_color": "#5C7448",
|
||
"is_dark": true,
|
||
},
|
||
{
|
||
"id": "steel_blue",
|
||
"name": "钢蓝",
|
||
"bg_color": "#56687A",
|
||
"card_bg": "rgba(67, 81, 96, 0.92)",
|
||
"text_color": "#F0F4F8",
|
||
"preview_color": "#56687A",
|
||
"border_color": "#6A7E92",
|
||
"is_dark": true,
|
||
},
|
||
{
|
||
"id": "temp1",
|
||
"name": "军歌嘹亮",
|
||
"bg_color": "#FAF7F0",
|
||
"card_bg": "rgba(255, 255, 255, 0.9)",
|
||
"text_color": "#8A6A1F",
|
||
"preview_color": "#FAF7F0",
|
||
"border_color": "#E5D9B8",
|
||
"bg_image": "/static/themes/temp1.jpg",
|
||
},
|
||
{
|
||
"id": "temp2",
|
||
"name": "学雷锋",
|
||
"bg_color": "#A81818",
|
||
"card_bg": "rgba(60, 10, 10, 0.92)",
|
||
"text_color": "#FFE9C4",
|
||
"preview_color": "#A81818",
|
||
"border_color": "#C23232",
|
||
"bg_image": "/static/themes/temp2.jpg",
|
||
"is_dark": true,
|
||
},
|
||
{
|
||
"id": "temp3",
|
||
"name": "党建红绸",
|
||
"bg_color": "#C4171D",
|
||
"card_bg": "rgba(90, 12, 14, 0.92)",
|
||
"text_color": "#FFD970",
|
||
"preview_color": "#C4171D",
|
||
"border_color": "#E0453B",
|
||
"bg_image": "/static/themes/temp3.jpg",
|
||
"is_dark": true,
|
||
},
|
||
{
|
||
"id": "temp4",
|
||
"name": "红金庆典",
|
||
"bg_color": "#C0272D",
|
||
"card_bg": "rgba(122, 22, 24, 0.92)",
|
||
"text_color": "#FFF3D6",
|
||
"preview_color": "#C0272D",
|
||
"border_color": "#E8A33D",
|
||
"bg_image": "/static/themes/temp4.jpg",
|
||
"is_dark": true,
|
||
},
|
||
{
|
||
"id": "temp5",
|
||
"name": "水墨初心",
|
||
"bg_color": "#CC2222",
|
||
"card_bg": "rgba(96, 14, 14, 0.9)",
|
||
"text_color": "#FFF6EC",
|
||
"preview_color": "#CC2222",
|
||
"border_color": "#B91C1C",
|
||
"bg_image": "/static/themes/temp5.jpg",
|
||
"is_dark": true,
|
||
},
|
||
{
|
||
"id": "temp6",
|
||
"name": "致敬军人",
|
||
"bg_color": "#F5EFE8",
|
||
"card_bg": "rgba(255, 255, 255, 0.9)",
|
||
"text_color": "#8A4A2A",
|
||
"preview_color": "#F5EFE8",
|
||
"border_color": "#E3D5C5",
|
||
"bg_image": "/static/themes/temp6.jpg",
|
||
},
|
||
{
|
||
"id": "temp7",
|
||
"name": "不忘初心",
|
||
"bg_color": "#B01F24",
|
||
"card_bg": "rgba(70, 16, 18, 0.92)",
|
||
"text_color": "#FFE9C4",
|
||
"preview_color": "#B01F24",
|
||
"border_color": "#D8B26A",
|
||
"bg_image": "/static/themes/temp7.jpg",
|
||
"is_dark": true,
|
||
},
|
||
{
|
||
"id": "temp8",
|
||
"name": "暖金岁月",
|
||
"bg_color": "#F0E4D0",
|
||
"card_bg": "rgba(255, 252, 246, 0.92)",
|
||
"text_color": "#A0522D",
|
||
"preview_color": "#F0E4D0",
|
||
"border_color": "#D9C9A8",
|
||
"bg_image": "/static/themes/temp8.jpg",
|
||
},
|
||
{
|
||
"id": "temp9",
|
||
"name": "星空夜航",
|
||
"bg_color": "#2B3A8C",
|
||
"card_bg": "rgba(23, 30, 72, 0.92)",
|
||
"text_color": "#F0F4FF",
|
||
"preview_color": "#2B3A8C",
|
||
"border_color": "#4A5AC0",
|
||
"bg_image": "/static/themes/temp9.jpg",
|
||
"is_dark": true,
|
||
},
|
||
{
|
||
"id": "temp10",
|
||
"name": "鎏金荣光",
|
||
"bg_color": "#E8A81E",
|
||
"card_bg": "rgba(255, 248, 230, 0.9)",
|
||
"text_color": "#6B3A0A",
|
||
"preview_color": "#E8A81E",
|
||
"border_color": "#C9920F",
|
||
"bg_image": "/static/themes/temp10.jpg",
|
||
},
|
||
}
|
||
httpx.WriteJSON(w, http.StatusOK, themes)
|
||
})
|
||
r.Get("/fonts", func(w http.ResponseWriter, r *http.Request) {
|
||
httpx.WriteJSON(w, http.StatusOK, []any{})
|
||
})
|
||
|
||
// 资产与直传上传(图片/视频/音频多端通用)
|
||
r.Post("/assets/upload", d.uploadDirectAsset)
|
||
r.Post("/media/upload", d.uploadDirectAsset)
|
||
|
||
// 认证
|
||
r.Route("/auth", func(r chi.Router) {
|
||
r.Post("/register", d.register)
|
||
r.Post("/login", d.login)
|
||
r.Post("/logout", middleware.RequireAuth(d.logout))
|
||
r.Get("/me", middleware.RequireAuth(d.me))
|
||
})
|
||
|
||
// 文章
|
||
r.Route("/articles", func(r chi.Router) {
|
||
r.Get("/", d.listArticles)
|
||
r.Post("/", middleware.Perm("article:create", d.createArticle))
|
||
r.Get("/{maskID}", d.getArticle)
|
||
r.Put("/{maskID}", middleware.Perm("article:edit_own", d.updateArticle))
|
||
r.Patch("/{maskID}", middleware.Perm("article:edit_own", d.updateArticle))
|
||
r.Delete("/{maskID}", middleware.Perm("article:delete_own", d.deleteArticle))
|
||
r.Post("/{maskID}/publish", middleware.Perm("article:publish", d.publishArticle))
|
||
r.Post("/{maskID}/view", d.recordArticleView)
|
||
|
||
// 区块
|
||
r.Get("/{maskID}/sections", d.listSections)
|
||
r.Post("/{maskID}/sections", middleware.Perm("article:edit_own", d.upsertSection))
|
||
r.Put("/{maskID}/sections/{sectionID}", middleware.Perm("article:edit_own", d.upsertSection))
|
||
r.Delete("/{maskID}/sections/{sectionID}", middleware.Perm("article:edit_own", d.deleteSection))
|
||
r.Post("/{maskID}/sections/reorder", middleware.Perm("article:edit_own", d.reorderSections))
|
||
|
||
// 草稿快照与版本回滚
|
||
r.Get("/{maskID}/drafts", middleware.Perm("article:edit_own", d.listDrafts))
|
||
r.Post("/{maskID}/drafts", middleware.Perm("article:edit_own", d.saveDraft))
|
||
r.Get("/{maskID}/drafts/latest", middleware.Perm("article:edit_own", d.getLatestDraft))
|
||
r.Post("/{maskID}/drafts/{draftID}/rollback", middleware.Perm("article:edit_own", d.rollbackDraft))
|
||
|
||
// 回收站
|
||
r.Get("/trash", middleware.RequireAuth(d.listTrashArticles))
|
||
r.Post("/{maskID}/restore", middleware.RequireAuth(d.userRestoreArticle))
|
||
r.Delete("/{maskID}/trash", middleware.RequireAuth(d.userPermanentlyDelete))
|
||
|
||
// 互动
|
||
r.Post("/{maskID}/like", middleware.RequireAuth(d.likeArticle))
|
||
r.Delete("/{maskID}/like", middleware.RequireAuth(d.unlikeArticle))
|
||
r.Post("/{maskID}/favorite", middleware.RequireAuth(d.favoriteArticle))
|
||
r.Delete("/{maskID}/favorite", middleware.RequireAuth(d.unfavoriteArticle))
|
||
|
||
// 评论
|
||
r.Get("/{maskID}/comments", d.listComments)
|
||
r.Post("/{maskID}/comments", middleware.RequireAuth(d.createComment))
|
||
})
|
||
|
||
// 评论管理(独立于文章路径)
|
||
r.Route("/comments", func(r chi.Router) {
|
||
r.Get("/{commentID}/replies", d.listReplies)
|
||
r.Delete("/{commentID}", middleware.RequireAuth(d.deleteComment))
|
||
r.Patch("/{commentID}/status", middleware.Perm("comment:manage_all", d.setCommentStatus))
|
||
})
|
||
|
||
// 审核
|
||
r.Route("/review", func(r chi.Router) {
|
||
r.Get("/pending", middleware.Perm("article:review", d.pendingReview))
|
||
r.Post("/{maskID}/approve", middleware.Perm("article:review", d.approveArticle))
|
||
r.Post("/{maskID}/reject", middleware.Perm("article:review", d.rejectArticle))
|
||
})
|
||
|
||
// 分类与标签
|
||
r.Get("/categories/tree", d.categoryTree)
|
||
r.Get("/tags/hot", d.hotTags)
|
||
r.Post("/tags", middleware.Perm("tag:manage", d.upsertTag))
|
||
r.Post("/categories", middleware.Perm("category:manage", d.upsertCategory))
|
||
|
||
// 互动关系
|
||
r.Post("/users/{userID}/follow", middleware.Perm("user:follow", d.follow))
|
||
r.Delete("/users/{userID}/follow", middleware.RequireAuth(d.unfollow))
|
||
|
||
// 通知
|
||
r.Route("/notifications", func(r chi.Router) {
|
||
r.Get("/", middleware.RequireAuth(d.listNotifications))
|
||
r.Get("/unread-count", middleware.RequireAuth(d.unreadCount))
|
||
r.Patch("/read", middleware.RequireAuth(d.markRead))
|
||
})
|
||
|
||
// 举报
|
||
r.Post("/reports", middleware.Perm("report:create", d.createReport))
|
||
r.Get("/admin/reports", middleware.Perm("report:read", d.listReports))
|
||
r.Patch("/admin/reports/{reportID}", middleware.Perm("report:handle", d.handleReport))
|
||
|
||
// 作者中心
|
||
r.Route("/author", func(r chi.Router) {
|
||
r.Get("/articles", middleware.RequireAuth(d.myArticles))
|
||
r.Get("/articles/{maskID}/stats", middleware.RequireAuth(d.articleStatsDetail))
|
||
r.Get("/stats/overview", middleware.RequireAuth(d.authorStats))
|
||
r.Get("/followers", middleware.RequireAuth(d.myFollowers))
|
||
})
|
||
|
||
// 个人中心六大列表及资料设置
|
||
r.Route("/users/me", func(r chi.Router) {
|
||
r.Get("/", middleware.RequireAuth(d.me))
|
||
r.Put("/", middleware.RequireAuth(d.updateProfile))
|
||
r.Put("/profile", middleware.RequireAuth(d.updateProfile))
|
||
r.Put("/password", middleware.RequireAuth(d.updatePassword))
|
||
r.Delete("/", middleware.RequireAuth(d.deleteAccount))
|
||
r.Get("/favorites", middleware.RequireAuth(d.myFavorites))
|
||
r.Get("/likes", middleware.RequireAuth(d.myLikes))
|
||
r.Get("/followings", middleware.RequireAuth(d.myFollowings))
|
||
r.Get("/following", middleware.RequireAuth(d.myFollowings))
|
||
r.Get("/followers", middleware.RequireAuth(d.myFollowersList))
|
||
r.Get("/topics", middleware.RequireAuth(d.myFollowedTopics))
|
||
r.Get("/history", middleware.RequireAuth(d.myHistory))
|
||
r.Delete("/history/{articleID}", middleware.RequireAuth(d.deleteHistoryItem))
|
||
r.Delete("/history", middleware.RequireAuth(d.clearHistory))
|
||
})
|
||
|
||
// 用户
|
||
r.Get("/users/{userID}", d.getUserProfile)
|
||
r.Get("/users/{userID}/articles", d.getUserArticles)
|
||
r.Get("/users/me/quota", middleware.RequireAuth(d.myQuota))
|
||
|
||
// 话题
|
||
r.Route("/topics", func(r chi.Router) {
|
||
r.Get("/", d.listTopics)
|
||
r.Get("/mine/followed", middleware.RequireAuth(d.myFollowedTopics))
|
||
r.Get("/{topicID}", d.getTopic)
|
||
r.Get("/{topicID}/articles", d.topicArticles)
|
||
r.Post("/{topicID}/follow", middleware.RequireAuth(d.followTopic))
|
||
r.Delete("/{topicID}/follow", middleware.RequireAuth(d.unfollowTopic))
|
||
})
|
||
|
||
// 模板
|
||
r.Route("/templates", func(r chi.Router) {
|
||
r.Get("/", d.listTemplates)
|
||
r.Get("/{templateID}", d.getTemplate)
|
||
r.Post("/{templateID}/apply", middleware.Perm("article:create", d.applyTemplate))
|
||
r.Post("/", middleware.Perm("template:manage", d.createTemplate))
|
||
r.Put("/{templateID}", middleware.Perm("template:manage", d.updateTemplate))
|
||
r.Delete("/{templateID}", middleware.Perm("template:manage", d.deleteTemplate))
|
||
})
|
||
|
||
// 媒体:客户端直传对象存储,服务端只签发凭证
|
||
r.Route("/media", func(r chi.Router) {
|
||
r.Post("/presign", middleware.RequireAuth(d.presign))
|
||
r.Post("/confirm", middleware.RequireAuth(d.confirmMedia))
|
||
r.Get("/{assetID}", middleware.RequireAuth(d.getMedia))
|
||
})
|
||
|
||
// 好友圈动态流(朋友圈式,关注的人 ∪ 自己)
|
||
r.Get("/feed/following", middleware.RequireAuth(d.feedFollowing))
|
||
|
||
// 搜索
|
||
// /search/articles 与 /search/suggestions 是 H5 客户端使用的路径,
|
||
// 复用同一 handler 保持前后端契约一致
|
||
r.Get("/search", d.search)
|
||
r.Get("/search/articles", d.search)
|
||
r.Get("/search/suggestions", d.searchSuggestions)
|
||
r.Get("/search/hot-words", d.hotWords)
|
||
// 美篇式实体搜索:全部聚合 + 用户 / 圈子单 Tab
|
||
r.Get("/search/all", d.searchAll)
|
||
r.Get("/search/users", d.searchUsers)
|
||
r.Get("/search/circles", d.searchCircles)
|
||
|
||
// 分享导出
|
||
r.Get("/share/{maskID}", d.shareArticle)
|
||
r.Get("/share/{maskID}/html", d.exportHTML)
|
||
|
||
// 管理后台
|
||
r.Route("/admin", func(r chi.Router) {
|
||
r.Get("/analytics/overview", middleware.Perm("stats:read_all", d.adminOverview))
|
||
r.Get("/analytics/trends", middleware.Perm("stats:read_all", d.adminTrends))
|
||
|
||
// 文章管理(page 分页 + total,供 admin 前台表格)
|
||
r.Get("/articles", middleware.Perm("article:manage_all", d.adminArticlesPage))
|
||
r.Put("/articles/{maskID}/offline", middleware.Perm("article:manage_all", d.adminOfflineArticle))
|
||
r.Delete("/articles/{maskID}", middleware.Perm("article:manage_all", d.adminDeleteArticle))
|
||
|
||
// 内容审核
|
||
r.Get("/audit/articles", middleware.Perm("article:review", d.adminAuditArticles))
|
||
r.Post("/audit/articles/batch", middleware.Perm("article:review", d.adminAuditBatch))
|
||
r.Post("/audit/articles/{articleID}/approve", middleware.Perm("article:review", d.adminAuditApprove))
|
||
r.Post("/audit/articles/{articleID}/reject", middleware.Perm("article:review", d.adminAuditReject))
|
||
|
||
// 回收站
|
||
r.Get("/recycle/articles", middleware.Perm("article:manage_all", d.adminRecycleList))
|
||
r.Post("/recycle/articles/{articleID}/restore", middleware.Perm("article:manage_all", d.adminRecycleRestore))
|
||
r.Delete("/recycle/articles/{articleID}", middleware.Perm("article:manage_all", d.adminRecycleDelete))
|
||
|
||
// 用户管理
|
||
r.Get("/users", middleware.Perm("user:manage", d.adminListUsers))
|
||
r.Patch("/users/{userID}/status", middleware.Perm("user:manage", d.adminSetUserStatus))
|
||
r.Put("/users/{userID}/role", middleware.Perm("user:manage", d.adminSetUserRole))
|
||
|
||
// 评论管理
|
||
r.Get("/comments", middleware.Perm("comment:manage_all", d.adminListComments))
|
||
r.Put("/comments/{commentID}/hide", middleware.Perm("comment:manage_all", d.adminHideComment))
|
||
r.Delete("/comments/{commentID}", middleware.Perm("comment:manage_all", d.adminDeleteComment))
|
||
|
||
// 举报处置(PUT 契约与 PATCH /admin/reports/{id} 并存)
|
||
r.Put("/reports/{reportID}", middleware.Perm("report:handle", d.adminHandleReportPUT))
|
||
|
||
// 标签 / 分类
|
||
r.Get("/tags", middleware.Perm("tag:read", d.adminListTagsPaged))
|
||
r.Post("/tags", middleware.Perm("tag:manage", d.adminTagAction))
|
||
r.Post("/categories", middleware.Perm("category:manage", d.adminCreateCategory))
|
||
r.Put("/categories/{categoryID}", middleware.Perm("category:manage", d.adminUpdateCategory))
|
||
r.Delete("/categories/{categoryID}", middleware.Perm("category:manage", d.adminDeleteCategory))
|
||
|
||
// 系统设置
|
||
r.Get("/settings", middleware.Perm("user:manage", d.adminGetSettings))
|
||
r.Put("/settings", middleware.Perm("user:manage", d.adminPutSettings))
|
||
|
||
// 模板管理
|
||
r.Get("/templates", middleware.Perm("template:manage", d.listTemplates))
|
||
r.Post("/templates", middleware.Perm("template:manage", d.createTemplate))
|
||
r.Put("/templates/{templateID}", middleware.Perm("template:manage", d.updateTemplate))
|
||
r.Delete("/templates/{templateID}", middleware.Perm("template:manage", d.deleteTemplate))
|
||
|
||
r.Route("/topics", func(r chi.Router) {
|
||
r.Get("/", middleware.Perm("topic:manage", d.adminListTopics))
|
||
r.Post("/", middleware.Perm("topic:manage", d.adminCreateTopic))
|
||
r.Put("/{topicID}", middleware.Perm("topic:manage", d.adminUpdateTopic))
|
||
r.Patch("/{topicID}/status", middleware.Perm("topic:manage", d.adminSetTopicStatus))
|
||
r.Delete("/{topicID}", middleware.Perm("topic:manage", d.adminDeleteTopic))
|
||
})
|
||
})
|
||
})
|
||
}
|
||
|
||
func healthz(d *Deps) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
w.Header().Set("Content-Type", "application/json")
|
||
w.Write([]byte(`{"status":"ok"}`))
|
||
}
|
||
}
|
||
|
||
// readyz 检查依赖是否可用。K8s readinessProbe 用这个,
|
||
// 与 liveness 分开:数据库挂了不应重启进程,但应停止接流量。
|
||
func readyz(d *Deps) http.HandlerFunc {
|
||
return func(w http.ResponseWriter, r *http.Request) {
|
||
ctx := r.Context()
|
||
if err := d.Pool.Ping(ctx); err != nil {
|
||
w.WriteHeader(http.StatusServiceUnavailable)
|
||
w.Write([]byte(`{"status":"pg_unavailable"}`))
|
||
return
|
||
}
|
||
w.Header().Set("Content-Type", "application/json")
|
||
w.Write([]byte(`{"status":"ready"}`))
|
||
}
|
||
}
|