Git Product home page Git Product logo

Comments (14)

adamcohen avatar adamcohen commented on August 29, 2024 31

I used the details from grpc/grpc-go#555 for a workaround:

import (
  "context"
  "net/http"

  "golang.org/x/net/http2"
  "golang.org/x/net/http2/h2c"

  "github.com/grpc-ecosystem/grpc-gateway/runtime"

  "google.golang.org/grpc"
  "google.golang.org/grpc/credentials"

  pb "github.com/philips/grpc-gateway-example/echopb"
)

func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {
  return h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
      grpcServer.ServeHTTP(w, r)
    } else {
      otherHandler.ServeHTTP(w, r)
    }
  }), &http2.Server{})
}

func StartServer() {
  grpcServer := grpc.NewServer()
  pb.RegisterEchoServiceServer(grpcServer, newServer())

  mux := http.NewServeMux()
  gwmux := runtime.NewServeMux()

  ctx := context.Background()

  dopts := []grpc.DialOption{grpc.WithInsecure()}

  err = pb.RegisterEchoServiceHandlerFromEndpoint(ctx, gwmux, "localhost:8020", dopts)

  http.ListenAndServe(":8020", grpcHandlerFunc(grpcServer, mux))
}

from grpc-gateway-example.

mydnight avatar mydnight commented on August 29, 2024 3

@adamcohen
Thanks, I was encountering the same issue and it's been driving me nuts for the last day. This solution works wonderfully!

from grpc-gateway-example.

Stoakes avatar Stoakes commented on August 29, 2024 2

Hi,

I also had to get it working without TLS, so I adapted the code. You can find my version here: https://github.com/Stoakes/grpc-gateway-example

from grpc-gateway-example.

sstarcher avatar sstarcher commented on August 29, 2024

And to answer my question this is due to the http.Server upgrading to http2

from grpc-gateway-example.

sstarcher avatar sstarcher commented on August 29, 2024

That seems to be the case for http directly hitting the gateway, but I also get the same issue when using a grpc client which should already be http2.

from grpc-gateway-example.

JesseYan avatar JesseYan commented on August 29, 2024

The same problem, anyone can explain how to fix it in other ways?

from grpc-gateway-example.

cy-zheng avatar cy-zheng commented on August 29, 2024

Same question, is there any way to run grpc server and grpc gateway in a port without tls?

from grpc-gateway-example.

nikkolasg avatar nikkolasg commented on August 29, 2024

So youre version use cmux apparently and set back regular HTTP connection to use HTTP 1 for non-gRPC connections.
While that's a working solution, that does not solve the udnerlying problem...

I've dig up a little bit by re-creating a small example and what I found is that in the grpcHandlerFunc , the function never enters the "grpc section" because the content type is empty while it should be "application/grpc" ).

After that, I tried to run a basic basic gRPC example starting from a http.ServeMux but I could not make it work, even when explicitely using http2.

Any ideas ?

from grpc-gateway-example.

nikkolasg avatar nikkolasg commented on August 29, 2024

Just for reference everything is explained here grpc/grpc-go#555

In short, it's not directly possible for now and there's a few options described in this post (including cmux's solution like @Stoakes ): https://medium.com/@thatcher/why-choose-between-grpc-and-rest-bc0d351f2f84

from grpc-gateway-example.

 avatar commented on August 29, 2024
-   mux := http.NewServeMux()
  gwmux := runtime.NewServeMux()

  ctx := context.Background()

  dopts := []grpc.DialOption{grpc.WithInsecure()}

  err = pb.RegisterEchoServiceHandlerFromEndpoint(ctx, gwmux, "localhost:8020", dopts)

-   http.ListenAndServe(":8020", grpcHandlerFunc(grpcServer, mux))
+   http.ListenAndServe(":8020", grpcHandlerFunc(grpcServer, gwmux))

Hey @adamcohen, as per my example above, why do we need the mux here? Couldn't we just use gwmux?

from grpc-gateway-example.

adamcohen avatar adamcohen commented on August 29, 2024

Hey @adamcohen, as per my example above, why do we need the mux here? Couldn't we just use gwmux?

@kadenlnelson possibly - I think there was a reason why I needed the separate mux, but I can't remember now.

from grpc-gateway-example.

 avatar commented on August 29, 2024

Hey @adamcohen, as per my example above, why do we need the mux here? Couldn't we just use gwmux?

@kadenlnelson possibly - I think there was a reason why I needed the separate mux, but I can't remember now.

I think the mux is used for serving the swagger ui

from grpc-gateway-example.

Azuka avatar Azuka commented on August 29, 2024

Tried using this with grpc gateway, but paths that take in a body seem to be losing the request body somewhere downstream. Anyone else experiencing this?

from grpc-gateway-example.

amit-chandak-unskript avatar amit-chandak-unskript commented on August 29, 2024

Tried using this with grpc gateway, but paths that take in a body seem to be losing the request body somewhere downstream. Anyone else experiencing this?

Works fine for me. I have tried a POST endpoint with payload in the body.

from grpc-gateway-example.

Related Issues (18)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.