Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] possible without tls #22

Open
sstarcher opened this issue Oct 12, 2017 · 14 comments
Open

[Question] possible without tls #22

sstarcher opened this issue Oct 12, 2017 · 14 comments

Comments

@sstarcher
Copy link

I'm attempting to terminate tls before hitting the grpc/rest gateway so I disabled the TLS certs in the code base and turned on grpc.WithInsecure, but I always end up with
{"error":"transport is closing","code":14}%

@sstarcher
Copy link
Author

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

@sstarcher
Copy link
Author

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.

@JesseYan
Copy link

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

@cy-zheng
Copy link

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

@Stoakes
Copy link

Stoakes commented Apr 30, 2018

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

@nikkolasg
Copy link

nikkolasg commented Jun 14, 2018

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 ?

@nikkolasg
Copy link

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

@adamcohen
Copy link

adamcohen commented May 9, 2019

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))
}

@mydnight
Copy link

mydnight commented May 24, 2019

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

@ghost
Copy link

ghost commented Oct 9, 2020

-   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?

@adamcohen
Copy link

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.

@ghost
Copy link

ghost commented Oct 14, 2020

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

@Azuka
Copy link

Azuka commented Nov 1, 2020

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?

@amit-chandak-unskript
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants