feat: 官网 + 管理端 + Gin/GORM 后端首个完整版本
- web/: 产品官网(Hero 事件流终端、功能矩阵、架构、快速开始、下载、博客 + Markdown 详情页),青瓷绿双主题 - admin/: 内容管理(JWT 登录、文章分页/搜索/CRUD、草稿与发布),embed 挂 /admin - server/: Gin + GORM,MySQL(DSN 走 .env,SQLite 兜底);规范落地:sundynix_ 表前缀、字符串雪花主键、snake_case 列名、统一响应信封、公共分页参数 - 双 SPA embed 单二进制部署,make build 一键出包 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"crypto/subtle"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"git.sundynix.cn/Blizzard/sundynix-site/server/internal/pkg/auth"
|
||||
"git.sundynix.cn/Blizzard/sundynix-site/server/internal/pkg/resp"
|
||||
)
|
||||
|
||||
// AdminAuthHandler 管理端登录。
|
||||
// 账号密码走环境变量 SUNDYNIX_ADMIN_USER / SUNDYNIX_ADMIN_PASS。
|
||||
type AdminAuthHandler struct {
|
||||
username string
|
||||
password string
|
||||
}
|
||||
|
||||
func NewAdminAuthHandler() *AdminAuthHandler {
|
||||
u := os.Getenv("SUNDYNIX_ADMIN_USER")
|
||||
p := os.Getenv("SUNDYNIX_ADMIN_PASS")
|
||||
if u == "" {
|
||||
u = "admin"
|
||||
}
|
||||
if p == "" {
|
||||
p = "admin123"
|
||||
log.Println("[WARN] SUNDYNIX_ADMIN_PASS 未设置,使用开发默认密码 admin123,生产环境务必配置")
|
||||
}
|
||||
return &AdminAuthHandler{username: u, password: p}
|
||||
}
|
||||
|
||||
type loginReq struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
// Login POST /api/admin/login
|
||||
func (h *AdminAuthHandler) Login(c *gin.Context) {
|
||||
var body loginReq
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
resp.BadRequest(c, "用户名和密码不能为空")
|
||||
return
|
||||
}
|
||||
userOK := subtle.ConstantTimeCompare([]byte(body.Username), []byte(h.username)) == 1
|
||||
passOK := subtle.ConstantTimeCompare([]byte(body.Password), []byte(h.password)) == 1
|
||||
if !userOK || !passOK {
|
||||
resp.Unauthorized(c, "用户名或密码错误")
|
||||
return
|
||||
}
|
||||
token, err := auth.Sign(body.Username)
|
||||
if err != nil {
|
||||
resp.ServerError(c, "签发 token 失败")
|
||||
return
|
||||
}
|
||||
resp.OK(c, gin.H{"token": token, "username": body.Username})
|
||||
}
|
||||
@@ -0,0 +1,158 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.sundynix.cn/Blizzard/sundynix-site/server/internal/model"
|
||||
"git.sundynix.cn/Blizzard/sundynix-site/server/internal/pkg/req"
|
||||
"git.sundynix.cn/Blizzard/sundynix-site/server/internal/pkg/resp"
|
||||
)
|
||||
|
||||
// AdminPostHandler 管理端文章 CRUD(含草稿)。
|
||||
type AdminPostHandler struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewAdminPostHandler(db *gorm.DB) *AdminPostHandler {
|
||||
return &AdminPostHandler{db: db}
|
||||
}
|
||||
|
||||
type postForm struct {
|
||||
Slug string `json:"slug" binding:"required"`
|
||||
Title string `json:"title" binding:"required"`
|
||||
Category string `json:"category"`
|
||||
Summary string `json:"summary"`
|
||||
Content string `json:"content"`
|
||||
Published bool `json:"published"`
|
||||
}
|
||||
|
||||
// List GET /api/admin/posts — 分页 + 关键词,含草稿
|
||||
func (h *AdminPostHandler) List(c *gin.Context) {
|
||||
var q req.PageQuery
|
||||
if err := c.ShouldBindQuery(&q); err != nil {
|
||||
resp.BadRequest(c, "分页参数不合法")
|
||||
return
|
||||
}
|
||||
q.Normalize()
|
||||
|
||||
tx := h.db.Model(&model.Post{})
|
||||
if q.Keyword != "" {
|
||||
kw := "%" + q.Keyword + "%"
|
||||
tx = tx.Where("title LIKE ? OR slug LIKE ? OR category LIKE ?", kw, kw, kw)
|
||||
}
|
||||
|
||||
var total int64
|
||||
if err := tx.Count(&total).Error; err != nil {
|
||||
resp.ServerError(c, "查询失败")
|
||||
return
|
||||
}
|
||||
var items []model.PostListItem
|
||||
err := tx.Order("created_at DESC").
|
||||
Offset(q.Offset()).Limit(q.PageSize).
|
||||
Find(&items).Error
|
||||
if err != nil {
|
||||
resp.ServerError(c, "查询失败")
|
||||
return
|
||||
}
|
||||
resp.Page(c, items, total, q.Page, q.PageSize)
|
||||
}
|
||||
|
||||
// Get GET /api/admin/posts/:id
|
||||
func (h *AdminPostHandler) Get(c *gin.Context) {
|
||||
var post model.Post
|
||||
err := h.db.First(&post, "id = ?", c.Param("id")).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
resp.NotFound(c, "文章不存在")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
resp.ServerError(c, "查询失败")
|
||||
return
|
||||
}
|
||||
resp.OK(c, post)
|
||||
}
|
||||
|
||||
// Create POST /api/admin/posts
|
||||
func (h *AdminPostHandler) Create(c *gin.Context) {
|
||||
var form postForm
|
||||
if err := c.ShouldBindJSON(&form); err != nil {
|
||||
resp.BadRequest(c, "slug 和标题不能为空")
|
||||
return
|
||||
}
|
||||
post := model.Post{
|
||||
Slug: form.Slug,
|
||||
Title: form.Title,
|
||||
Category: form.Category,
|
||||
Summary: form.Summary,
|
||||
Content: form.Content,
|
||||
}
|
||||
if form.Published {
|
||||
now := time.Now()
|
||||
post.PublishedAt = &now
|
||||
}
|
||||
if err := h.db.Create(&post).Error; err != nil {
|
||||
if errors.Is(err, gorm.ErrDuplicatedKey) {
|
||||
resp.BadRequest(c, "slug 已存在")
|
||||
return
|
||||
}
|
||||
resp.ServerError(c, "创建失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
resp.OK(c, post)
|
||||
}
|
||||
|
||||
// Update PUT /api/admin/posts/:id
|
||||
func (h *AdminPostHandler) Update(c *gin.Context) {
|
||||
var form postForm
|
||||
if err := c.ShouldBindJSON(&form); err != nil {
|
||||
resp.BadRequest(c, "slug 和标题不能为空")
|
||||
return
|
||||
}
|
||||
var post model.Post
|
||||
err := h.db.First(&post, "id = ?", c.Param("id")).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
resp.NotFound(c, "文章不存在")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
resp.ServerError(c, "查询失败")
|
||||
return
|
||||
}
|
||||
|
||||
post.Slug = form.Slug
|
||||
post.Title = form.Title
|
||||
post.Category = form.Category
|
||||
post.Summary = form.Summary
|
||||
post.Content = form.Content
|
||||
// 发布状态切换:首次发布记时间,撤回清空,重复发布保留原时间
|
||||
if form.Published && post.PublishedAt == nil {
|
||||
now := time.Now()
|
||||
post.PublishedAt = &now
|
||||
} else if !form.Published {
|
||||
post.PublishedAt = nil
|
||||
}
|
||||
|
||||
if err := h.db.Save(&post).Error; err != nil {
|
||||
resp.ServerError(c, "更新失败:"+err.Error())
|
||||
return
|
||||
}
|
||||
resp.OK(c, post)
|
||||
}
|
||||
|
||||
// Delete DELETE /api/admin/posts/:id
|
||||
func (h *AdminPostHandler) Delete(c *gin.Context) {
|
||||
res := h.db.Delete(&model.Post{}, "id = ?", c.Param("id"))
|
||||
if res.Error != nil {
|
||||
resp.ServerError(c, "删除失败")
|
||||
return
|
||||
}
|
||||
if res.RowsAffected == 0 {
|
||||
resp.NotFound(c, "文章不存在")
|
||||
return
|
||||
}
|
||||
resp.OK(c, nil)
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"git.sundynix.cn/Blizzard/sundynix-site/server/internal/model"
|
||||
"git.sundynix.cn/Blizzard/sundynix-site/server/internal/pkg/resp"
|
||||
)
|
||||
|
||||
// PostHandler 面向用户端的公开文章接口。
|
||||
type PostHandler struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
func NewPostHandler(db *gorm.DB) *PostHandler {
|
||||
return &PostHandler{db: db}
|
||||
}
|
||||
|
||||
// List GET /api/posts — 已发布文章列表(不含正文)
|
||||
func (h *PostHandler) List(c *gin.Context) {
|
||||
var items []model.PostListItem
|
||||
err := h.db.Model(&model.Post{}).
|
||||
Where("published_at IS NOT NULL").
|
||||
Order("published_at DESC").
|
||||
Find(&items).Error
|
||||
if err != nil {
|
||||
resp.ServerError(c, "查询失败")
|
||||
return
|
||||
}
|
||||
resp.OK(c, items)
|
||||
}
|
||||
|
||||
// Get GET /api/posts/:slug — 文章详情(含 Markdown 正文)
|
||||
func (h *PostHandler) Get(c *gin.Context) {
|
||||
var post model.Post
|
||||
err := h.db.
|
||||
Where("slug = ? AND published_at IS NOT NULL", c.Param("slug")).
|
||||
First(&post).Error
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
resp.NotFound(c, "文章不存在")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
resp.ServerError(c, "查询失败")
|
||||
return
|
||||
}
|
||||
resp.OK(c, post)
|
||||
}
|
||||
Reference in New Issue
Block a user