user viper
This commit is contained in:
@@ -1,13 +1,43 @@
|
||||
package routers // Add the package declaration at the top
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go_blog/controllers"
|
||||
"go_blog/models"
|
||||
"go_blog/serializers"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func RegisterRoutes(r *gin.Engine) {
|
||||
|
||||
// 注册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
|
||||
models.DB.Select("*").Offset(offset).Limit(pager.PageSize).Find(&items, "type = ?", "post")
|
||||
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)
|
||||
// Frontend routes (dynamic themes)
|
||||
r.GET("/", controllers.Home)
|
||||
r.GET("/post/:id", controllers.ShowPost)
|
||||
@@ -22,3 +52,35 @@ func RegisterRoutes(r *gin.Engine) {
|
||||
// Static files for themes
|
||||
r.StaticFS("/themes", http.Dir("web/themes"))
|
||||
}
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user