gin
gin copied to clipboard
Can gin handle gRPC
Excuse me!
What I wanted was that Gin could handle both HTTP and gRPC, either directly with GIN, or through an additional agent, such as the agent listening on the port, identifying different data and forwarding it to different handlers.
For example, this case is the same, but it USES native HTTP, not GIN. go-grpc-example
Something like this:
http.ListenAndServeTLS(":"+PORT,
certFile,
keyFile,
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {
server.ServeHTTP(w, r)
} else {
mux.ServeHTTP(w, r)
}
return
}),
)
If so, can you help provide an example?
I stumbled across the same question and found my solution here: https://ahmet.im/blog/grpc-http-mux-go/
Instead of httpMux := http.NewServeMux()
just use gin.Default()
Any solutions based on cmux didn't work for me due to the "Different Protocols on The Same Connection" limitation of cmux.