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

Method registerSubscriber

subscribe.go:199–287  ·  view source on GitHub ↗

registerSubscriber initializes the connection.

(ctx context.Context, w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

197
198// registerSubscriber initializes the connection.
199func (h *Hub) registerSubscriber(ctx context.Context, w http.ResponseWriter, r *http.Request) (*LocalSubscriber, *responseController) { //nolint:funlen
200 ctx, span := startSpan(ctx, "mercure.subscribe", trace.WithSpanKind(trace.SpanKindConsumer))
201 defer span.End()
202
203 s := NewLocalSubscriber(h.retrieveLastEventID(ctx, r), h.logger, h.topicSelectorStore)
204
205 var (
206 privateTopics []string
207 claims *claims
208 )
209
210 if h.subscriberJWTKeyFunc != nil { //nolint:nestif
211 var err error
212
213 claims, err = h.authorize(r, false)
214 if claims != nil {
215 s.Claims = claims
216 privateTopics = claims.Mercure.Subscribe
217 }
218
219 if err != nil || (claims == nil && !h.anonymous) {
220 http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
221
222 if h.logger.Enabled(ctx, slog.LevelDebug) {
223 h.logger.LogAttrs(ctx, slog.LevelDebug, "Subscriber unauthorized", slog.Any("error", err))
224 }
225
226 if err != nil {
227 recordSpanError(span, err)
228 }
229
230 return nil, nil
231 }
232 }
233
234 topics := r.URL.Query()["topic"]
235 if len(topics) == 0 {
236 http.Error(w, `Missing "topic" parameter.`, http.StatusBadRequest)
237
238 return nil, nil
239 }
240
241 if len(topics) > maxQueryTopics {
242 http.Error(w, `Too many "topic" parameters.`, http.StatusBadRequest)
243
244 return nil, nil
245 }
246
247 s.SetTopics(topics, privateTopics)
248
249 if span.IsRecording() {
250 span.SetAttributes(
251 attribute.String("mercure.subscriber.id", s.ID),
252 attribute.StringSlice("mercure.topics", topics),
253 )
254 }
255
256 addCtx := context.WithoutCancel(ctx)

Callers 1

SubscribeHandlerMethod · 0.95

Calls 15

retrieveLastEventIDMethod · 0.95
authorizeMethod · 0.95
sendHeadersMethod · 0.95
newResponseControllerMethod · 0.95
startSpanFunction · 0.85
NewLocalSubscriberFunction · 0.85
recordSpanErrorFunction · 0.85
ErrorMethod · 0.80
EnabledMethod · 0.80
SetTopicsMethod · 0.80
StringMethod · 0.80

Tested by

no test coverage detected