summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--system/admin/cache.go28
-rw-r--r--system/admin/server.go6
-rw-r--r--system/api/analytics/batch.go4
-rw-r--r--system/api/handlers.go4
-rw-r--r--system/db/cache.go79
-rw-r--r--system/db/config.go11
-rw-r--r--system/db/content.go18
7 files changed, 105 insertions, 45 deletions
diff --git a/system/admin/cache.go b/system/admin/cache.go
deleted file mode 100644
index 2abdb26..0000000
--- a/system/admin/cache.go
+++ /dev/null
@@ -1,28 +0,0 @@
-package admin
-
-import (
- "fmt"
- "net/http"
- "strings"
-
- "github.com/bosssauce/ponzu/system/db"
-)
-
-// CacheControl sets the default cache policy on static asset responses
-func CacheControl(next http.Handler) http.Handler {
- return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
- etag := db.ConfigCache("etag")
- policy := fmt.Sprintf("max-age=%d, public, must-revalidate, proxy-revalidate", 60*60*24*30)
- res.Header().Add("Etag", etag)
- res.Header().Add("Cache-Control", policy)
-
- if match := res.Header().Get("If-None-Match"); match != "" {
- if strings.Contains(match, etag) {
- res.WriteHeader(http.StatusNotModified)
- return
- }
- }
-
- next.ServeHTTP(res, req)
- })
-}
diff --git a/system/admin/server.go b/system/admin/server.go
index f80a750..bc6f4d6 100644
--- a/system/admin/server.go
+++ b/system/admin/server.go
@@ -7,6 +7,8 @@ import (
"path/filepath"
"github.com/bosssauce/ponzu/system/admin/user"
+ "github.com/bosssauce/ponzu/system/api"
+ "github.com/bosssauce/ponzu/system/db"
)
// Run adds Handlers to default http listener for Admin
@@ -41,11 +43,11 @@ func Run() {
}
staticDir := filepath.Join(pwd, "cmd", "ponzu", "vendor", "github.com", "bosssauce", "ponzu", "system")
- http.Handle("/admin/static/", CacheControl(http.FileServer(restrict(http.Dir(staticDir)))))
+ http.Handle("/admin/static/", db.CacheControl(http.FileServer(restrict(http.Dir(staticDir)))))
// API path needs to be registered within server package so that it is handled
// even if the API server is not running. Otherwise, images/files uploaded
// through the editor will not load within the admin system.
uploadsDir := filepath.Join(pwd, "uploads")
- http.Handle("/api/uploads/", CacheControl(http.StripPrefix("/api/uploads/", http.FileServer(restrict(http.Dir(uploadsDir))))))
+ http.Handle("/api/uploads/", api.Record(db.CacheControl(http.StripPrefix("/api/uploads/", http.FileServer(restrict(http.Dir(uploadsDir)))))))
}
diff --git a/system/api/analytics/batch.go b/system/api/analytics/batch.go
index c4a60db..68ffd65 100644
--- a/system/api/analytics/batch.go
+++ b/system/api/analytics/batch.go
@@ -19,7 +19,7 @@ func batchInsert(requests chan apiRequest) error {
}
err := store.Update(func(tx *bolt.Tx) error {
- b, err := tx.CreateBucketIfNotExists([]byte("requests"))
+ b, err := tx.CreateBucketIfNotExists([]byte("__requests"))
if err != nil {
return err
}
@@ -64,7 +64,7 @@ func batchPrune(threshold time.Duration) error {
// iterate through all request data
err := store.Update(func(tx *bolt.Tx) error {
- b := tx.Bucket([]byte("requests"))
+ b := tx.Bucket([]byte("__requests"))
err := b.ForEach(func(k, v []byte) error {
var r apiRequest
diff --git a/system/api/handlers.go b/system/api/handlers.go
index 8a1517b..c238ca9 100644
--- a/system/api/handlers.go
+++ b/system/api/handlers.go
@@ -200,14 +200,14 @@ func SendJSON(res http.ResponseWriter, j map[string]interface{}) {
// CORS wraps a HandleFunc to response to OPTIONS requests properly
func CORS(next http.HandlerFunc) http.HandlerFunc {
- return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
+ return db.CacheControl(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
if req.Method == http.MethodOptions {
SendPreflight(res)
return
}
next.ServeHTTP(res, req)
- })
+ }))
}
// Record wraps a HandleFunc to record API requests for analytical purposes
diff --git a/system/db/cache.go b/system/db/cache.go
new file mode 100644
index 0000000..5f2dd03
--- /dev/null
+++ b/system/db/cache.go
@@ -0,0 +1,79 @@
+package db
+
+import (
+ "encoding/base64"
+ "encoding/json"
+ "fmt"
+ "net/http"
+ "net/url"
+ "strings"
+ "time"
+)
+
+// CacheControl sets the default cache policy on static asset responses
+func CacheControl(next http.Handler) http.HandlerFunc {
+ return http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
+ etag := ConfigCache("etag")
+ policy := fmt.Sprintf("max-age=%d, public", 60*60*24*30)
+ res.Header().Add("ETag", etag)
+ res.Header().Add("Cache-Control", policy)
+
+ if match := req.Header.Get("If-None-Match"); match != "" {
+ if strings.Contains(match, etag) {
+ res.WriteHeader(http.StatusNotModified)
+ return
+ }
+ }
+
+ next.ServeHTTP(res, req)
+ })
+}
+
+// NewEtag generates a new Etag for response caching
+func NewEtag() string {
+ now := fmt.Sprintf("%d", time.Now().Unix())
+ etag := base64.StdEncoding.EncodeToString([]byte(now))
+
+ return etag
+}
+
+// InvalidateCache sets a new Etag for http responses
+func InvalidateCache() error {
+ kv := make(map[string]interface{})
+
+ c, err := ConfigAll()
+ if err != nil {
+ return err
+ }
+
+ err = json.Unmarshal(c, &kv)
+ if err != nil {
+ return err
+ }
+
+ kv["etag"] = NewEtag()
+
+ data := make(url.Values)
+ for k, v := range kv {
+ switch v.(type) {
+ case string:
+ data.Set(k, v.(string))
+ case []string:
+ vv := v.([]string)
+ for i := range vv {
+ if i == 0 {
+ data.Set(k, vv[i])
+ } else {
+ data.Add(k, vv[i])
+ }
+ }
+ }
+ }
+
+ err = SetConfig(data)
+ if err != nil {
+
+ }
+
+ return nil
+}
diff --git a/system/db/config.go b/system/db/config.go
index ab1c720..b5a07e4 100644
--- a/system/db/config.go
+++ b/system/db/config.go
@@ -2,12 +2,9 @@ package db
import (
"bytes"
- "encoding/base64"
"encoding/json"
- "fmt"
"net/url"
"strings"
- "time"
"github.com/bosssauce/ponzu/system/admin/config"
@@ -124,11 +121,3 @@ func ConfigAll() ([]byte, error) {
func ConfigCache(key string) string {
return configCache.Get(key)
}
-
-// NewEtag generates a new Etag for response caching
-func NewEtag() string {
- now := fmt.Sprintf("%d", time.Now().Unix())
- etag := base64.StdEncoding.EncodeToString([]byte(now))
-
- return etag
-}
diff --git a/system/db/content.go b/system/db/content.go
index 74a77ec..19c31d7 100644
--- a/system/db/content.go
+++ b/system/db/content.go
@@ -77,6 +77,12 @@ func update(ns, id string, data url.Values) (int, error) {
go SortContent(ns)
}
+ // update changes data, so invalidate client caching
+ err = InvalidateCache()
+ if err != nil {
+ return 0, err
+ }
+
return cid, nil
}
@@ -132,6 +138,12 @@ func insert(ns string, data url.Values) (int, error) {
go SortContent(ns)
}
+ // insert changes data, so invalidate client caching
+ err = InvalidateCache()
+ if err != nil {
+ return 0, err
+ }
+
return effectedID, nil
}
@@ -149,6 +161,12 @@ func DeleteContent(target string) error {
return err
}
+ // delete changes data, so invalidate client caching
+ err = InvalidateCache()
+ if err != nil {
+ return err
+ }
+
// exception to typical "run in goroutine" pattern:
// we want to have an updated admin view as soon as this is deleted, so
// in some cases, the delete and redirect is faster than the sort,