Vanguard adds REST to connect-go
services. Using Google's HTTP transcoding options,
it maps each RPC to an HTTP method, URL path, and JSON body from its strongly
typed Protobuf definition. connect-go's connecthttp package serves the
Connect, gRPC, and gRPC-Web protocols from the same *connect.Server, so one
set of handlers is reachable from all four.
-
REST for RPC services: Add
google.api.httpannotations and your RPC handlers also serve REST clients. This is especially handy during a migration from a REST API to a schema-driven RPC API: existing REST clients keep working while your server implementations move to Protobuf and RPC. -
No extra code generation: Unlike gRPC-Gateway, Vanguard runs inside your Go server and needs no generated gateway code. It works from Protobuf descriptors at runtime, so service definitions can come from configuration, schema registries, or gRPC Server Reflection. That makes it a good fit for proxies, which don't need to be recompiled and redeployed each time an RPC service schema changes.
-
RPC clients for REST servers: The same annotations let generated Connect clients call REST APIs. Teams can adopt RPC, for example in web or mobile clients, before every backend service has migrated.
-
Existing gRPC services:
vanguardgrpcregisters grpc-go service implementations on a*connect.Server.connecthttpthen serves them to Connect and gRPC-Web clients and Vanguard serves them over REST, without rewriting your handlers.
Vanguard builds on connect-go v2.
Services register with a *connect.Server, and transports expose the server
on the wire. The connecthttp package (part of connect-go) serves the Connect,
gRPC, and gRPC-Web protocols. vanguard adds the REST half, driven by the
google.api.http annotations on your service's Protobuf schema.
Register your services with a *connect.Server, then mount vanguard on an
http.ServeMux alongside connecthttp:
server := connect.NewServer()
pingv1connect.RegisterPingServiceHandler(server, &pingServer{})
mux := http.NewServeMux()
connecthttp.Mount(mux, server) // Connect, gRPC, gRPC-Web
if err := vanguard.Mount(mux, server); err != nil { // google.api.http
log.Fatal(err)
}
log.Fatal(http.ListenAndServe(":8080", mux))Methods whose descriptors carry a google.api.http annotation become REST
endpoints. Methods without one are only reachable via the RPC protocols. For
services that don't define the mapping in their Protobuf sources, supply rules
externally with vanguard.WithRules.
vanguard installs one catch-all "/" handler rather than per-pattern routes:
google.api.http templates support **, single-segment *, and :verb
suffixes that net/http's pattern matcher does not. Mount vanguard on a
sub-mux (e.g. mux.Handle("/api/", subMux)) to scope its catch-all.
Dispatch flows through connect.Server.Call, so interceptors registered on
the server fire for REST requests just like for RPC requests.
vanguard.NewTransport returns a connect.Transport that translates each
RPC into the REST request described by the method's google.api.http
annotation. Generated Connect clients work unchanged:
transport, err := vanguard.NewTransport(http.DefaultClient, "https://api.example.com")
if err != nil {
log.Fatal(err)
}
client := pingv1connect.NewPingServiceClient(connect.NewClient(transport))
resp, err := client.Ping(ctx, &pingv1.PingRequest{Number: 42})vanguard.ForwardService registers methods that relay each call to an
upstream *connect.Client, so you can put REST in front of a backend without
its generated code. Only the service descriptor is needed:
upstream := connect.NewClient(connecthttp.NewTransport(http.DefaultClient, backendURL))
server := connect.NewServer()
server.Register(vanguard.ForwardService(upstream, serviceDescriptor)...)
mux := http.NewServeMux()
if err := vanguard.Mount(mux, server); err != nil {
log.Fatal(err)
}The generated Go code for gRPC does not include a handler factory like Connect
uses. Its generated functions instead register handler information with a
grpc.ServiceRegistrar. The vanguardgrpc package provides a registrar that
adapts those stubs onto a *connect.Server:
server := connect.NewServer()
registrar := vanguardgrpc.NewServiceRegistrar(server)
// Existing protoc-gen-go-grpc-generated code:
pingv1grpc.RegisterPingServiceServer(registrar, &pingServer{})
mux := http.NewServeMux()
connecthttp.Mount(mux, server) // Connect, gRPC, gRPC-Web
if err := vanguard.Mount(mux, server); err != nil { // google.api.http
log.Fatal(err)
}Vanguard is undergoing initial development and is not yet stable.
Offered under the Apache 2 license.