From d145c2fd646e252fee561f40b2d81c44d8523385 Mon Sep 17 00:00:00 2001 From: woon Date: Thu, 9 Mar 2023 14:05:19 +0800 Subject: [PATCH] update --- .gitignore | 3 +- article/1.md | 14 ---- article/2.md | 1 - article/go/3.md | 1 - article/go/5.md | 5 -- handler/article.go | 161 ++++++++++++++++++++++++++++++++++++++++----- handler/index.go | 54 +++++++-------- main.go | 21 +++++- router.go | 2 +- tmpl/article.html | 2 +- tmpl/index.html | 5 +- 11 files changed, 200 insertions(+), 69 deletions(-) delete mode 100644 article/1.md delete mode 100644 article/2.md delete mode 100644 article/go/3.md delete mode 100644 article/go/5.md diff --git a/.gitignore b/.gitignore index 2eea525..c06317a 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.env \ No newline at end of file +.env +/data \ No newline at end of file diff --git a/article/1.md b/article/1.md deleted file mode 100644 index 5fc5b32..0000000 --- a/article/1.md +++ /dev/null @@ -1,14 +0,0 @@ -## hello - - -```go -func getTrue() bool { - return true -} -``` - -Name | Age ---------|------ -Bob | 27 -Alice | 23 -Alice | 23 \ No newline at end of file diff --git a/article/2.md b/article/2.md deleted file mode 100644 index 6e23a7b..0000000 --- a/article/2.md +++ /dev/null @@ -1 +0,0 @@ -# hello2 \ No newline at end of file diff --git a/article/go/3.md b/article/go/3.md deleted file mode 100644 index 3164348..0000000 --- a/article/go/3.md +++ /dev/null @@ -1 +0,0 @@ -# this is go \ No newline at end of file diff --git a/article/go/5.md b/article/go/5.md deleted file mode 100644 index cff3f55..0000000 --- a/article/go/5.md +++ /dev/null @@ -1,5 +0,0 @@ -# article-5 - -hello - -this is article 5;;; \ No newline at end of file diff --git a/handler/article.go b/handler/article.go index 6b529f8..af0f465 100644 --- a/handler/article.go +++ b/handler/article.go @@ -1,42 +1,171 @@ package handler import ( - "fmt" + "bufio" + "errors" "html/template" "io/ioutil" "os" + "path/filepath" + "sort" + "strconv" "strings" + "time" "github.com/gin-gonic/gin" "github.com/russross/blackfriday" ) -func Article(c *gin.Context) { - id := c.Param("id") +type ArticleConf struct { + Title string + Time int64 +} - id = strings.Replace(id, "-", "/", -1) +type Article struct { + Id string + Uri string + Conf ArticleConf + Content string + Path string +} - articleBasePath := os.Getenv("ARTICLE_PATH") +func (a *Article) GetArticleById(id string) (*Article, error) { + path := os.Getenv("ARTICLE_PATH") + strings.Replace(id, "-", "/", -1) + ".md" - articlePath := articleBasePath + "/" + id + ".md" + return a.GetArticleByPath(path) +} - if _, err := os.Stat(articlePath); os.IsNotExist(err) { - c.String(200, "文章不存在") +func (a *Article) GetArticleByPath(path string) (*Article, error) { + a.Path = path + a.Uri = strings.Replace(a.Path, os.Getenv("ARTICLE_PATH"), "", -1) + a.Id = a.Uri[:len(a.Uri)-len(filepath.Ext(a.Uri))] + a.Id = strings.Replace(a.Id, "\\", "/", -1) + a.Id = strings.Replace(a.Id, "/", "-", -1) + + // 读取文件内容 + fileByte, err := ioutil.ReadFile(a.Path) + if err != nil { + return a, errors.New("文章不存在") + } + + parts := strings.Split(string(fileByte), "<-->") + var articleConf map[string]string + + if len(parts) == 2 { + if len(parts[0]) > 0 { + articleConf = GetArticleConf(parts[0]) + if len(articleConf["date"]) > 0 { + t, err := time.Parse("2006-1-2 15:04", articleConf["date"]) + if err == nil { + a.Conf.Time = t.Unix() + } + } + + if len(articleConf["title"]) > 0 { + a.Conf.Title = articleConf["title"] + } + } + + a.Content = parts[1] + } else { + a.Conf.Title = a.Id + info, _ := os.Stat(a.Path) + mtime := info.ModTime() + a.Conf.Time = mtime.Unix() + a.Content = parts[0] + } + return a, nil +} + +func GetArticle(c *gin.Context) { + article, err := new(Article).GetArticleById(c.Param("id")) + + if err != nil { + c.String(200, err.Error()) return } - // 读取Go Markdown文件 - input, err := ioutil.ReadFile(articlePath) - if err != nil { - fmt.Println(err) - os.Exit(1) - } - // 转换为HTML - output := blackfriday.MarkdownCommon(input) + output := blackfriday.MarkdownCommon([]byte(article.Content)) c.HTML(200, "article.html", gin.H{ "content": template.HTML(string(output)), + "title": article.Conf.Title, }) } + +func MakeArticleOrder() { + articleBasePath := os.Getenv("ARTICLE_PATH") + + list := make(map[string]int64) + + aritcleNew := new(Article) + + _ = filepath.Walk(articleBasePath, func(path string, info os.FileInfo, err error) error { + + if info.IsDir() || filepath.Ext(path) != ".md" { + return nil + } + article, err := aritcleNew.GetArticleByPath(path) + if err != nil { + return nil + } + list[article.Id] = article.Conf.Time + + return nil + }) + + if len(list) == 0 { + return + } + + sortList := sortArticleByTime(list) + filename := "./data/index_sort_list" + file, err := os.Create(filename) + if err != nil { + return + } + defer file.Close() + writer := bufio.NewWriter(file) + total := len(sortList) + + writer.WriteString(strconv.Itoa(total) + "\n") + + for _, info := range sortList { + writer.WriteString(info.Title + "\n") + } + + writer.Flush() +} + +func GetArticleConf(conf string) map[string]string { + data := make(map[string]string) + conf = strings.Replace(conf, "\r\n", "\n", -1) + list := strings.Split(conf, "\n") + + for _, conf := range list { + index := strings.Index(conf, ":") + if index == -1 { + continue + } + key := conf[0:index] + value := conf[index+1:] + data[key] = value + } + return data +} + +func sortArticleByTime(m map[string]int64) []ArticleConf { + pairs := make([]ArticleConf, 0, len(m)) + for k, v := range m { + pairs = append(pairs, ArticleConf{k, v}) + } + + // 对结构体切片按照值进行排序 + sort.Slice(pairs, func(i, j int) bool { + return pairs[i].Time > pairs[j].Time + }) + + return pairs +} diff --git a/handler/index.go b/handler/index.go index ced07c7..316b64b 100644 --- a/handler/index.go +++ b/handler/index.go @@ -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, }) } diff --git a/main.go b/main.go index 5d187fc..5fa92c1 100644 --- a/main.go +++ b/main.go @@ -2,9 +2,12 @@ package main import ( "flag" + "gin-article/handler" "log" "os" "strconv" + "sync" + "time" "github.com/joho/godotenv" ) @@ -26,5 +29,21 @@ func main() { // 解析命令行参数 flag.Parse() - r.Run(":" + strconv.Itoa(*port)) + var wg sync.WaitGroup + wg.Add(1) + + go func() { + r.Run(":" + strconv.Itoa(*port)) + }() + + wg.Add(1) + + go func() { + for { + handler.MakeArticleOrder() + time.Sleep(10 * time.Second) + } + }() + + wg.Wait() } diff --git a/router.go b/router.go index 9aef71f..346279d 100644 --- a/router.go +++ b/router.go @@ -10,7 +10,7 @@ func Router() *gin.Engine { r := gin.Default() r.GET("/", handler.Index) - r.GET("/article/:id", handler.Article) + r.GET("/article/:id", handler.GetArticle) return r } diff --git a/tmpl/article.html b/tmpl/article.html index 130f3c2..6822f31 100644 --- a/tmpl/article.html +++ b/tmpl/article.html @@ -5,7 +5,7 @@ - title + {{ .title }} diff --git a/tmpl/index.html b/tmpl/index.html index 8161c44..b905c25 100644 --- a/tmpl/index.html +++ b/tmpl/index.html @@ -5,13 +5,14 @@ - title + Home +
总数: {{ .total }}