mirror of
https://github.com/linka-cloud/grpc.git
synced 2025-06-22 09:12:28 +00:00
remove transport draft, add grpc web and gateway support
This commit is contained in:
35
service/web.go
Normal file
35
service/web.go
Normal file
@ -0,0 +1,35 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/improbable-eng/grpc-web/go/grpcweb"
|
||||
)
|
||||
|
||||
var defaultWebOptions = []grpcweb.Option{
|
||||
grpcweb.WithWebsockets(true),
|
||||
grpcweb.WithWebsocketOriginFunc(func(req *http.Request) bool {
|
||||
return true
|
||||
}),
|
||||
grpcweb.WithCorsForRegisteredEndpointsOnly(false),
|
||||
grpcweb.WithOriginFunc(func(origin string) bool {
|
||||
return true
|
||||
}),
|
||||
grpcweb.WithWebsocketPingInterval(time.Second),
|
||||
}
|
||||
|
||||
func (s *service) grpcWeb(opts ...grpcweb.Option) error {
|
||||
if !s.opts.grpcWeb {
|
||||
return nil
|
||||
}
|
||||
h := grpcweb.WrapServer(s.server, opts...)
|
||||
for _, v := range grpcweb.ListGRPCResources(s.server) {
|
||||
if s.opts.grpcWebPrefix != "" {
|
||||
s.mux.Handle(s.opts.grpcWebPrefix+v, http.StripPrefix(s.opts.grpcWebPrefix, h))
|
||||
} else {
|
||||
s.mux.Handle(v, h)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
Reference in New Issue
Block a user