-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
136 lines (113 loc) · 3.01 KB
/
get.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
// Copyright 2013 Joshua Marsh. All rights reserved. Use of this
// source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package list
import (
"appengine"
"appengine/datastore"
"appengine/memcache"
"fmt"
"github.com/gorilla/mux"
"github.com/icub3d/gorca"
"net/http"
"strconv"
"time"
)
// GetAllLists fetches all of the lists.
func GetAllLists(w http.ResponseWriter, r *http.Request) {
// Create the query.
c := appengine.NewContext(r)
q := datastore.NewQuery("List").Order("-LastModified")
// Fetch the lists.
lists := []List{}
if _, err := q.GetAll(c, &lists); err != nil {
gorca.LogAndUnexpected(c, w, r, err)
return
}
// Write the lists as JSON.
gorca.WriteJSON(c, w, r, lists)
}
// GetList fetches the list for the given tag.
func GetList(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
// Get the Key.
vars := mux.Vars(r)
key := vars["key"]
// Handle Not-modified
ut := r.FormValue("date")
if ut != "" {
notmod(c, w, r, key, ut)
return
}
l, ok := GetListHelper(c, w, r, key)
if !ok {
return
}
// Save the results to memcache.
item := &memcache.Item{
Key: key,
Value: []byte(fmt.Sprintf("%d", l.LastModified.Unix())),
}
if err := memcache.Set(c, item); err != nil {
gorca.Log(c, r, "error", "failed to set memcache: %v", err)
}
// Write the lists as JSON.
gorca.WriteJSON(c, w, r, l)
}
// notmod checks to see if the cached date for the key is newer than
// the date given from the url. This call is terminal. It will always
// respond to the request. If the dates are equal, then this function
// sends a 304 Not Modified. If an error occurs, the error is logged
// and sent back.
func notmod(c appengine.Context, w http.ResponseWriter,
r *http.Request, key string, date string) {
// Convert the given string.
i, err := strconv.ParseInt(date, 10, 64)
if err != nil {
gorca.LogAndFailed(c, w, r, err)
return
}
t := time.Unix(i, 0)
// Try to get the key from memcache
item, err := memcache.Get(c, key)
if err != nil && err != memcache.ErrCacheMiss {
gorca.LogAndFailed(c, w, r, err)
return
}
var mt time.Time
// Check to see if it's simply not there.
if err == memcache.ErrCacheMiss {
gorca.Log(c, r, "info", "failed to get memcache: %s", key)
// Try to get the list.
l, ok := GetListHelper(c, w, r, key)
if !ok {
return
}
// Save the results to memcache.
item := &memcache.Item{
Key: key,
Value: []byte(fmt.Sprintf("%d", l.LastModified.Unix())),
}
if err := memcache.Set(c, item); err != nil {
gorca.Log(c, r, "error", "failed to set memcache: %v", err)
}
mt = l.LastModified
} else {
// Convert the memcache string.
mi, err := strconv.ParseInt(string(item.Value), 10, 64)
if err != nil {
gorca.LogAndFailed(c, w, r, err)
return
}
mt = time.Unix(mi, 0)
}
if mt.Equal(t) {
// Write out the not modified.
w.WriteHeader(http.StatusNotModified)
return
}
// Write out that we modified.
gorca.WriteMessage(c, w, r, "success", "Modified.",
http.StatusOK)
return
}