diff options
Diffstat (limited to 'internal/handler/events.go')
-rw-r--r-- | internal/handler/events.go | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/internal/handler/events.go b/internal/handler/events.go index 299971d..43de3c4 100644 --- a/internal/handler/events.go +++ b/internal/handler/events.go @@ -1,20 +1,22 @@ package handler import ( + "encoding/json" "fmt" "net/http" "strings" "github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5/middleware" - dmodel "go.neonxp.dev/djson/internal/model" + + "go.neonxp.dev/djson/internal/command" ) func (h *handler) HandleEvents(r chi.Router) { r.Route("/sse", func(r chi.Router) { r.Get("/*", func(w http.ResponseWriter, r *http.Request) { - h.receiveEvents(w, r, func(ev *dmodel.Mutation) { - message, _ := ev.Body.MarshalJSON() + h.receiveEvents(w, r, func(ev *command.Mutation) { + message, _ := json.Marshal(ev.Data) fmt.Fprintf( w, `event: %s\ndata: {"path":"%s","data":%s}\n\n`, @@ -27,8 +29,8 @@ func (h *handler) HandleEvents(r chi.Router) { }) r.Route("/json", func(r chi.Router) { r.Get("/*", func(w http.ResponseWriter, r *http.Request) { - h.receiveEvents(w, r, func(ev *dmodel.Mutation) { - message, _ := ev.Body.MarshalJSON() + h.receiveEvents(w, r, func(ev *command.Mutation) { + message, _ := json.Marshal(ev.Data) fmt.Fprintf( w, `{"event":"%s","path":"%s","data":"%s"}\n`, @@ -44,7 +46,7 @@ func (h *handler) HandleEvents(r chi.Router) { func (h *handler) receiveEvents( w http.ResponseWriter, r *http.Request, - render func(ev *dmodel.Mutation), + render func(ev *command.Mutation), ) { flusher := w.(http.Flusher) w.Header().Set("Content-Type", "application/x-ndjson") @@ -53,7 +55,7 @@ func (h *handler) receiveEvents( w.Header().Set("Access-Control-Allow-Origin", "*") rctx := chi.RouteContext(r.Context()) path := parsePath(rctx.RoutePath) - ch := make(chan dmodel.Mutation) + ch := make(chan command.Mutation) reqID := middleware.GetReqID(r.Context()) h.events.Subscribe(path, reqID, ch) defer h.events.Unsubscribe(path, reqID) |