2025-04-01 17:59:10 +08:00
|
|
|
package routers // Add the package declaration at the top
|
|
|
|
|
|
|
|
|
|
import (
|
2025-04-15 16:47:00 +08:00
|
|
|
"fmt"
|
2025-04-09 16:56:05 +08:00
|
|
|
"go_blog/controllers"
|
2025-04-15 16:47:00 +08:00
|
|
|
"go_blog/models"
|
|
|
|
|
"go_blog/serializers"
|
|
|
|
|
"log"
|
2025-04-01 17:59:10 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
2025-05-21 20:38:25 +08:00
|
|
|
"gorm.io/gorm"
|
2025-04-01 17:59:10 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func RegisterRoutes(r *gin.Engine) {
|
2025-04-15 16:47:00 +08:00
|
|
|
|
|
|
|
|
// 注册WebSocket路由
|
|
|
|
|
r.GET("/events", esSSE)
|
|
|
|
|
|
|
|
|
|
r.GET("/page", func(c *gin.Context) {
|
|
|
|
|
var items []models.Content
|
|
|
|
|
var pager serializers.Pager
|
|
|
|
|
pager.InitPager(c)
|
|
|
|
|
offset := (pager.Page - 1) * pager.PageSize
|
2025-05-21 20:38:25 +08:00
|
|
|
if dbInterface, ok := c.Get("DB"); ok {
|
|
|
|
|
if db, ok := dbInterface.(*gorm.DB); ok {
|
|
|
|
|
db.Select("*").Offset(offset).Limit(pager.PageSize).Find(&items, "type = ?", "post")
|
|
|
|
|
} else {
|
|
|
|
|
log.Println("无法将 DB 转换为 *gorm.DB 类型")
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "内部服务器错误"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
log.Println("未找到键 'DB' 的上下文值")
|
|
|
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": "内部服务器错误"})
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-15 16:47:00 +08:00
|
|
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
|
|
|
|
"Items": items,
|
|
|
|
|
"Pager": pager,
|
|
|
|
|
"Title": "文章列表",
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
r.GET("/createcontent", func(c *gin.Context) {
|
|
|
|
|
c.HTML(http.StatusOK, "content.tmpl", nil)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.GET("/ws", controllers.WebSocketHandler)
|
|
|
|
|
r.POST("/content", controllers.CreateContentHandler)
|
|
|
|
|
r.POST("/login", controllers.UsersLoginHandler)
|
|
|
|
|
r.POST("/register", controllers.UsersRegisterHandler)
|
|
|
|
|
r.POST("/setinfo", controllers.UsersSetInfoHandler)
|
|
|
|
|
r.POST("/setpwd", controllers.UsersSetPwdHandler)
|
2025-04-01 17:59:10 +08:00
|
|
|
// Frontend routes (dynamic themes)
|
2025-04-09 16:56:05 +08:00
|
|
|
r.GET("/", controllers.Home)
|
|
|
|
|
r.GET("/post/:id", controllers.ShowPost)
|
2025-04-01 17:59:10 +08:00
|
|
|
|
2025-04-09 16:56:05 +08:00
|
|
|
// // Admin panel
|
|
|
|
|
// admin := r.Group("/admin", middleware.AuthRequired())
|
|
|
|
|
// {
|
|
|
|
|
// admin.GET("/themes", controllers.ListThemes)
|
|
|
|
|
// admin.POST("/themes/switch", controllers.SwitchTheme)
|
|
|
|
|
// }
|
2025-04-01 17:59:10 +08:00
|
|
|
|
|
|
|
|
// Static files for themes
|
|
|
|
|
r.StaticFS("/themes", http.Dir("web/themes"))
|
|
|
|
|
}
|
2025-04-15 16:47:00 +08:00
|
|
|
func getUserInfo() models.User {
|
|
|
|
|
user := models.User{
|
|
|
|
|
Name: "user",
|
|
|
|
|
Gender: "male",
|
|
|
|
|
Age: 18,
|
|
|
|
|
Password: "nothings",
|
|
|
|
|
PasswordHash: []byte("nothings"),
|
|
|
|
|
}
|
|
|
|
|
return user
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func esSSE(c *gin.Context) {
|
|
|
|
|
w := c.Writer
|
|
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "text/event-stream")
|
|
|
|
|
w.Header().Set("Cache-Control", "no-cache")
|
|
|
|
|
w.Header().Set("Connection", "keep-alive")
|
|
|
|
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
|
|
|
|
|
|
|
|
_, ok := w.(http.Flusher)
|
|
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
|
log.Panic("server not support") //浏览器不兼容
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_, err := fmt.Fprintf(w, "id: aaa\ndata: %s\n\n", "dsdf")
|
|
|
|
|
_, er1r := fmt.Fprintf(w, "event: connecttime\ndata: %s\n\n", "connecttime")
|
|
|
|
|
if err != nil || er1r != nil {
|
|
|
|
|
print("error", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|