| 40 | } |
| 41 | |
| 42 | func (h *Hub) SubscriptionsHandler(w http.ResponseWriter, r *http.Request) { |
| 43 | span, currentURL, lastEventID, subscribers, ok := h.initSubscription(w, r) |
| 44 | defer span.End() |
| 45 | |
| 46 | if !ok { |
| 47 | return |
| 48 | } |
| 49 | |
| 50 | w.WriteHeader(http.StatusOK) |
| 51 | |
| 52 | subscriptionCollection := subscriptionCollection{ |
| 53 | Context: jsonldContext, |
| 54 | ID: currentURL, |
| 55 | Type: "Subscriptions", |
| 56 | LastEventID: lastEventID, |
| 57 | Subscriptions: make([]subscription, 0), |
| 58 | } |
| 59 | |
| 60 | vars := mux.Vars(r) |
| 61 | |
| 62 | t, _ := url.QueryUnescape(vars["topic"]) |
| 63 | for _, subscriber := range subscribers { |
| 64 | subscriptionCollection.Subscriptions = append(subscriptionCollection.Subscriptions, subscriber.getSubscriptions(t, "", true)...) |
| 65 | } |
| 66 | |
| 67 | j, err := json.MarshalIndent(subscriptionCollection, "", " ") |
| 68 | if err != nil { |
| 69 | // Can't happen |
| 70 | panic(err) |
| 71 | } |
| 72 | |
| 73 | if _, err := w.Write(j); err != nil { |
| 74 | ctx := r.Context() |
| 75 | |
| 76 | if h.logger.Enabled(ctx, slog.LevelInfo) { |
| 77 | h.logger.LogAttrs(ctx, slog.LevelInfo, "Failed to write subscriptions response", slog.Any("error", err)) |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func (h *Hub) SubscriptionHandler(w http.ResponseWriter, r *http.Request) { |
| 83 | span, _, lastEventID, subscribers, ok := h.initSubscription(w, r) |