ms-tester/internal/transport/server.go

73 lines
1.6 KiB
Go
Raw Normal View History

2024-07-27 07:31:04 +00:00
package transport
import (
"context"
2024-08-16 11:05:29 +00:00
"git.sch9.ru/new_gate/ms-tester/internal/models"
2024-08-16 15:01:08 +00:00
sessionv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/session/v1"
2024-08-16 11:05:29 +00:00
testerv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/tester/v1"
2024-07-27 07:31:04 +00:00
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/timestamppb"
"time"
"google.golang.org/grpc"
)
2024-08-16 11:05:29 +00:00
type ProblemService interface {
CreateProblem(ctx context.Context, problem *models.Problem) (int32, error)
ReadProblem(ctx context.Context, id int32) (*models.Problem, error)
UpdateProblem(ctx context.Context, problem *models.Problem) error
DeleteProblem(ctx context.Context, id int32) error
2024-07-27 07:31:04 +00:00
}
2024-08-16 15:01:08 +00:00
type SessionClient interface {
Read(ctx context.Context,
in *sessionv1.ReadSessionRequest,
opts ...grpc.CallOption,
) (*sessionv1.ReadSessionResponse, error)
}
2024-08-16 11:05:29 +00:00
type TesterServer struct {
testerv1.UnimplementedTesterServiceServer
problemService ProblemService
2024-07-27 07:31:04 +00:00
2024-08-16 15:01:08 +00:00
sessionClient SessionClient
grpcServer *grpc.Server
2024-07-27 07:31:04 +00:00
logger *zap.Logger
}
2024-08-16 15:01:08 +00:00
func NewTesterServer(
problemService ProblemService,
2024-08-16 15:03:56 +00:00
sessionClient SessionClient,
2024-08-16 15:01:08 +00:00
logger *zap.Logger,
) *TesterServer {
2024-08-16 11:05:29 +00:00
server := &TesterServer{
problemService: problemService,
2024-08-16 15:03:56 +00:00
sessionClient: sessionClient,
2024-07-27 07:31:04 +00:00
logger: logger,
}
2024-08-16 15:01:08 +00:00
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(server.AuthInterceptor()),
)
testerv1.RegisterTesterServiceServer(grpcServer, server)
2024-07-27 07:31:04 +00:00
2024-08-16 11:05:29 +00:00
return server
2024-07-27 07:31:04 +00:00
}
func AsTimeP(t *timestamppb.Timestamp) *time.Time {
if t == nil {
return nil
}
tt := t.AsTime()
return &tt
}
func AsTimestampP(t *time.Time) *timestamppb.Timestamp {
if t == nil {
return nil
}
return timestamppb.New(*t)
}