Files
gin-article/handler/index.go
T
2023-03-09 14:05:19 +08:00

46 lines
730 B
Go

package handler
import (
"bufio"
"os"
"strconv"
"github.com/gin-gonic/gin"
)
func Index(c *gin.Context) {
p, _ := strconv.Atoi(c.Query("p"))
if p < 1 {
p = 1
}
limit := 5
start := (p - 1) * limit
list := make([]*Article, 0)
sortListPath := "./data/index_sort_list"
file, _ := os.Open(sortListPath)
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Scan()
total, _ := strconv.Atoi(scanner.Text())
for i := 0; i < start+limit; i++ {
scanner.Scan()
if i >= start {
id := scanner.Text()
article, err := new(Article).GetArticleById(id)
if err != nil {
continue
}
list = append(list, article)
}
}
c.HTML(200, "index.html", gin.H{
"list": list,
"total": total,
})
}