forked from prest/prest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
29 lines (26 loc) · 811 Bytes
/
cache.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
package middlewares
import (
"fmt"
"net/http"
"github.com/hitlyl/prest/cache"
"github.com/hitlyl/prest/config"
"github.com/urfave/negroni"
)
// CacheMiddleware simple caching to avoid equal queries to the database
func CacheMiddleware() negroni.Handler {
return negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
match, err := MatchURL(r.URL.String())
if err != nil {
http.Error(w, fmt.Sprintf(`{"error": "%v"}`, err), http.StatusInternalServerError)
return
}
// team will not be used when downloading information, second result ignored
cacheRule, _ := cache.EndpointRules(r.URL.Path)
if config.PrestConf.Cache.Enabled && r.Method == "GET" && !match && cacheRule {
if cache.BuntGet(r.URL.String(), w) {
return
}
}
next(w, r)
})
}