diff options
Diffstat (limited to 'system')
-rw-r--r-- | system/api/handlers.go | 31 | ||||
-rw-r--r-- | system/api/push.go | 36 |
2 files changed, 38 insertions, 29 deletions
diff --git a/system/api/handlers.go b/system/api/handlers.go index a152301..a01f3dc 100644 --- a/system/api/handlers.go +++ b/system/api/handlers.go @@ -11,7 +11,6 @@ import ( "github.com/ponzu-cms/ponzu/system/api/analytics" "github.com/ponzu-cms/ponzu/system/db" "github.com/ponzu-cms/ponzu/system/item" - "github.com/tidwall/gjson" ) func typesHandler(res http.ResponseWriter, req *http.Request) { @@ -99,7 +98,8 @@ func contentHandler(res http.ResponseWriter, req *http.Request) { return } - if pt, ok := item.Types[t]; !ok { + pt, ok := item.Types[t] + if !ok { res.WriteHeader(http.StatusNotFound) return } @@ -183,33 +183,6 @@ func toJSON(data []string) ([]byte, error) { return buf.Bytes(), nil } -func push(res http.ResponseWriter, 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(v.String(), nil) - if err != nil { - log.Println("Error during Push of value:", v.String()) - } - - return true - }) - } - } - } - -} - // sendData() should be used any time you want to communicate // data back to a foreign client func sendData(res http.ResponseWriter, data []byte, code int) { diff --git a/system/api/push.go b/system/api/push.go new file mode 100644 index 0000000..64eb5b0 --- /dev/null +++ b/system/api/push.go @@ -0,0 +1,36 @@ +package api + +import ( + "log" + + "github.com/ponzu-cms/ponzu/system/item" + + "github.com/tidwall/gjson" +) + +func push(res http.ResponseWriter, 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(v.String(), nil) + if err != nil { + log.Println("Error during Push of value:", v.String()) + } + + return true + }) + } + } + } + +} |