diff options
author | Steve <nilslice@gmail.com> | 2017-01-02 11:58:56 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-02 11:58:56 -0800 |
commit | 6f0d53777bcc792099113c7dbd1e5f0b012be562 (patch) | |
tree | c148636f245bb1efd4ce5b1f597519038da9816c /system/api/push.go | |
parent | 806fdbe1e8839feb1bcc4e5e07aa7c144a429901 (diff) | |
parent | ae7d01f3aae28797f3b9ebc67be843763a02da6d (diff) |
Merge pull request #27 from ponzu-cms/ponzu-dev
[core] HTTP/2 Server Push for referenced items, Hideable interface
Diffstat (limited to 'system/api/push.go')
-rw-r--r-- | system/api/push.go | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/system/api/push.go b/system/api/push.go new file mode 100644 index 0000000..5db0a53 --- /dev/null +++ b/system/api/push.go @@ -0,0 +1,37 @@ +package api + +import ( + "log" + "net/http" + + "github.com/ponzu-cms/ponzu/system/item" + + "github.com/tidwall/gjson" +) + +func push(res http.ResponseWriter, req *http.Request, pt func() interface{}, data []byte) { + // Push(target string, opts *PushOptions) error + if pusher, ok := res.(http.Pusher); ok { + if p, ok := pt().(item.Pushable); ok { + // get fields to pull values from data + fields := p.Push() + + // parse values from data to push + values := gjson.GetManyBytes(data, fields...) + + // push all values from Pushable items' fields + for i := range values { + val := values[i] + val.ForEach(func(k, v gjson.Result) bool { + err := pusher.Push(req.URL.Path+v.String(), nil) + if err != nil { + log.Println("Error during Push of value:", v.String()) + } + + return true + }) + } + } + } + +} |