refactor: impl part of grpcweb

This commit is contained in:
Steven
2023-09-17 19:20:03 +08:00
parent d5c1706e9c
commit 72ca4e74ee
20 changed files with 3325 additions and 26 deletions

View File

@@ -5,6 +5,7 @@ import (
"fmt"
grpcRuntime "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/improbable-eng/grpc-web/go/grpcweb"
"github.com/labstack/echo/v4"
apiv2pb "github.com/usememos/memos/proto/gen/api/v2"
"github.com/usememos/memos/server/profile"
@@ -24,6 +25,7 @@ type APIV2Service struct {
}
func NewAPIV2Service(secret string, profile *profile.Profile, store *store.Store, grpcServerPort int) *APIV2Service {
grpc.EnableTracing = true
authProvider := NewGRPCAuthInterceptor(store, secret)
grpcServer := grpc.NewServer(
grpc.ChainUnaryInterceptor(
@@ -81,5 +83,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("/memos.api.v2.*", echo.WrapHandler(wrappedGrpc))
return nil
}