How to config public route and auth route in gRPC gateway

188 views Asked by At

Is there any way to config public route and auth route in gRPC gateway similar to other http frameworks. For example in echo framework we can config like this

func main() {
    e := echo.New()
    authRoute := e.Group("/auth", middlewares.Verify)
    publicRoute := e.Group("/")
    // ...
}

in grpc-gateway, I have config like this.

func run() error {
  ctx := context.Background()
  ctx, cancel := context.WithCancel(ctx)
  defer cancel()

  // Register gRPC server endpoint
  // Note: Make sure the gRPC server is running properly and accessible
  mux := runtime.NewServeMux()
  opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
  err := gw.RegisterYourServiceHandlerFromEndpoint(ctx, mux,  *grpcServerEndpoint, opts)
  if err != nil {
    return err
  }

  // Start HTTP server (and proxy calls to gRPC server endpoint)
  return http.ListenAndServe(":8081", mux)
}

Is there any way to make mux with authenticated decorator function for specific routes. And function RegisterYourServiceHandlerFromEndpoint will register it

authenMux := runtime.NewServeMux()
// make it with authenticated func
//....

err := gw.RegisterYourServiceHandlerFromEndpoint(ctx, authenMux,  *grpcServerEndpoint, opts)

1

There are 1 answers

1
Tony On

I'm unsure about your specific goal, but if you only want to customize the gRPC-gateway, you can follow this approach. Alternatively, you can transfer the logic to a separate function.

// run starts the HTTP server and sets up a custom gRPC-gateway route.
// It registers the gRPC server endpoint and a custom POST route ("/auth").
func run() error {
    // Create a background context and cancel function for graceful shutdown
    ctx := context.Background()
    ctx, cancel := context.WithCancel(ctx)
    defer cancel()

    // Create a gRPC-gateway multiplexer
    mux := runtime.NewServeMux()

    // Register gRPC server endpoint with insecure transport credentials
    opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
    err := gw.RegisterYourServiceHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts)
    if err != nil {
        return err
    }

    // Handle custom POST route ("/auth")
    err = mux.HandlePath("POST", "/auth", func(w http.ResponseWriter, r *http.Request, pathParams map[string]string) {
        // Create a map with the desired data
        responseData := map[string]string{
            "data": "something",
        }

        // Convert the map to a JSON string
        resp, err := json.Marshal(responseData)
        if err != nil {
            http.Error(w, "Error encoding JSON", http.StatusInternalServerError)
            return
        }

        // Set the Content-Type header to application/json
        w.Header().Set("Content-Type", "application/json")

        // Write the JSON response to the response writer
        w.Write(resp)
    })

    if err != nil {
        return err
    }

    // Start HTTP server (and proxy calls to gRPC server endpoint)
    return http.ListenAndServe(":8081", mux)
}