feat: add panic recovery middleware for grpc and echo server (#3459)

This commit is contained in:
Ryo
2024-05-26 21:38:59 +08:00
committed by GitHub
parent 1ccfa81cf3
commit 784fcbb698
3 changed files with 27 additions and 0 deletions

View File

@ -10,10 +10,12 @@ import (
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/pkg/errors"
"github.com/soheilhy/cmux"
"google.golang.org/grpc"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/server/profile"
apiv1 "github.com/usememos/memos/server/router/api/v1"
@ -43,6 +45,7 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
echoServer.Debug = true
echoServer.HideBanner = true
echoServer.HidePort = true
echoServer.Use(middleware.Recover())
s.echoServer = echoServer
workspaceBasicSetting, err := s.getOrUpsertWorkspaceBasicSetting(ctx)
@ -74,6 +77,7 @@ func NewServer(ctx context.Context, profile *profile.Profile, store *store.Store
grpc.MaxRecvMsgSize(100*1024*1024),
grpc.ChainUnaryInterceptor(
apiv1.NewLoggerInterceptor().LoggerInterceptor,
grpc_recovery.UnaryServerInterceptor(),
apiv1.NewGRPCAuthInterceptor(store, secret).AuthenticationInterceptor,
))
s.grpcServer = grpcServer