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

Skip to content

Commit 792f5e0

Browse files
committed
atom feed 订阅
1 parent 683299f commit 792f5e0

File tree

3 files changed

+97
-22
lines changed

3 files changed

+97
-22
lines changed

src/http/controller/feed.go

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
package controller
88

99
import (
10+
"fmt"
1011
"logic"
1112
"model"
1213
"net/http"
@@ -22,14 +23,20 @@ type FeedController struct{}
2223

2324
// 注册路由
2425
func (self FeedController) RegisterRoute(g *echo.Group) {
26+
g.Get("/feed.html", self.Atom)
2527
g.Get("/feed.xml", self.List)
2628
}
2729

30+
func (self FeedController) Atom(ctx echo.Context) error {
31+
return Render(ctx, "atom.html", map[string]interface{}{})
32+
}
33+
2834
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
35+
link := logic.WebsiteSetting.Domain
36+
if logic.WebsiteSetting.OnlyHttps {
37+
link = "https://" + link
38+
} else {
39+
link = "http://" + link
3340
}
3441

3542
now := time.Now()
@@ -43,32 +50,37 @@ func (self FeedController) List(ctx echo.Context) error {
4350
Updated: now,
4451
}
4552

46-
siteFeeds := logic.DefaultFeed.FindRecent(ctx, 50)
53+
respBody, err := logic.DefaultSearcher.FindAtomFeeds(50)
54+
if err != nil {
55+
return err
56+
}
57+
58+
feed.Items = make([]*feeds.Item, len(respBody.Docs))
4759

48-
feed.Items = make([]*feeds.Item, len(siteFeeds))
60+
for i, doc := range respBody.Docs {
61+
url := ""
4962

50-
for i, siteFeed := range siteFeeds {
51-
strObjtype := ""
52-
switch siteFeed.Objtype {
63+
switch doc.Objtype {
5364
case model.TypeTopic:
54-
strObjtype = "主题"
55-
case model.TypeResource:
56-
strObjtype = "资源"
65+
url = fmt.Sprintf("%s/topics/%d", link, doc.Objid)
5766
case model.TypeArticle:
58-
strObjtype = "文章"
67+
url = fmt.Sprintf("%s/articles/%d", link, doc.Objid)
68+
case model.TypeResource:
69+
url = fmt.Sprintf("%s/resources/%d", link, doc.Objid)
5970
case model.TypeProject:
60-
strObjtype = "开源项目"
61-
case model.TypeBook:
62-
strObjtype = "图书"
71+
url = fmt.Sprintf("%s/p/%d", link, doc.Objid)
6372
case model.TypeWiki:
64-
strObjtype = "Wiki"
73+
url = fmt.Sprintf("%s/wiki/%d", link, doc.Objid)
74+
case model.TypeBook:
75+
url = fmt.Sprintf("%s/book/%d", link, doc.Objid)
6576
}
6677
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),
78+
Title: doc.Title,
79+
Link: &feeds.Link{Href: url},
80+
Author: &feeds.Author{Name: doc.Author},
81+
Description: doc.Content,
82+
Created: time.Time(doc.SortTime),
83+
Updated: time.Time(doc.UpdatedAt),
7284
}
7385
}
7486

src/logic/searcher.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,39 @@ func (this *SearcherLogic) SearchByField(field, value string, start, rows int, s
417417
return searchResponse.RespBody, nil
418418
}
419419

420+
func (this *SearcherLogic) FindAtomFeeds(rows int) (*model.ResponseBody, error) {
421+
selectUrl := this.engineUrl + "/select?"
422+
423+
var values = url.Values{
424+
"q": []string{"*:*"},
425+
"sort": []string{"sort_time desc"},
426+
"wt": []string{"json"},
427+
"start": []string{"0"},
428+
"rows": []string{strconv.Itoa(rows)},
429+
}
430+
431+
resp, err := http.Get(selectUrl + values.Encode())
432+
if err != nil {
433+
logger.Errorln("search error:", err)
434+
return &model.ResponseBody{}, err
435+
}
436+
437+
defer resp.Body.Close()
438+
439+
var searchResponse model.SearchResponse
440+
err = json.NewDecoder(resp.Body).Decode(&searchResponse)
441+
if err != nil {
442+
logger.Errorln("parse response error:", err)
443+
return &model.ResponseBody{}, err
444+
}
445+
446+
if searchResponse.RespBody == nil {
447+
searchResponse.RespBody = &model.ResponseBody{}
448+
}
449+
450+
return searchResponse.RespBody, nil
451+
}
452+
420453
func (this *SearcherLogic) FillNodeAndUser(ctx context.Context, respBody *model.ResponseBody) (map[int]*model.User, map[int]*model.TopicNode) {
421454
if respBody.NumFound == 0 {
422455
return nil, nil

template/atom.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
{{define "title"}}Feed订阅 {{end}}
2+
{{define "content"}}
3+
<div class="row">
4+
<div class="col-md-9 col-sm-6">
5+
<div class="sep20"></div>
6+
<div>
7+
<ol class="breadcrumb">
8+
<li><a href="/">首页</a></li>
9+
<li class="active">Feed订阅</li>
10+
</ol>
11+
</div>
12+
13+
<div class="page box_white">
14+
<div class="cell" style="font-size: 1.2em;">
15+
<p>欢迎订阅下面的Feed,您可以及时跟踪我的更新:</p>
16+
<p><img src="https://static.studygolang.com/171006/a7c3d8e5e57aab26c01d965f3bb47870.png" alt=""></p>
17+
18+
<ul>
19+
<li><a href="/feed.xml">{{if .is_https}}https://{{else}}http://{{end}}{{.setting.Domain}}/feed.xml</a></li>
20+
</ul>
21+
</div>
22+
</div>
23+
</div>
24+
<div class="col-md-3 col-sm-6">
25+
<div class="sep20"></div>
26+
27+
{{include "common/my_info.html" .}}
28+
</div>
29+
</div>
30+
{{end}}

0 commit comments

Comments
 (0)