MCPcopy
hub / github.com/dunglas/mercure / SubscriptionsHandler

Method SubscriptionsHandler

subscription.go:42–80  ·  view source on GitHub ↗
(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

40}
41
42func (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
82func (h *Hub) SubscriptionHandler(w http.ResponseWriter, r *http.Request) {
83 span, _, lastEventID, subscribers, ok := h.initSubscription(w, r)

Calls 5

initSubscriptionMethod · 0.95
getSubscriptionsMethod · 0.80
EnabledMethod · 0.80
WriteHeaderMethod · 0.45
WriteMethod · 0.45