HandleMessage is the single entry point.
(ctx context.Context, eventID string, data []byte)
| 68 | |
| 69 | // HandleMessage is the single entry point. |
| 70 | func (r *Router) HandleMessage(ctx context.Context, eventID string, data []byte) error { |
| 71 | // 1. Efficient Peek: We only parse the metadata fields. |
| 72 | // The rest of the JSON is captured as RawMessage without full decoding. |
| 73 | var env envelope |
| 74 | if err := json.Unmarshal(data, &env); err != nil { |
| 75 | return fmt.Errorf("%w: %w: %w", ErrUnprocessableEntity, ErrInvalidEnvelope, err) |
| 76 | } |
| 77 | |
| 78 | // 2. Find matching route |
| 79 | for _, route := range r.routes { |
| 80 | if route.Matches(env.Kind, env.APIVersion) { |
| 81 | // 3. Dispatch using only the inner 'data' payload |
| 82 | |
| 83 | return route.Dispatch(ctx, eventID, env.Data) |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return fmt.Errorf("%w: %w: kind=%q version=%q", ErrUnprocessableEntity, ErrNoHandler, env.Kind, env.APIVersion) |
| 88 | } |
| 89 | |
| 90 | // envelope is used for the initial peek. |
| 91 | type envelope struct { |