diff options
| author | bndw <ben@bdw.to> | 2026-02-13 18:26:53 -0800 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2026-02-13 18:26:53 -0800 |
| commit | 83876eae868bd1e4fb6b9a823a6e8173919f290d (patch) | |
| tree | f754bcb8b10337db34f6f36ba3d094e53c1bb808 /internal/handler/connect/handler.go | |
| parent | 3481c3273f8764bd0a0ab51183dc57f592fb616c (diff) | |
feat: add Connect (gRPC over HTTP/JSON) support
Connect integration:
- Buf Connect codegen added to buf.gen.yaml
- Connect handler wraps gRPC server
- Serves on same port as WebSocket (:8080)
- HTTP/2 with h2c for cleartext HTTP/2
Now serving THREE protocols:
1. gRPC (native) on :50051 - binary, high performance
2. Connect on :8080/nostr.v1.NostrRelay/* - HTTP/JSON, browser compatible
3. WebSocket on :8080/ - Nostr standard protocol
All three protocols share:
- Same storage layer
- Same subscription manager
- Same validation logic
Browser-friendly! Call gRPC methods with fetch() or curl.
Diffstat (limited to 'internal/handler/connect/handler.go')
| -rw-r--r-- | internal/handler/connect/handler.go | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/internal/handler/connect/handler.go b/internal/handler/connect/handler.go new file mode 100644 index 0000000..f33e4fc --- /dev/null +++ b/internal/handler/connect/handler.go | |||
| @@ -0,0 +1,101 @@ | |||
| 1 | package connect | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | |||
| 6 | "connectrpc.com/connect" | ||
| 7 | "google.golang.org/grpc/metadata" | ||
| 8 | |||
| 9 | pb "northwest.io/nostr-grpc/api/nostr/v1" | ||
| 10 | "northwest.io/nostr-grpc/api/nostr/v1/nostrv1connect" | ||
| 11 | grpchandler "northwest.io/nostr-grpc/internal/handler/grpc" | ||
| 12 | ) | ||
| 13 | |||
| 14 | type Handler struct { | ||
| 15 | grpcServer *grpchandler.Server | ||
| 16 | } | ||
| 17 | |||
| 18 | func NewHandler(grpcServer *grpchandler.Server) *Handler { | ||
| 19 | return &Handler{grpcServer: grpcServer} | ||
| 20 | } | ||
| 21 | |||
| 22 | func (h *Handler) PublishEvent(ctx context.Context, req *connect.Request[pb.PublishEventRequest]) (*connect.Response[pb.PublishEventResponse], error) { | ||
| 23 | resp, err := h.grpcServer.PublishEvent(ctx, req.Msg) | ||
| 24 | if err != nil { | ||
| 25 | return nil, err | ||
| 26 | } | ||
| 27 | return connect.NewResponse(resp), nil | ||
| 28 | } | ||
| 29 | |||
| 30 | func (h *Handler) Subscribe(ctx context.Context, req *connect.Request[pb.SubscribeRequest], stream *connect.ServerStream[pb.Event]) error { | ||
| 31 | return h.grpcServer.Subscribe(req.Msg, &subscribeStreamAdapter{stream: stream, ctx: ctx}) | ||
| 32 | } | ||
| 33 | |||
| 34 | func (h *Handler) Unsubscribe(ctx context.Context, req *connect.Request[pb.UnsubscribeRequest]) (*connect.Response[pb.Empty], error) { | ||
| 35 | resp, err := h.grpcServer.Unsubscribe(ctx, req.Msg) | ||
| 36 | if err != nil { | ||
| 37 | return nil, err | ||
| 38 | } | ||
| 39 | return connect.NewResponse(resp), nil | ||
| 40 | } | ||
| 41 | |||
| 42 | func (h *Handler) PublishBatch(ctx context.Context, req *connect.Request[pb.PublishBatchRequest]) (*connect.Response[pb.PublishBatchResponse], error) { | ||
| 43 | resp, err := h.grpcServer.PublishBatch(ctx, req.Msg) | ||
| 44 | if err != nil { | ||
| 45 | return nil, err | ||
| 46 | } | ||
| 47 | return connect.NewResponse(resp), nil | ||
| 48 | } | ||
| 49 | |||
| 50 | func (h *Handler) QueryEvents(ctx context.Context, req *connect.Request[pb.QueryRequest]) (*connect.Response[pb.QueryResponse], error) { | ||
| 51 | resp, err := h.grpcServer.QueryEvents(ctx, req.Msg) | ||
| 52 | if err != nil { | ||
| 53 | return nil, err | ||
| 54 | } | ||
| 55 | return connect.NewResponse(resp), nil | ||
| 56 | } | ||
| 57 | |||
| 58 | func (h *Handler) CountEvents(ctx context.Context, req *connect.Request[pb.CountRequest]) (*connect.Response[pb.CountResponse], error) { | ||
| 59 | resp, err := h.grpcServer.CountEvents(ctx, req.Msg) | ||
| 60 | if err != nil { | ||
| 61 | return nil, err | ||
| 62 | } | ||
| 63 | return connect.NewResponse(resp), nil | ||
| 64 | } | ||
| 65 | |||
| 66 | type subscribeStreamAdapter struct { | ||
| 67 | stream *connect.ServerStream[pb.Event] | ||
| 68 | ctx context.Context | ||
| 69 | } | ||
| 70 | |||
| 71 | func (s *subscribeStreamAdapter) Send(event *pb.Event) error { | ||
| 72 | return s.stream.Send(event) | ||
| 73 | } | ||
| 74 | |||
| 75 | func (s *subscribeStreamAdapter) Context() context.Context { | ||
| 76 | return s.ctx | ||
| 77 | } | ||
| 78 | |||
| 79 | func (s *subscribeStreamAdapter) SetHeader(md metadata.MD) error { | ||
| 80 | return nil | ||
| 81 | } | ||
| 82 | |||
| 83 | func (s *subscribeStreamAdapter) SendHeader(md metadata.MD) error { | ||
| 84 | return nil | ||
| 85 | } | ||
| 86 | |||
| 87 | func (s *subscribeStreamAdapter) SetTrailer(md metadata.MD) { | ||
| 88 | } | ||
| 89 | |||
| 90 | func (s *subscribeStreamAdapter) SendMsg(m any) error { | ||
| 91 | if event, ok := m.(*pb.Event); ok { | ||
| 92 | return s.Send(event) | ||
| 93 | } | ||
| 94 | return nil | ||
| 95 | } | ||
| 96 | |||
| 97 | func (s *subscribeStreamAdapter) RecvMsg(m any) error { | ||
| 98 | return nil | ||
| 99 | } | ||
| 100 | |||
| 101 | var _ nostrv1connect.NostrRelayHandler = (*Handler)(nil) | ||
