Skip to content

Commit

Permalink
chore: 优化rss信息
Browse files Browse the repository at this point in the history
  • Loading branch information
Redish101 committed Aug 31, 2024
1 parent 4470c95 commit 5725273
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 19 deletions.
15 changes: 12 additions & 3 deletions internal/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -679,14 +679,14 @@ const docTemplate = `{
},
"/rss": {
"get": {
"description": "获取包含所有文章的RSS",
"description": "获取包含所有文章的Feed",
"produces": [
"text/xml"
],
"tags": [
"Rss"
"Feed"
],
"summary": "获取Rss",
"summary": "获取Feed",
"responses": {
"200": {
"description": "RSS Feed"
Expand Down Expand Up @@ -907,6 +907,9 @@ const docTemplate = `{
"desc": {
"type": "string"
},
"draft": {
"type": "boolean"
},
"slug": {
"type": "string"
},
Expand All @@ -930,6 +933,9 @@ const docTemplate = `{
"desc": {
"type": "string"
},
"draft": {
"type": "boolean"
},
"slug": {
"type": "string"
},
Expand Down Expand Up @@ -1090,6 +1096,9 @@ const docTemplate = `{
"desc": {
"type": "string"
},
"draft": {
"type": "boolean"
},
"id": {
"type": "integer"
},
Expand Down
15 changes: 12 additions & 3 deletions internal/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -672,14 +672,14 @@
},
"/rss": {
"get": {
"description": "获取包含所有文章的RSS",
"description": "获取包含所有文章的Feed",
"produces": [
"text/xml"
],
"tags": [
"Rss"
"Feed"
],
"summary": "获取Rss",
"summary": "获取Feed",
"responses": {
"200": {
"description": "RSS Feed"
Expand Down Expand Up @@ -900,6 +900,9 @@
"desc": {
"type": "string"
},
"draft": {
"type": "boolean"
},
"slug": {
"type": "string"
},
Expand All @@ -923,6 +926,9 @@
"desc": {
"type": "string"
},
"draft": {
"type": "boolean"
},
"slug": {
"type": "string"
},
Expand Down Expand Up @@ -1083,6 +1089,9 @@
"desc": {
"type": "string"
},
"draft": {
"type": "boolean"
},
"id": {
"type": "integer"
},
Expand Down
12 changes: 9 additions & 3 deletions internal/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ definitions:
type: string
desc:
type: string
draft:
type: boolean
slug:
type: string
title:
Expand All @@ -90,6 +92,8 @@ definitions:
type: string
desc:
type: string
draft:
type: boolean
slug:
type: string
title:
Expand Down Expand Up @@ -202,6 +206,8 @@ definitions:
$ref: '#/definitions/gorm.DeletedAt'
desc:
type: string
draft:
type: boolean
id:
type: integer
slug:
Expand Down Expand Up @@ -664,7 +670,7 @@ paths:
- 站点管理
/rss:
get:
description: 获取包含所有文章的RSS
description: 获取包含所有文章的Feed
produces:
- text/xml
responses:
Expand All @@ -674,9 +680,9 @@ paths:
description: 服务器错误
schema:
$ref: '#/definitions/common.Resp'
summary: 获取Rss
summary: 获取Feed
tags:
- Rss
- Feed
/site:
get:
description: 获取站点信息
Expand Down
14 changes: 12 additions & 2 deletions internal/rss/rss.go → internal/feed/base.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package rss
package feed

import (
"fmt"

"github.com/redish101/reblog/internal/core"
"github.com/redish101/reblog/internal/model"

"github.com/gorilla/feeds"
)

func GenerateRSS(app *core.App, articles []*model.Article) (string, error) {
func GenerateFeed(app *core.App, articles []*model.Article) (string, error) {
s := app.Query().Site

site, err := s.First()
Expand All @@ -16,9 +18,16 @@ func GenerateRSS(app *core.App, articles []*model.Article) (string, error) {
return "", err
}

user, err := app.Query().User.First()

if err != nil {
return "", err
}

feed := feeds.Feed{
Title: site.Name,
Description: site.Desc,
Author: &feeds.Author{Name: user.Nickname},
Link: &feeds.Link{Href: site.Url},
}

Expand All @@ -33,6 +42,7 @@ func GenerateRSS(app *core.App, articles []*model.Article) (string, error) {
Title: article.Title,
Description: article.Desc,
Content: markdownService.Render(article.Content),
Link: &feeds.Link{Href: fmt.Sprintf("%s/article/%s", site.Url, article.Slug)},
Created: article.CreatedAt,
})
}
Expand Down
14 changes: 7 additions & 7 deletions server/handler/rss.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ package handler

import (
"github.com/redish101/reblog/internal/core"
"github.com/redish101/reblog/internal/rss"
"github.com/redish101/reblog/internal/feed"
"github.com/redish101/reblog/server/common"

"github.com/gofiber/fiber/v3"
)

// @Summary 获取Rss
// @Description 获取包含所有文章的RSS
// @Tags Rss
// @Summary 获取Feed
// @Description 获取包含所有文章的Feed
// @Tags Feed
// @Produce xml
// @Success 200 "RSS Feed"
// @Failure 500 {object} common.Resp "服务器错误"
// @Router /rss [get]
func Rss(app *core.App, router fiber.Router) {
router.Get("/rss", func(c fiber.Ctx) error {
func Feed(app *core.App, router fiber.Router) {
router.Get("/feed", func(c fiber.Ctx) error {
a := app.Query().Article

limit := app.Config().Rss.Limit
Expand All @@ -26,7 +26,7 @@ func Rss(app *core.App, router fiber.Router) {
return common.RespServerError(c, err)
}

rssString, err := rss.GenerateRSS(app, articles)
rssString, err := feed.GenerateFeed(app, articles)

if err != nil {
return common.RespServerError(c, err)
Expand Down
2 changes: 1 addition & 1 deletion server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func LoadHttp(app *core.App) {
h.ArticleUpdate(app, article)

// rss
h.Rss(app, api)
h.Feed(app, api)

// site
site := api.Group("/site")
Expand Down

0 comments on commit 5725273

Please sign in to comment.