This commit is contained in:
zhangchao
2024-11-11 19:35:24 +08:00
parent f9b330e5a9
commit 67a20f60eb
2 changed files with 15 additions and 3 deletions

2
go.mod
View File

@@ -3,6 +3,7 @@ module go_blog
go 1.20
require (
github.com/fsnotify/fsnotify v1.7.0
github.com/gin-gonic/gin v1.9.1
github.com/go-ini/ini v1.67.0
github.com/golang-jwt/jwt v3.2.2+incompatible
@@ -12,7 +13,6 @@ require (
)
require (
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-sql-driver/mysql v1.7.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect

16
main.go
View File

@@ -6,6 +6,7 @@ import (
"go_blog/conf"
"go_blog/controllers"
"go_blog/models"
"go_blog/serializers"
"log"
"net/http"
"strconv"
@@ -74,9 +75,20 @@ func esSSE(c *gin.Context) {
func registerRoutes(r *gin.Engine) {
var items []models.Content
models.DB.Select("*").Limit(5).Find(&items, "type = ?", "post")
r.GET("/", func(c *gin.Context) {
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)
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,
})