From 2a5d17676f969ceda90105fe9bc95b01f3e82f3d Mon Sep 17 00:00:00 2001 From: yvling Date: Thu, 19 Dec 2024 12:13:56 +0800 Subject: [PATCH] new: Add RSS Feed --- _blog/dst/about/index.html | 1 - _blog/dst/post/test.md.html | 30 -------------------- _blog/src/about/index.md | 2 -- config.yaml | 10 +++++-- config/mInfo.go | 13 +++++---- go.mod | 1 + go.sum | 8 ++++++ internal/mApp/mApp.go | 2 ++ internal/mApp/mHandler.go | 11 ++++++++ internal/mApp/mRouter.go | 1 + internal/mApp/mRss.go | 56 +++++++++++++++++++++++++++++++++++++ 11 files changed, 95 insertions(+), 40 deletions(-) delete mode 100644 _blog/dst/about/index.html delete mode 100644 _blog/dst/post/test.md.html delete mode 100644 _blog/src/about/index.md create mode 100644 internal/mApp/mRss.go diff --git a/_blog/dst/about/index.html b/_blog/dst/about/index.html deleted file mode 100644 index fe2da1b..0000000 --- a/_blog/dst/about/index.html +++ /dev/null @@ -1 +0,0 @@ -

About me

diff --git a/_blog/dst/post/test.md.html b/_blog/dst/post/test.md.html deleted file mode 100644 index 20f3df7..0000000 --- a/_blog/dst/post/test.md.html +++ /dev/null @@ -1,30 +0,0 @@ - -

Heading level 1

-

Heading level 2

-

Heading level 3

-

Heading level 4

-
Heading level 5
-
Heading level 6
-

I just love bold text.

-
-

Dorothy followed her through many of the beautiful rooms in her castle.

-
-
    -
  1. First item
  2. -
  3. Second item
  4. -
  5. Third item
  6. -
  7. Fourth item
  8. -
- -
func main() {
-	fmt.Println("Hello, World")
-}
-
-
-

This is a link: Markdown link

-

Image

