forked from nytimes/gizmo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cats.go
59 lines (52 loc) · 1.5 KB
/
cats.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
package service
import (
"html"
"net/http"
"text/template"
"github.com/NYTimes/gizmo/examples/nyt"
"github.com/NYTimes/gizmo/server"
"github.com/sirupsen/logrus"
)
func (s *MixedService) GetCats(w http.ResponseWriter, r *http.Request) {
res, err := s.client.SemanticConceptSearch("des", "cats")
if err != nil {
server.LogWithFields(r).WithFields(logrus.Fields{
"error": err,
}).Error("unable to perform semantic search")
http.Error(w, "unable to perform cat search", http.StatusServiceUnavailable)
return
}
w.Header().Add("Content-Type", "text/html; charset=utf-8")
err = catsTemplate.Execute(w, &catList{res})
if err != nil {
server.LogWithFields(r).WithFields(logrus.Fields{
"error": err,
}).Error("unable to execute cats template")
http.Error(w, "unable to perform cat search", http.StatusServiceUnavailable)
}
}
var tempFuncs = template.FuncMap{"unescape": html.UnescapeString}
var catsTemplate = template.Must(template.New("dash").Funcs(tempFuncs).Parse(dashHTML))
type catList struct {
Articles []*nyt.SemanticConceptArticle
}
const dashHTML = `<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>NYT Cat Articles</title>
</head>
<body>
<h1>Recent nytimes.com Articles about "Cats"</h1>
<ol>
{{ range .Articles }}
<li class="article">
<h2>{{ unescape .Title }}</h2>
<h5>{{ unescape .Byline }}</h5>
<p><a target="none" href="{{ .Url }}">{{ .Url }}</a></p>
<p>{{ unescape .Body }}</p>
</li>
{{ end }}
</ol>
</body>
</html>`