[hw-8] add: comment service

This commit is contained in:
3ybacTuK
2025-07-26 23:47:18 +03:00
parent 6420eaf3d7
commit 6e0d90a6d5
29 changed files with 1249 additions and 725 deletions

View File

@@ -0,0 +1,24 @@
package mw
import (
"context"
"github.com/rs/zerolog/log"
"google.golang.org/grpc"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)
func Logging(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
raw, _ := protojson.Marshal((req).(proto.Message))
log.Debug().Msgf("request: method: %v, req: %s", info.FullMethod, string(raw))
if resp, err = handler(ctx, req); err != nil {
log.Debug().Msgf("response: method: %v, err: %s", info.FullMethod, err.Error())
return
}
rawResp, _ := protojson.Marshal((resp).(proto.Message))
log.Debug().Msgf("response: method: %v, resp: %s", info.FullMethod, string(rawResp))
return
}

View File

@@ -0,0 +1,18 @@
package mw
import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func Validate(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
if v, ok := req.(interface{ Validate() error }); ok {
if err := v.Validate(); err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
}
return handler(ctx, req)
}