diff --git a/_blog/src/about/index.md b/_blog/src/about/index.md deleted file mode 100644 index cb191e0..0000000 --- a/_blog/src/about/index.md +++ /dev/null @@ -1,2 +0,0 @@ -## About me - diff --git a/config.yaml b/config.yaml index 749adb3..07ae5c8 100644 --- a/config.yaml +++ b/config.yaml @@ -9,8 +9,8 @@ update_secret: 123456 # configure object storage to automatically obtain article updates storage: - src: _blog/src/post - dst: _blog/dst/post + src: _blog/post/src + dst: _blog/post/dst auto_pull: true type: COS cos: @@ -29,6 +29,7 @@ site: # basic info info: logo: /assets/img/logo.png + link: https://blog.yvling.cn motto: - I hope I can make it out of here. - I hope that I can still feel the sun on my face, and hear the sound of the rain. @@ -39,8 +40,10 @@ site: - —— Andy Dufresne title: Molly Blog author: yvling + email: yvling.cn@outlook.com language: en copyright: Copyright © 2024 Powered by MollyBlog + description: A simple and easy-to-use blog # menu config menu: @@ -54,6 +57,9 @@ site: - name: About icon: fa-solid fa-circle-info url: /about + - name: RSS + icon: fa-solid fa-rss + url: /rss # post config post: diff --git a/config/mInfo.go b/config/mInfo.go index 5da170f..5f7a7c8 100644 --- a/config/mInfo.go +++ b/config/mInfo.go @@ -1,11 +1,14 @@ package config type mInfo struct { - Logo string `yaml:"logo"` - Title string `yaml:"title"` - Author string `yaml:"author"` - Language string `yaml:"language"` - Copyright string `yaml:"copyright"` + Logo string `yaml:"logo"` + Link string `yaml:"link"` + Title string `yaml:"title"` + Author string `yaml:"author"` + Email string `yaml:"email"` + Language string `yaml:"language"` + Copyright string `yaml:"copyright"` + Description string `yaml:"description"` Motto []string `yaml:"motto"` } diff --git a/go.mod b/go.mod index 0647b68..51fee1a 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.23 require ( github.com/88250/lute v1.7.6 github.com/gin-gonic/gin v1.10.0 + github.com/gorilla/feeds v1.2.0 github.com/huichen/wukong v0.0.0-20210824074240-ecbd39fa0b56 github.com/tencentyun/cos-go-sdk-v5 v0.7.59 gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum index b405cf1..6f105ef 100644 --- a/go.sum +++ b/go.sum @@ -68,6 +68,8 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY= github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g= github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k= +github.com/gorilla/feeds v1.2.0 h1:O6pBiXJ5JHhPvqy53NsjKOThq+dNFm8+DFrxBEdzSCc= +github.com/gorilla/feeds v1.2.0/go.mod h1:WMib8uJP3BbY+X8Szd1rA5Pzhdfh+HCCAYT2z7Fza6Y= github.com/huichen/murmur v0.0.0-20130808212358-e0489551cf51 h1:86ZSBmeBzG7dWW2rx9imn5pVKgqe7YjWzZ9qhn4Z+9A= github.com/huichen/murmur v0.0.0-20130808212358-e0489551cf51/go.mod h1:UKrDR4kaPWAPk8cJGrHoTgyI8OmHPNDjUxx/aOK4ySU= github.com/huichen/sego v0.0.0-20210824061530-c87651ea5c76 h1:qNQ2+1IQT9Mor/vfEHePOQSbiapLoNI7sQmpxM7l1Ew= @@ -82,6 +84,10 @@ github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa02 github.com/klauspost/cpuid/v2 v2.2.7 h1:ZWSB3igEs+d0qvnxR/ZBzXVmxkgt8DdzP6m9pfuVLDM= github.com/klauspost/cpuid/v2 v2.2.7/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/leodido/go-urn v1.4.0 h1:WT9HwE9SGECu3lg4d/dIA+jxlljEa1/ffXKmRjqdmIQ= github.com/leodido/go-urn v1.4.0/go.mod h1:bvxc+MVxLKB4z00jd1z+Dvzr47oO32F/QSNjSBOlFxI= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= @@ -101,6 +107,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 h1:OdAsTTz6OkFY5QxjkYwrChwuRruF69c169dPK26NUlk= github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo= +github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= +github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= diff --git a/internal/mApp/mApp.go b/internal/mApp/mApp.go index adc6174..bb8d51b 100644 --- a/internal/mApp/mApp.go +++ b/internal/mApp/mApp.go @@ -23,6 +23,8 @@ type MApp struct { engine *gin.Engine searcher *engine.Engine + RSS string + Posts []*model.MPost IndexedPosts map[uint64]*model.MPost diff --git a/internal/mApp/mHandler.go b/internal/mApp/mHandler.go index ca73dab..40b1227 100644 --- a/internal/mApp/mHandler.go +++ b/internal/mApp/mHandler.go @@ -457,6 +457,9 @@ func (ma *MApp) UpdateBlogHandler(ctx *gin.Context) { // parse post index ma.loadPostIndex() + // parse rss + ma.RSS = ma.generateRSS() + log.Println("update blog success") ctx.JSON(http.StatusOK, gin.H{"msg": "ok"}) } @@ -485,3 +488,11 @@ func (ma *MApp) AboutHandler(ctx *gin.Context) { ctx.HTML(http.StatusOK, "about.html", resData) } + +func (ma *MApp) RSSHandler(ctx *gin.Context) { + if ma.RSS != "" { + ctx.String(http.StatusOK, ma.RSS) + } else { + ctx.String(http.StatusNotFound, "RSS Not Found") + } +} diff --git a/internal/mApp/mRouter.go b/internal/mApp/mRouter.go index 9ccafa2..06e2204 100644 --- a/internal/mApp/mRouter.go +++ b/internal/mApp/mRouter.go @@ -4,6 +4,7 @@ func (ma *MApp) loadRoutes() { ma.engine.Use(ma.AuthMiddleware) ma.engine.GET("/", ma.IndexHandler) + ma.engine.GET("/rss", ma.RSSHandler) ma.engine.GET("/about", ma.AboutHandler) ma.engine.GET("/search", ma.SearchHandler) ma.engine.GET("/archive", ma.ArchiveHandler) diff --git a/internal/mApp/mRss.go b/internal/mApp/mRss.go new file mode 100644 index 0000000..f066324 --- /dev/null +++ b/internal/mApp/mRss.go @@ -0,0 +1,56 @@ +package mApp + +import ( + "fmt" + "log" + "strconv" + "time" + + "github.com/gorilla/feeds" +) + +func (ma *MApp) generateRSS() string { + layout := "2006-01-02 15:04:05" + now := time.Now() + + feed := &feeds.Feed{ + Created: now, + Title: ma.Config.MSite.Info.Title, + Link: &feeds.Link{Href: ma.Config.MSite.Info.Link}, + Author: &feeds.Author{Name: ma.Config.MSite.Info.Author, Email: ma.Config.MSite.Info.Email}, + Description: ma.Config.MSite.Info.Description, + } + + count := 0 + var items []*feeds.Item + for _, post := range ma.Posts { + if count >= ma.Config.MSite.Post.RecentPost.Number { + break + } + + updated, err := time.Parse(layout, post.Date) + if err != nil { + log.Println("Error parsing updated time ", post.Date) + continue + } + + item := &feeds.Item{ + Title: post.Title, + Id: strconv.FormatUint(post.Index, 10), + Link: &feeds.Link{Href: fmt.Sprintf("%s/post/%s", ma.Config.MSite.Info.Link, post.HtmlHash)}, + Author: &feeds.Author{Name: ma.Config.MSite.Info.Author, Email: ma.Config.MSite.Info.Email}, + Updated: updated, + } + + items = append(items, item) + count++ + } + + feed.Items = items + rss, err := feed.ToRss() + if err != nil { + log.Println("Error generating rss ", err) + } + + return rss +}