Skip to content

Commit

Permalink
add grpc-web gateways for REST access to Journal and Shard APIs
Browse files Browse the repository at this point in the history
Use the `github.com/grpc-ecosystem/grpc-gateway` project to
automatically generate REST handlers for portions of the Journals
and Shards gRPC services.

Also implement a CORS wrapper with configurable allowed origins.
Together these changes allow Gazette brokers and consumers to be
directly queried by browsers in cross-origin contexts.

Note that REST handlers automatically forward `Authorization: Bearer`
tokens into their corresponding gRPC handlers, so authorization checks
"just work".
  • Loading branch information
jgraettinger committed Sep 16, 2024
1 parent d0c9d56 commit dc5ad84
Show file tree
Hide file tree
Showing 11 changed files with 550 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ ci-release-gazette-examples-targets = \
# Targets of protobufs which must be compiled.
protobuf-targets = \
./broker/protocol/protocol.pb.go \
./broker/protocol/protocol.pb.gw.go \
./consumer/protocol/protocol.pb.go \
./consumer/protocol/protocol.pb.gw.go \
./consumer/recoverylog/recorded_op.pb.go \
./examples/word-count/word_count.pb.go

Expand Down
200 changes: 200 additions & 0 deletions broker/protocol/protocol.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions broker/protocol/protocol_gateway.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
type: google.api.Service
config_version: 3

http:
rules:
- selector: protocol.Journal.List
post: /v1/journals/list
body: "*"
- selector: protocol.Journal.Read
post: /v1/journals/read
body: "*"
9 changes: 9 additions & 0 deletions cmd/gazette/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"syscall"
"time"

"github.com/gogo/gateway"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/jessevdk/go-flags"
log "github.com/sirupsen/logrus"
"go.gazette.dev/core/allocator"
Expand Down Expand Up @@ -116,6 +118,13 @@ func (cmdServe) Execute(args []string) error {
signalCh = make(chan os.Signal, 1)
)
pb.RegisterJournalServer(srv.GRPCServer, pb.NewVerifiedJournalServer(service, verifier))

var mux *runtime.ServeMux = runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, new(gateway.JSONPb)),
runtime.WithProtoErrorHandler(runtime.DefaultHTTPProtoErrorHandler),
)
pb.RegisterJournalHandler(tasks.Context(), mux, srv.GRPCLoopback)
srv.HTTPMux.Handle("/v1/", Config.Broker.CORSWrapper(mux))
srv.HTTPMux.Handle("/", http_gateway.NewGateway(pb.NewRoutedJournalClient(lo, pb.NoopDispatchRouter{})))
ks.WatchApplyDelay = Config.Broker.WatchDelay

Expand Down
Loading

0 comments on commit dc5ad84

Please sign in to comment.