Mortar

gRPC-Gateway

Mortar comes with a gRPC-Gateway, which is a reverse-proxy that translates a RESTful HTTP API into gRPC. We will show how you should register its handlers, after you generate them from the proto files.

Register grpc-gateway Handlers

Before reading this part, get yourself familiar with the gRPC API counterpart.

If you’ve read the gRPC part, you simply need to add one function to Uber-FX graph. This function should return a slice of GRPCGatewayGeneratedHandlers.

  1. Register your grpc-gateway generated Handlers:

    Each handler function must register itself on the provided runtime.ServeMux

    func workshopGRPCGatewayHandlers() []serverInt.GRPCGatewayGeneratedHandlers {
        return []serverInt.GRPCGatewayGeneratedHandlers{
            // Register workshop REST API
            func(mux *runtime.ServeMux, endpoint string) error {
                return workshop.RegisterWorkshopHandlerFromEndpoint(context.Background(), mux, endpoint, []grpc.DialOption{grpc.WithInsecure()})
            },
            // Any additional gRPC gateway registrations should be called here
        }
    }
    
  2. Now tell Uber-FX about it and make sure it’s added to the GRPCGatewayGeneratedHandlers group.

    However, in this case our function doesn’t return a single value, but an array of them. By default Uber-FX will treat it as an Array of Arrays [][]GRPCGatewayGeneratedHandlers. Hence we need to flatten our value.

    // GRPC Gateway Generated Handlers registration
    fx.Provide(fx.Annotated{
     // "flatten" does this [][]serverInt.GRPCGatewayGeneratedHandlers -> []serverInt.GRPCGatewayGeneratedHandlers
     Group:  groups.GRPCGatewayGeneratedHandlers + ",flatten",
     Target: workshopGRPCGatewayHandlers,
    })
    
  3. Finally, you need to add the above fx.Option to Uber-FX graph, as shown here.

REST Enabled

You now have an RESTful reverse-proxy that automatically translates REST API calls to their gRPC counterparts. Following this guide, your REST API will be exposed on 5381 port.

POST /v1/workshop/cars HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 88
Content-Type: application/json
Host: localhost:5381

{
    "body_style": "HATCHBACK",
    "color": "blue",
    "number": "12345679",
    "owner": "me myself"
}


HTTP/1.1 200 OK
Content-Length: 2
Content-Type: application/json
Date: Thu, 21 Jan 2021 11:54:47 GMT
Grpc-Metadata-Content-Type: application/grpc

{}

As you can see above grpc-gateway did a great job.