Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 668d296

Browse files
committed
增加 feed
1 parent 2729602 commit 668d296

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed

src/http/controller/feed.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
// Copyright 2016 The StudyGolang Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style
3+
// license that can be found in the LICENSE file.
4+
// http://studygolang.com
5+
// Author: polaris [email protected]
6+
7+
package controller
8+
9+
import (
10+
"logic"
11+
"model"
12+
"net/http"
13+
"time"
14+
15+
. "http"
16+
17+
"github.com/gorilla/feeds"
18+
"github.com/labstack/echo"
19+
)
20+
21+
type FeedController struct{}
22+
23+
// 注册路由
24+
func (self FeedController) RegisterRoute(g *echo.Group) {
25+
g.Get("/feed", self.List)
26+
}
27+
28+
func (self FeedController) List(ctx echo.Context) error {
29+
link := "http://" + logic.WebsiteSetting.Domain
30+
isHttps := CheckIsHttps(ctx)
31+
if isHttps {
32+
link = "https://" + logic.WebsiteSetting.Domain
33+
}
34+
35+
now := time.Now()
36+
37+
feed := &feeds.Feed{
38+
Title: logic.WebsiteSetting.Name,
39+
Link: &feeds.Link{Href: link},
40+
Description: logic.WebsiteSetting.Slogan,
41+
Author: &feeds.Author{Name: "polaris", Email: "[email protected]"},
42+
Created: now,
43+
Updated: now,
44+
}
45+
46+
siteFeeds := logic.DefaultFeed.FindRecent(ctx, 50)
47+
48+
feed.Items = make([]*feeds.Item, len(siteFeeds))
49+
50+
for i, siteFeed := range siteFeeds {
51+
strObjtype := ""
52+
switch siteFeed.Objtype {
53+
case model.TypeTopic:
54+
strObjtype = "主题"
55+
case model.TypeResource:
56+
strObjtype = "资源"
57+
case model.TypeArticle:
58+
strObjtype = "文章"
59+
case model.TypeProject:
60+
strObjtype = "开源项目"
61+
case model.TypeBook:
62+
strObjtype = "图书"
63+
case model.TypeWiki:
64+
strObjtype = "Wiki"
65+
}
66+
feed.Items[i] = &feeds.Item{
67+
Title: siteFeed.Title,
68+
Link: &feeds.Link{Href: link + siteFeed.Uri},
69+
Description: "这是" + strObjtype,
70+
Created: time.Time(siteFeed.CreatedAt),
71+
Updated: time.Time(siteFeed.UpdatedAt),
72+
}
73+
}
74+
75+
atom, err := feed.ToAtom()
76+
if err != nil {
77+
return err
78+
}
79+
80+
return self.responseXML(ctx, atom)
81+
}
82+
83+
func (FeedController) responseXML(ctx echo.Context, data string) (err error) {
84+
response := ctx.Response()
85+
response.Header().Set(echo.HeaderContentType, echo.MIMEApplicationXMLCharsetUTF8)
86+
response.WriteHeader(http.StatusOK)
87+
_, err = response.Write([]byte(data))
88+
return
89+
}

src/http/controller/routes.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ func RegisterRoutes(g *echo.Group) {
3737
new(DownloadController).RegisterRoute(g)
3838
new(LinkController).RegisterRoute(g)
3939

40+
new(FeedController).RegisterRoute(g)
41+
4042
new(WechatController).RegisterRoute(g)
4143

4244
new(InstallController).RegisterRoute(g)

src/vendor/manifest

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@
145145
"revision": "1ea25387ff6f684839d82767c1733ff4d4d15d0a",
146146
"branch": "master"
147147
},
148+
{
149+
"importpath": "github.com/gorilla/feeds",
150+
"repository": "https://github.com/gorilla/feeds",
151+
"revision": "b78e02c3f88b84269be5c20c2b98127bbea1fcba",
152+
"branch": "master"
153+
},
148154
{
149155
"importpath": "github.com/gorilla/schema",
150156
"repository": "https://github.com/gorilla/schema",

0 commit comments

Comments
 (0)