summaryrefslogtreecommitdiff
path: root/system/api/push.go
blob: b7c5642b619db0de9a334307b6198198eff3308d (plain)
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
30
31
32
33
34
35
36
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(v.String(), nil)
					if err != nil {
						log.Println("Error during Push of value:", v.String())
					}

					return true
				})
			}
		}
	}

}