2021-09-17 23:39:15 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
2024-10-17 16:30:48 +00:00
|
|
|
"github.com/traefik/grpc-web/go/grpcweb"
|
2022-06-03 11:12:19 +00:00
|
|
|
|
2023-07-07 23:33:10 +00:00
|
|
|
"go.linka.cloud/grpc-toolkit/react"
|
2021-09-17 23:39:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2021-09-30 16:10:12 +00:00
|
|
|
h := grpcweb.WrapServer(s.server, append(defaultWebOptions, opts...)...)
|
2021-09-17 23:39:15 +00:00
|
|
|
for _, v := range grpcweb.ListGRPCResources(s.server) {
|
|
|
|
if s.opts.grpcWebPrefix != "" {
|
2023-07-07 23:29:18 +00:00
|
|
|
s.lazyMux().Handle(s.opts.grpcWebPrefix+v, http.StripPrefix(s.opts.grpcWebPrefix, h))
|
2021-09-17 23:39:15 +00:00
|
|
|
} else {
|
2023-07-07 23:29:18 +00:00
|
|
|
s.lazyMux().Handle(v, h)
|
2021-09-17 23:39:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2022-06-03 11:12:19 +00:00
|
|
|
|
|
|
|
func (s *service) reactApp() error {
|
|
|
|
if !s.opts.hasReactUI {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
h, err := react.NewHandler(s.opts.reactUI, s.opts.reactUISubPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-07-07 23:29:18 +00:00
|
|
|
s.lazyMux().Handle("/", h)
|
2022-06-03 11:12:19 +00:00
|
|
|
return nil
|
|
|
|
}
|