1c/Ox
1
0
mirror of https://github.com/LazarenkoA/Ox.git synced 2025-11-29 05:36:44 +02:00
This commit is contained in:
Артем
2025-11-04 18:58:12 +03:00
parent 4dca5d34e3
commit 84b51ad85f
30 changed files with 1736 additions and 600 deletions

View File

@@ -5,8 +5,9 @@ import (
"fmt"
grpc_recovery "github.com/grpc-ecosystem/go-grpc-middleware/recovery"
"google.golang.org/grpc"
"load_testing/worker/internal/utils"
"load_testing/worker/proto/gen"
"log"
"log/slog"
"net"
)
@@ -17,7 +18,9 @@ func NewGRPCServer(ctx context.Context, port int, worker gen.WorkerServer) error
}
actualAddr := listener.Addr().String() // ip:port
srv := grpc.NewServer(grpc.ChainStreamInterceptor(grpc_recovery.StreamServerInterceptor()))
logger := utils.Logger().With("name", "grpc")
srv := grpc.NewServer(grpc.ChainUnaryInterceptor(logInterceptor(logger)), grpc.ChainStreamInterceptor(grpc_recovery.StreamServerInterceptor()))
gen.RegisterWorkerServer(srv, worker)
go func() {
@@ -25,6 +28,19 @@ func NewGRPCServer(ctx context.Context, port int, worker gen.WorkerServer) error
srv.Stop()
}()
log.Println("сервер запущен на", actualAddr)
logger.InfoContext(ctx, fmt.Sprintf("сервер запущен на %s", actualAddr))
return srv.Serve(listener)
}
func logInterceptor(logger *slog.Logger) func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
return func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp any, err error) {
_, err = handler(ctx, req)
if err != nil {
logger.ErrorContext(ctx, "grpc error", "error", err)
} else {
logger.InfoContext(ctx, fmt.Sprintf("grpc method %s", info.FullMethod))
}
return
}
}