Files
gin-article/handler/index.go
T
2023-03-10 14:36:01 +08:00

63 lines
974 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 := 100
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)
}
}
nextPage := 0
if start+limit < total {
nextPage = p + 1
}
prevPage := 1
if p > 2 {
prevPage = p + 1
}
title := os.Getenv("HOME_TITLE")
c.HTML(200, "index.html", gin.H{
"list": list,
"total": total,
"title": title,
"page": p,
"nextPage": nextPage,
"prevPage": prevPage,
})
}