This repository has been archived by the owner on Oct 26, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.go
106 lines (91 loc) · 2.23 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
"strings"
"time"
)
const (
NSDublinCore = "http://purl.org/dc/elements/1.1/"
NSAtom = "http://www.w3.org/2005/Atom"
SiteURL = "https://hnrss.org"
)
type CDATA struct {
Value string `xml:",cdata"`
}
func Timestamp(fmt string, input time.Time) string {
switch fmt {
case "rss":
return input.Format(time.RFC1123Z)
case "atom", "jsonfeed":
return input.Format(time.RFC3339)
case "http":
return input.Format(http.TimeFormat)
default:
return input.Format(time.RFC1123Z)
}
}
func UTCNow() time.Time {
return time.Now().UTC()
}
func ParseRequest(c *gin.Context, sp *SearchParams, op *OutputParams) {
err := c.ShouldBindQuery(sp)
if err != nil {
c.Error(err)
c.String(http.StatusBadRequest, err.Error())
return
}
if strings.Contains(sp.Query, " OR ") {
sp.Query = strings.Replace(sp.Query, " OR ", " ", -1)
var q []string
for _, f := range strings.Fields(sp.Query) {
q = append(q, fmt.Sprintf("\"%s\"", f))
}
sp.Query = strings.Join(q, " ")
sp.OptionalWords = strings.Join(q, " ")
}
err = c.ShouldBindQuery(op)
if err != nil {
c.Error(err)
c.String(http.StatusBadRequest, err.Error())
return
}
op.Format = c.GetString("format")
op.SelfLink = SiteURL + c.Request.URL.String()
}
func Generate(c *gin.Context, sp *SearchParams, op *OutputParams) {
if op.Format == "" {
op.Format = "rss"
}
results, err := GetResults(sp.Values())
if err != nil {
c.Error(err)
c.String(http.StatusBadGateway, err.Error())
return
}
c.Header("X-Algolia-URL", algoliaSearchURL+sp.Values().Encode())
if len(results.Hits) > 0 {
item := results.Hits[0]
recent := item.GetCreatedAt()
c.Header("Last-Modified", Timestamp("http", recent))
if c.Request.URL.Path == "/item" {
if sp.Query != "" {
op.Title = fmt.Sprintf("Hacker News - \"%s\": \"%s\"", item.StoryTitle, sp.Query)
} else {
op.Title = fmt.Sprintf("Hacker News: New comments on \"%s\"", item.StoryTitle)
}
}
}
switch op.Format {
case "rss":
rss := NewRSS(results, op)
c.XML(http.StatusOK, rss)
case "atom":
atom := NewAtom(results, op)
c.XML(http.StatusOK, atom)
case "jsonfeed":
jsonfeed := NewJSONFeed(results, op)
c.JSON(http.StatusOK, jsonfeed)
}
}