registerSubscriber initializes the connection.
(ctx context.Context, w http.ResponseWriter, r *http.Request)
| 197 | |
| 198 | // registerSubscriber initializes the connection. |
| 199 | func (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) |
no test coverage detected