feat: impl grpcweb in frontend

This commit is contained in:
Steven
2023-09-20 21:49:13 +08:00
parent 9e8de4644a
commit ebe54d1131
11 changed files with 469 additions and 26 deletions

View File

@ -8,9 +8,11 @@ import (
"github.com/boojack/slash/server/profile"
"github.com/boojack/slash/store"
grpcRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/labstack/echo/v4"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/reflection"
)
type APIV2Service struct {
@ -33,6 +35,7 @@ func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store
apiv2pb.RegisterUserServiceServer(grpcServer, NewUserService(secret, store))
apiv2pb.RegisterUserSettingServiceServer(grpcServer, NewUserSettingService(store))
apiv2pb.RegisterShortcutServiceServer(grpcServer, NewShortcutService(secret, store))
reflection.Register(grpcServer)
return &APIV2Service{
Secret: secret,
@ -75,5 +78,15 @@ func (s *APIV2Service) RegisterGateway(ctx context.Context, e *echo.Echo) error
}
e.Any("/api/v2/*", echo.WrapHandler(gwMux))
// GRPC web proxy.
options := []grpcweb.Option{
grpcweb.WithCorsForRegisteredEndpointsOnly(false),
grpcweb.WithOriginFunc(func(origin string) bool {
return true
}),
}
wrappedGrpc := grpcweb.WrapServer(s.grpcServer, options...)
e.Any("/slash.api.v2.*", echo.WrapHandler(wrappedGrpc))
return nil
}