This repository has been archived by the owner on Nov 4, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
archive_handler.go
182 lines (156 loc) · 3.87 KB
/
archive_handler.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"
"github.com/pilu/traffic"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"google.golang.org/appengine/memcache"
"google.golang.org/appengine/user"
)
type ArchiveData struct {
Years *map[string]Year
Months *[]string
Posts *[]Entry
IsAdmin bool
}
type Year map[string]Month
type Month []Day
type Day int64
var Months = [12]time.Month{
time.January,
time.February,
time.March,
time.April,
time.May,
time.June,
time.July,
time.August,
time.September,
time.October,
time.November,
time.December,
}
func ArchiveTaskHandler(w traffic.ResponseWriter, r *traffic.Request) {
c := appengine.NewContext(r.Request)
entries, err := AllPosts(c)
if err != nil {
log.Errorf(c, err.Error())
http.Error(w, err.Error(), 500)
return
}
years := make(map[string]Year)
oldest := (*entries)[len(*entries)-1].Datetime
newest := (*entries)[0].Datetime
log.Infof(c, "Oldest: %v, Newest: %v", oldest, newest)
for year := oldest.Year(); year <= newest.Year(); year += 1 {
ystr := strconv.Itoa(year)
years[ystr] = make(Year)
log.Infof(c, "Adding %d.", year)
startMonth := time.January
endMonth := time.December
if year == oldest.Year() {
startMonth = oldest.Month()
}
if year == newest.Year() {
endMonth = newest.Month()
}
for month := startMonth; month <= endMonth; month += 1 {
mstr := month.String()
years[ystr][mstr] = make([]Day, daysIn(month, year)+1)
log.Debugf(c, "Adding %d/%d - %d days.", year, month, len(years[ystr][mstr]))
for day := range years[ystr][mstr] {
if day > 0 {
e, err := PostsForDay(c, int64(year), int64(month), int64(day))
if err != nil {
log.Errorf(c, err.Error())
http.Error(w, err.Error(), 500)
return
}
years[ystr][mstr][day] = Day(len(*e))
}
}
}
}
log.Infof(c, "Added posts.")
// https://blog.golang.org/json-and-go
b, err := json.Marshal(years)
if err != nil {
log.Errorf(c, err.Error())
http.Error(w, err.Error(), 500)
}
item := &memcache.Item{
Key: "archive_data",
Value: b,
}
// Set the item, unconditionally
if err := memcache.Set(c, item); err != nil {
log.Errorf(c, "error setting item: %v", err)
}
}
func ArchiveHandler(w traffic.ResponseWriter, r *traffic.Request) {
c := appengine.NewContext(r.Request)
entries, err := AllPosts(c)
if err != nil {
log.Errorf(c, err.Error())
http.Error(w, err.Error(), 500)
return
}
// Get the item from the memcache
var years map[string]Year
if year_data, err := memcache.Get(c, "archive_data"); err == memcache.ErrCacheMiss {
log.Infof(c, "item not in the cache")
} else if err != nil {
log.Errorf(c, "error getting item: %v", err)
} else {
err := json.Unmarshal(year_data.Value, &years)
if err != nil {
log.Errorf(c, err.Error())
http.Error(w, err.Error(), 500)
return
}
}
data := &ArchiveData{
Years: &years,
Months: &[]string{
time.January.String(),
time.February.String(),
time.March.String(),
time.April.String(),
time.May.String(),
time.June.String(),
time.July.String(),
time.August.String(),
time.September.String(),
time.October.String(),
time.November.String(),
time.December.String(),
},
IsAdmin: user.IsAdmin(c),
Posts: entries,
}
w.Render("archive", data)
}
// daysIn returns the number of days in a month for a given year.
func daysIn(m time.Month, year int) int {
// This is equivalent to time.daysIn(m, year).
return time.Date(year, m+1, 0, 0, 0, 0, 0, time.UTC).Day()
}
func PostsJsonHandler(w traffic.ResponseWriter, r *traffic.Request) {
c := appengine.NewContext(r.Request)
urls := []string{}
entries, err := AllPosts(c)
if err != nil {
log.Errorf(c, err.Error())
http.Error(w, err.Error(), 500)
return
}
for _, e := range *entries {
urls = append(urls, fmt.Sprintf("/post/%d.md", e.Id))
}
w.WriteJSON(urls)
return
}