add api/list/tag/tag_name_here
parent
ba3487852c
commit
7bfae2f004
|
|
@ -112,6 +112,32 @@ func (ex *Exchange) GetFeedItem(ID string) (string, error) {
|
|||
return item.Content, nil
|
||||
}
|
||||
|
||||
func (ex *Exchange) ListTag(tag string, n int) (string, error) {
|
||||
feeds := []*rss.Feed{}
|
||||
feedNames, err := ex.SClient.List(nsForFeeds, "", true, -1)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
for _, feedName := range feedNames {
|
||||
b, err := ex.SClient.Get(nsForFeeds, feedName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
feed, err := rss.Deserialize(b)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for i := range feed.Tags {
|
||||
if feed.Tags[i] == tag {
|
||||
feeds = append(feeds, feed)
|
||||
}
|
||||
}
|
||||
}
|
||||
b, err := json.Marshal(feeds)
|
||||
return string(b), err
|
||||
}
|
||||
|
||||
func (ex *Exchange) GetFeedTagRSS(tag string) (string, error) {
|
||||
feedNames, err := ex.SClient.List(nsForFeeds, "", true, -1)
|
||||
if err != nil {
|
||||
|
|
|
|||
1
main.go
1
main.go
|
|
@ -42,6 +42,7 @@ func core() {
|
|||
ex.GetFeedRSS,
|
||||
ex.GetFeedItem,
|
||||
ex.GetFeedTagRSS,
|
||||
ex.ListTag,
|
||||
); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -110,6 +110,11 @@ func Test_Core(t *testing.T) {
|
|||
status: 200,
|
||||
post: func() { time.Sleep(time.Second * 15) },
|
||||
},
|
||||
{
|
||||
method: "get",
|
||||
path: "api/list/tag/gotest",
|
||||
status: 200,
|
||||
},
|
||||
}
|
||||
for _, c := range cases {
|
||||
c.method = strings.ToUpper(c.method)
|
||||
|
|
|
|||
|
|
@ -22,15 +22,24 @@ type Server struct {
|
|||
getFeedHandler func(string, int) (string, error)
|
||||
getFeedItemHandler func(string) (string, error)
|
||||
getFeedTagHandler func(string) (string, error)
|
||||
getListTagHandler func(string, int) (string, error)
|
||||
}
|
||||
|
||||
func New(addr string, newFeedHandler func(string, string, string, []string, time.Duration), getFeedHandler func(string, int) (string, error), getFeedItemHandler func(string) (string, error), getFeedTagHandler func(string) (string, error)) (*Server, error) {
|
||||
func New(
|
||||
addr string,
|
||||
newFeedHandler func(string, string, string, []string, time.Duration),
|
||||
getFeedHandler func(string, int) (string, error),
|
||||
getFeedItemHandler func(string) (string, error),
|
||||
getFeedTagHandler func(string) (string, error),
|
||||
getListTagHandler func(string, int) (string, error),
|
||||
) (*Server, error) {
|
||||
return &Server{
|
||||
addr: addr,
|
||||
newFeedHandler: newFeedHandler,
|
||||
getFeedHandler: getFeedHandler,
|
||||
getFeedItemHandler: getFeedItemHandler,
|
||||
getFeedTagHandler: getFeedTagHandler,
|
||||
getListTagHandler: getListTagHandler,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
|
@ -81,6 +90,8 @@ func (s *Server) api(w http.ResponseWriter, r *http.Request) {
|
|||
switch advance(r) {
|
||||
case "feed":
|
||||
s.feed(w, r)
|
||||
case "list":
|
||||
s.list(w, r)
|
||||
default:
|
||||
s.notFound(w, r)
|
||||
}
|
||||
|
|
@ -198,6 +209,24 @@ func (s *Server) getFeed(w http.ResponseWriter, r *http.Request) {
|
|||
fmt.Fprintln(w, feedBody)
|
||||
}
|
||||
|
||||
func (s *Server) list(w http.ResponseWriter, r *http.Request) {
|
||||
switch advance(r) {
|
||||
case "tag":
|
||||
s.listTag(w, r)
|
||||
default:
|
||||
s.notFound(w, r)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Server) listTag(w http.ResponseWriter, r *http.Request) {
|
||||
tag := advance(r)
|
||||
out, err := s.getListTagHandler(tag, -1)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
fmt.Fprintln(w, out)
|
||||
}
|
||||
|
||||
func validURL(loc string) bool {
|
||||
_, err := url.ParseRequestURI(loc)
|
||||
return err == nil
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ func Test_Server(t *testing.T) {
|
|||
testPort = strings.Split(server.Listener.Addr().String(), ":")[1]
|
||||
|
||||
var err error
|
||||
s, err := New(testPort, func(string, string, string, []string, time.Duration) {}, func(string, int) (string, error) { return "", nil }, func(string) (string, error) { return "", nil }, func(string) (string, error) { return "", nil })
|
||||
s, err := New(testPort, func(string, string, string, []string, time.Duration) {}, func(string, int) (string, error) { return "", nil }, func(string) (string, error) { return "", nil }, func(string) (string, error) { return "", nil }, func(string, int) (string, error) { return "", nil })
|
||||
if err != nil {
|
||||
t.Errorf("failed to create server: %v", err)
|
||||
}
|
||||
|
|
@ -58,6 +58,12 @@ func Test_Server(t *testing.T) {
|
|||
if err := checkStatus("GET", "api/feed/tag/b", http.StatusOK); err != nil {
|
||||
t.Errorf(err.Error())
|
||||
}
|
||||
if err := checkStatus("GET", "api/feed/tag/b", http.StatusOK); err != nil {
|
||||
t.Errorf(err.Error())
|
||||
}
|
||||
if err := checkStatus("GET", "api/list/tag/b", http.StatusOK); err != nil {
|
||||
t.Errorf(err.Error())
|
||||
}
|
||||
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue