Skip to content

feat: add HTTP/REST gateway via grpc-gateway#21

Merged
rbaliyan merged 1 commit into
mainfrom
feat/http-gateway
Apr 24, 2026
Merged

feat: add HTTP/REST gateway via grpc-gateway#21
rbaliyan merged 1 commit into
mainfrom
feat/http-gateway

Conversation

@rbaliyan
Copy link
Copy Markdown
Owner

Summary

  • Annotates all 11 LedgerService RPC methods with google.api.http options; REST URLs follow POST /v1/streams/{stream}/entries, GET /v1/streams/{stream}/entries, etc.
  • New internal/server/gateway.go dials the local gRPC server and mounts a runtime.ServeMux; x-ledger-store and x-api-key HTTP headers are forwarded as gRPC metadata so existing auth interceptors handle authentication unchanged
  • http_listen config field (and --http-addr CLI flag) enables the gateway — empty means disabled, so existing deployments are unaffected
  • When gRPC TLS is configured, the gateway uses the same CA cert (or falls back to the server cert itself for self-signed setups)
  • Stop() calls httpServer.Shutdown(ctx) before GracefulStop(), preserving the existing shutdown order

Test plan

  • TestGatewayHealthGET /v1/health returns {"status":"ok"}
  • TestGatewayAppendRead — append via POST, read back via GET, verify entry round-trip
  • TestGatewayAPIKeyAuth — requests without x-api-key return 401; valid key returns 200
  • All existing tests pass, golangci-lint clean, gosec 0 issues

Adds an optional HTTP/REST gateway that translates JSON-over-HTTP requests
to gRPC calls, making all LedgerService methods accessible without a gRPC
client.

- Proto: annotate all 11 RPC methods with google.api.http options
  (POST/GET/PUT/PATCH/DELETE mapped to REST-idiomatic URLs under /v1/streams)
- buf.yaml: add buf.build/googleapis/googleapis dependency
- buf.gen.yaml: add buf.build/grpc-ecosystem/gateway plugin
- api/ledger/v1/ledger.pb.gw.go: generated grpc-gateway handler registration
- internal/server/gateway.go: newGatewayHandler, header matcher (x-ledger-store,
  x-api-key), and TLS-aware dial options for the loopback gRPC connection
- internal/server/server.go: optional HTTP server wired alongside gRPC;
  Serve() starts HTTP in a goroutine, Stop() shuts it down gracefully
- internal/config/config.go: HTTPListen field (empty = gateway disabled)
- internal/cli/root.go: --http-addr flag
- internal/cli/start.go: propagate --http-addr to background process;
  writeDefaultConfig includes http_listen comment
- Tests: TestGatewayHealth, TestGatewayAppendRead, TestGatewayAPIKeyAuth
@rbaliyan rbaliyan merged commit 73415ed into main Apr 24, 2026
10 checks passed
@rbaliyan rbaliyan deleted the feat/http-gateway branch April 24, 2026 05:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant