-
Notifications
You must be signed in to change notification settings - Fork 2
/
page_index.go
126 lines (114 loc) · 2.99 KB
/
page_index.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"bytes"
"compress/gzip"
"github.com/nokivan/gravatar"
"io"
"net/http"
"strings"
"time"
)
var indexCache *bytes.Buffer
var feedCache *bytes.Buffer
func updateIndexAndFeed() {
// TODO pager
indexList := getArticleList()
sortSlice(indexList, func(a, b interface{}) bool {
return a.(*Article).Id > b.(*Article).Id
})
for i, _ := range indexList {
temp := *indexList[i]
temp.Content = makeSummary(temp.Content)
indexList[i] = &temp
}
// index
newIndexCache := &bytes.Buffer{}
if err := tmpl.ExecuteTemplate(newIndexCache, "index", map[string]interface{}{
"config": config,
"articles": indexList,
"header": config["ServerName"],
}); err != nil {
logger.Println("index cache:", err.Error())
}
indexCache = newIndexCache
newFeedCache := &bytes.Buffer{}
if err := tmpl.ExecuteTemplate(newFeedCache, "feed", map[string]interface{}{
"config": config,
"articles": indexList,
"lastBuild": time.Now().String(),
}); err != nil {
logger.Println("feed cache:", err.Error())
}
feedCache = newFeedCache
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.Write(indexCache.Bytes())
}
func feedHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=UTF-8")
w.Write(feedCache.Bytes())
}
func initIndex() {
if config["RootUrl"] != "/" {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, config["RootUrl"], http.StatusFound)
})
}
http.HandleFunc(config["RootUrl"], getGzipHandler(indexHandler))
http.HandleFunc(config["RootUrl"]+"feed", getGzipHandler(feedHandler))
}
type responseRewriter struct {
http.ResponseWriter
io.Writer
}
func (this responseRewriter) Write(data []byte) (int, error) {
return this.Writer.Write(data)
}
func handler2HandlerFunc(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r)
}
}
func getGzipHandler(f http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
f(w, r)
return
}
w.Header().Set("Content-Encoding", "gzip")
gw := gzip.NewWriter(w)
defer gw.Close()
f(responseRewriter{
ResponseWriter: w,
Writer: gw,
}, r)
}
}
func makeSummary(s string) string {
min := 0
for min < 600 && s[min:] != "" {
if idx := strings.Index(s[min:], "</p>"); idx != -1 {
min += idx + len("</p>")
continue
}
if idx := strings.Index(s[min:], "<br/>"); idx != -1 {
min += idx + len("<br/>")
continue
}
if idx := strings.Index(s[min:], "<br>"); idx != -1 {
min += idx + len("<br>")
continue
}
min = len(s)
}
if min != len(s) {
return s[:min] + "<br/>... ...<br/>"
}
return s[:min]
}
func getGravatarURL(email string, size int) string {
emailHash := gravatar.EmailHash(email)
url := gravatar.GetAvatarURL("https", emailHash, gravatar.DefaultMonster, size)
return url.String()
}