events: fix filters not handling empty filter query params
Some checks failed
/ push (push) Failing after 13m15s

This commit is contained in:
Tigor Hutasuhut 2024-06-10 23:36:26 +07:00
parent 8e45faba28
commit a648fa745a
3 changed files with 15 additions and 6 deletions

View file

@ -20,7 +20,10 @@ func (handler *Handler) HTMXEvents(rw http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(rw).Encode(map[string]string{"error": "response writer does not support streaming"})
return
}
filters := strings.Split(r.URL.Query().Get("filter"), ",")
var filters []string
if q := r.URL.Query().Get("filter"); q != "" {
filters = strings.Split(q, ",")
}
log.New(ctx).Info("new htmx event stream connection", "user_agent", r.UserAgent())
rw.Header().Set("Content-Type", "text/event-stream")
@ -36,7 +39,7 @@ loop:
for {
select {
case <-r.Context().Done():
log.New(ctx).Info("simple event stream connection closed", "user_agent", r.UserAgent())
log.New(ctx).Info("HTMX event stream connection closed", "user_agent", r.UserAgent())
return
case event := <-ev:
msg := event.Event()

View file

@ -20,9 +20,12 @@ func (handler *Handler) JSONEvents(rw http.ResponseWriter, r *http.Request) {
_ = json.NewEncoder(rw).Encode(map[string]string{"error": "response writer does not support streaming"})
return
}
filters := strings.Split(r.URL.Query().Get("filter"), ",")
var filters []string
if q := r.URL.Query().Get("filter"); q != "" {
filters = strings.Split(q, ",")
}
log.New(ctx).Info("new htmx event stream connection", "user_agent", r.UserAgent())
log.New(ctx).Info("new json event stream connection", "user_agent", r.UserAgent())
rw.Header().Set("Content-Type", "text/event-stream")
rw.Header().Set("Cache-Control", "no-cache")
rw.Header().Set("Connection", "keep-alive")
@ -36,7 +39,7 @@ loop:
for {
select {
case <-r.Context().Done():
log.New(ctx).Info("simple event stream connection closed", "user_agent", r.UserAgent())
log.New(ctx).Info("json event stream connection closed", "user_agent", r.UserAgent())
return
case event := <-ev:
msg := event.Event()

View file

@ -26,7 +26,10 @@ func (handler *Handler) SimpleEvents(rw http.ResponseWriter, r *http.Request) {
return
}
filters := strings.Split(r.URL.Query().Get("filter"), ",")
var filters []string
if q := r.URL.Query().Get("filter"); q != "" {
filters = strings.Split(q, ",")
}
log.New(ctx).Info("new simple event stream connection", "user_agent", r.UserAgent())