2023-12-20 16:50:05 +08:00
|
|
|
package main
|
|
|
|
|
|
2024-01-10 16:05:15 +08:00
|
|
|
import (
|
2024-11-07 10:09:53 +08:00
|
|
|
"fmt"
|
2025-04-01 17:59:10 +08:00
|
|
|
conf "go_blog/config"
|
2024-07-04 19:15:44 +08:00
|
|
|
"go_blog/controllers"
|
2024-01-10 16:05:15 +08:00
|
|
|
"go_blog/models"
|
2024-11-11 19:35:24 +08:00
|
|
|
"go_blog/serializers"
|
2024-11-07 10:09:53 +08:00
|
|
|
"log"
|
2024-01-22 17:55:16 +08:00
|
|
|
"net/http"
|
2024-08-05 17:19:23 +08:00
|
|
|
"strconv"
|
2024-01-10 16:05:15 +08:00
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2024-07-04 19:15:44 +08:00
|
|
|
type User struct {
|
|
|
|
|
Name string
|
|
|
|
|
Gender string
|
|
|
|
|
Age int
|
|
|
|
|
Password string
|
|
|
|
|
PasswordHash []byte
|
2024-01-10 16:05:15 +08:00
|
|
|
}
|
|
|
|
|
|
2024-07-04 19:15:44 +08:00
|
|
|
const templatePath = "./templates/*"
|
|
|
|
|
|
2024-01-10 16:05:15 +08:00
|
|
|
func init() {
|
|
|
|
|
conf.SetUp()
|
2024-12-16 18:15:15 +08:00
|
|
|
models.SetUp()
|
2024-01-10 16:05:15 +08:00
|
|
|
}
|
2023-12-20 16:50:05 +08:00
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
|
r := gin.Default()
|
|
|
|
|
|
2024-07-04 19:15:44 +08:00
|
|
|
r.LoadHTMLGlob(templatePath)
|
|
|
|
|
// 注册WebSocket路由
|
|
|
|
|
registerRoutes(r)
|
2024-11-07 10:09:53 +08:00
|
|
|
r.GET("/events", esSSE)
|
2024-12-16 14:45:13 +08:00
|
|
|
r.Run(":8910")
|
2024-07-04 19:15:44 +08:00
|
|
|
}
|
2024-11-07 10:09:53 +08:00
|
|
|
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") //浏览器不兼容
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-07 17:44:14 +08:00
|
|
|
_, 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)
|
2024-11-07 10:09:53 +08:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-07-04 19:15:44 +08:00
|
|
|
|
|
|
|
|
func registerRoutes(r *gin.Engine) {
|
|
|
|
|
|
2023-12-20 16:50:05 +08:00
|
|
|
r.GET("/", func(c *gin.Context) {
|
2024-11-11 19:35:24 +08:00
|
|
|
var items []models.Content
|
|
|
|
|
models.DB.Select("*").Limit(5).Find(&items, "type = ?", "post")
|
|
|
|
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
|
|
|
|
"Items": items,
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
r.GET("/page", func(c *gin.Context) {
|
|
|
|
|
var items []models.Content
|
|
|
|
|
var pager serializers.Pager
|
|
|
|
|
pager.InitPager(c)
|
2024-11-11 19:38:40 +08:00
|
|
|
offset := (pager.Page - 1) * pager.PageSize
|
|
|
|
|
models.DB.Select("*").Offset(offset).Limit(pager.PageSize).Find(&items, "type = ?", "post")
|
2024-08-01 02:44:06 +08:00
|
|
|
c.HTML(http.StatusOK, "index.tmpl", gin.H{
|
|
|
|
|
"Items": items,
|
2024-11-26 18:41:24 +08:00
|
|
|
"Pager": pager,
|
|
|
|
|
"Title": "文章列表",
|
2024-08-01 02:44:06 +08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
r.GET("/createcontent", func(c *gin.Context) {
|
|
|
|
|
c.HTML(http.StatusOK, "content.tmpl", nil)
|
2023-12-20 16:50:05 +08:00
|
|
|
})
|
2024-01-10 16:05:15 +08:00
|
|
|
|
2024-08-05 17:19:23 +08:00
|
|
|
r.GET("/post/:id", func(c *gin.Context) {
|
|
|
|
|
id, err := strconv.ParseInt(c.Param("id"), 10, 32)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
var content = models.Content{Cid: int32(id)}
|
|
|
|
|
models.DB.First(&content)
|
|
|
|
|
c.HTML(http.StatusOK, "post.tmpl", content)
|
|
|
|
|
})
|
|
|
|
|
user := getUserInfo()
|
2024-01-10 16:05:15 +08:00
|
|
|
r.GET("/login", func(c *gin.Context) {
|
|
|
|
|
c.HTML(200, "login.tmpl", map[string]interface{}{
|
|
|
|
|
"title": "这个是titile,传入templates中的",
|
|
|
|
|
"user": user,
|
|
|
|
|
})
|
|
|
|
|
})
|
2024-12-19 10:29:31 +08:00
|
|
|
r.GET("/register", func(c *gin.Context) {
|
|
|
|
|
c.HTML(200, "register.tmpl", map[string]interface{}{
|
|
|
|
|
"title": "这个是titile,传入templates中的",
|
|
|
|
|
"user": user,
|
|
|
|
|
})
|
|
|
|
|
})
|
2024-01-10 16:05:15 +08:00
|
|
|
|
2024-07-04 19:15:44 +08:00
|
|
|
r.GET("/ws", controllers.WebSocketHandler)
|
2024-08-01 02:44:06 +08:00
|
|
|
r.POST("/content", controllers.CreateContentHandler)
|
2024-07-04 19:15:44 +08:00
|
|
|
r.POST("/login", controllers.UsersLoginHandler)
|
2024-12-19 09:56:01 +08:00
|
|
|
r.POST("/register", controllers.UsersRegisterHandler)
|
|
|
|
|
r.POST("/setinfo", controllers.UsersSetInfoHandler)
|
|
|
|
|
r.POST("/setpwd", controllers.UsersSetPwdHandler)
|
|
|
|
|
|
2023-12-20 16:50:05 +08:00
|
|
|
}
|
2024-01-22 17:55:16 +08:00
|
|
|
|
2024-07-04 19:15:44 +08:00
|
|
|
func getUserInfo() User {
|
|
|
|
|
user := User{
|
|
|
|
|
Name: "user",
|
|
|
|
|
Gender: "male",
|
|
|
|
|
Age: 18,
|
|
|
|
|
Password: "nothings",
|
|
|
|
|
PasswordHash: []byte("nothings"),
|
2024-01-22 17:55:16 +08:00
|
|
|
}
|
2024-07-04 19:15:44 +08:00
|
|
|
return user
|
2024-01-22 17:55:16 +08:00
|
|
|
}
|