This commit is contained in:
2023-03-09 14:05:19 +08:00
parent b33bbad5f3
commit d145c2fd64
11 changed files with 200 additions and 69 deletions
+28 -26
View File
@@ -1,43 +1,45 @@
package handler
import (
"bufio"
"os"
"path/filepath"
"strings"
"strconv"
"github.com/gin-gonic/gin"
)
func Index(c *gin.Context) {
articleBasePath := os.Getenv("ARTICLE_PATH")
p, _ := strconv.Atoi(c.Query("p"))
if p < 1 {
p = 1
}
limit := 5
start := (p - 1) * limit
list := make([]string, 0)
list := make([]*Article, 0)
sortListPath := "./data/index_sort_list"
file, _ := os.Open(sortListPath)
defer file.Close()
err := filepath.Walk(articleBasePath, func(path string, info os.FileInfo, err error) error {
// 如果是目录,直接返回
if info.IsDir() {
return nil
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)
}
// 如果文件扩展名不是 .md,直接返回
if filepath.Ext(path) != ".md" {
return nil
}
filePath := path
uri := filePath[:len(filePath)-len(filepath.Ext(filePath))]
uri = strings.Replace(uri, articleBasePath, "", -1)
uri = strings.Replace(uri, "/", "-", -1)
list = append(list, uri)
return nil
})
if err != nil {
panic(err)
}
c.HTML(200, "index.html", gin.H{
"list": list,
"list": list,
"total": total,
})
}