2020-07-29 20:45:26 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-30 14:43:04 +00:00
|
|
|
"encoding/json"
|
2020-07-29 20:45:26 +00:00
|
|
|
"net/http"
|
2020-07-30 14:43:04 +00:00
|
|
|
"net/http/httptest"
|
2020-07-29 20:45:26 +00:00
|
|
|
"net/url"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2020-07-30 19:18:13 +00:00
|
|
|
func cacheMiddleware(next http.Handler) http.Handler {
|
2020-07-29 20:45:26 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
requestUrl, _ := url.ParseRequestURI(r.RequestURI)
|
2020-07-30 19:18:13 +00:00
|
|
|
path := slashTrimmedPath(r)
|
2020-08-04 17:42:09 +00:00
|
|
|
if appConfig.Cache.Enable &&
|
2020-07-30 19:08:41 +00:00
|
|
|
// check bypass query
|
2020-07-29 20:45:26 +00:00
|
|
|
!(requestUrl != nil && requestUrl.Query().Get("cache") == "0") {
|
2020-07-30 19:18:13 +00:00
|
|
|
cacheTime, header, body := getCache(r.Context(), path)
|
2020-07-30 14:43:04 +00:00
|
|
|
if cacheTime == 0 {
|
|
|
|
recorder := httptest.NewRecorder()
|
|
|
|
next.ServeHTTP(recorder, r)
|
2020-07-30 19:08:41 +00:00
|
|
|
// copy values from recorder
|
2020-07-30 14:43:04 +00:00
|
|
|
code := recorder.Code
|
2020-07-30 19:08:41 +00:00
|
|
|
// send response
|
2020-07-30 14:43:04 +00:00
|
|
|
for k, v := range recorder.Header() {
|
|
|
|
w.Header()[k] = v
|
|
|
|
}
|
2020-07-30 19:08:41 +00:00
|
|
|
now := time.Now()
|
2020-08-04 17:42:09 +00:00
|
|
|
setCacheHeaders(w, now.Format(time.RFC1123), time.Unix(now.Unix()+appConfig.Cache.Expiration, 0).Format(time.RFC1123))
|
2020-07-30 14:43:04 +00:00
|
|
|
w.Header().Set("GoBlog-Cache", "MISS")
|
|
|
|
w.WriteHeader(code)
|
|
|
|
_, _ = w.Write(recorder.Body.Bytes())
|
|
|
|
// Save cache
|
|
|
|
if code == http.StatusOK {
|
2020-07-30 19:08:41 +00:00
|
|
|
saveCache(path, now, recorder.Header(), recorder.Body.Bytes())
|
2020-07-30 14:43:04 +00:00
|
|
|
}
|
2020-07-29 20:45:26 +00:00
|
|
|
return
|
2020-07-30 19:18:13 +00:00
|
|
|
}
|
|
|
|
cacheTimeString := time.Unix(cacheTime, 0).Format(time.RFC1123)
|
2020-08-04 17:42:09 +00:00
|
|
|
expiresTimeString := time.Unix(cacheTime+appConfig.Cache.Expiration, 0).Format(time.RFC1123)
|
2020-07-30 19:18:13 +00:00
|
|
|
// check conditional request
|
|
|
|
ifModifiedSinceHeader := r.Header.Get("If-Modified-Since")
|
|
|
|
if ifModifiedSinceHeader != "" && ifModifiedSinceHeader == cacheTimeString {
|
2020-07-30 19:08:41 +00:00
|
|
|
setCacheHeaders(w, cacheTimeString, expiresTimeString)
|
2020-07-30 19:18:13 +00:00
|
|
|
// send 304
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
2020-07-30 19:08:41 +00:00
|
|
|
return
|
2020-07-29 20:45:26 +00:00
|
|
|
}
|
2020-07-30 19:18:13 +00:00
|
|
|
// copy cached headers
|
|
|
|
for k, v := range header {
|
|
|
|
w.Header()[k] = v
|
|
|
|
}
|
|
|
|
setCacheHeaders(w, cacheTimeString, expiresTimeString)
|
|
|
|
w.Header().Set("GoBlog-Cache", "HIT")
|
|
|
|
// write cached body
|
|
|
|
_, _ = w.Write(body)
|
2020-07-30 19:08:41 +00:00
|
|
|
return
|
2020-07-29 20:45:26 +00:00
|
|
|
}
|
2020-07-30 19:18:13 +00:00
|
|
|
next.ServeHTTP(w, r)
|
|
|
|
return
|
2020-07-29 20:45:26 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-07-30 19:08:41 +00:00
|
|
|
func setCacheHeaders(w http.ResponseWriter, cacheTimeString string, expiresTimeString string) {
|
|
|
|
w.Header().Set("Cache-Control", "public")
|
|
|
|
w.Header().Set("Last-Modified", cacheTimeString)
|
|
|
|
w.Header().Set("Expires", expiresTimeString)
|
|
|
|
}
|
|
|
|
|
2020-07-30 19:18:13 +00:00
|
|
|
func getCache(context context.Context, path string) (creationTime int64, header map[string][]string, body []byte) {
|
2020-07-30 14:43:04 +00:00
|
|
|
var headerBytes []byte
|
2020-08-04 17:42:09 +00:00
|
|
|
allowedTime := time.Now().Unix() - appConfig.Cache.Expiration
|
2020-07-30 14:43:04 +00:00
|
|
|
row := appDb.QueryRowContext(context, "select COALESCE(time, 0), header, body from cache where path=? and time>=?", path, allowedTime)
|
|
|
|
_ = row.Scan(&creationTime, &headerBytes, &body)
|
|
|
|
header = make(map[string][]string)
|
|
|
|
_ = json.Unmarshal(headerBytes, &header)
|
|
|
|
return
|
2020-07-29 20:45:26 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 19:08:41 +00:00
|
|
|
func saveCache(path string, now time.Time, header map[string][]string, body []byte) {
|
2020-07-30 14:43:04 +00:00
|
|
|
headerBytes, _ := json.Marshal(header)
|
2020-07-29 20:45:26 +00:00
|
|
|
startWritingToDb()
|
|
|
|
tx, err := appDb.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2020-08-04 17:42:09 +00:00
|
|
|
_, _ = tx.Exec("delete from cache where time<?;", now.Unix()-appConfig.Cache.Expiration)
|
2020-07-30 19:08:41 +00:00
|
|
|
_, _ = tx.Exec("insert or replace into cache (path, time, header, body) values (?, ?, ?, ?);", path, now.Unix(), headerBytes, body)
|
2020-07-29 20:45:26 +00:00
|
|
|
_ = tx.Commit()
|
|
|
|
finishWritingToDb()
|
|
|
|
}
|
2020-08-01 13:16:21 +00:00
|
|
|
|
|
|
|
func purgeCache(path string) {
|
|
|
|
startWritingToDb()
|
|
|
|
tx, err := appDb.Begin()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
_, _ = tx.Exec("delete from cache where path=?", path)
|
|
|
|
_ = tx.Commit()
|
|
|
|
finishWritingToDb()
|
|
|
|
}
|