2024-07-27 07:31:04 +00:00
|
|
|
package transport
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-08-25 16:52:00 +00:00
|
|
|
"git.sch9.ru/new_gate/ms-tester/pkg/models"
|
2024-08-18 00:07:31 +00:00
|
|
|
problemv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/problem/v1"
|
2024-08-16 17:09:03 +00:00
|
|
|
sessionv1 "git.sch9.ru/new_gate/ms-tester/pkg/go/gen/proto/session/v1"
|
2024-07-27 07:31:04 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
2024-08-20 22:54:46 +00:00
|
|
|
"net"
|
2024-07-27 07:31:04 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
)
|
|
|
|
|
2024-08-16 11:05:29 +00:00
|
|
|
type ProblemService interface {
|
2024-08-22 22:56:03 +00:00
|
|
|
CanCreateProblem(ctx context.Context) error
|
|
|
|
CreateProblem(ctx context.Context, problem *models.Problem) (int32, error)
|
2024-08-18 00:07:31 +00:00
|
|
|
ReadProblemById(ctx context.Context, id int32) (*models.Problem, error)
|
2024-08-16 11:05:29 +00:00
|
|
|
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-20 11:18:23 +00:00
|
|
|
type UserService interface {
|
|
|
|
CreateUser(ctx context.Context, user *models.User) error
|
|
|
|
ReadUserById(ctx context.Context, userId int32) (*models.User, error)
|
|
|
|
}
|
|
|
|
|
2024-08-16 11:05:29 +00:00
|
|
|
type TesterServer struct {
|
2024-08-18 00:07:31 +00:00
|
|
|
problemv1.UnimplementedProblemServiceServer
|
2024-08-16 11:05:29 +00:00
|
|
|
problemService ProblemService
|
2024-07-27 07:31:04 +00:00
|
|
|
|
2024-08-16 15:01:08 +00:00
|
|
|
sessionClient SessionClient
|
2024-08-20 11:18:23 +00:00
|
|
|
userService UserService
|
|
|
|
|
2024-08-16 15:01:08 +00:00
|
|
|
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-20 11:18:23 +00:00
|
|
|
userService UserService,
|
2024-08-16 15:01:08 +00:00
|
|
|
logger *zap.Logger,
|
|
|
|
) *TesterServer {
|
2024-08-16 11:05:29 +00:00
|
|
|
server := &TesterServer{
|
2024-08-22 22:56:03 +00:00
|
|
|
problemService: problemService,
|
|
|
|
sessionClient: sessionClient,
|
|
|
|
userService: userService,
|
|
|
|
logger: logger,
|
2024-07-27 07:31:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-16 15:01:08 +00:00
|
|
|
grpcServer := grpc.NewServer(
|
2024-08-22 22:56:03 +00:00
|
|
|
grpc.UnaryInterceptor(server.ErrUnwrappingUnaryInterceptor()),
|
|
|
|
grpc.StreamInterceptor(server.ErrUnwrappingStreamInterceptor()),
|
2024-08-20 11:18:23 +00:00
|
|
|
grpc.UnaryInterceptor(server.AuthUnaryInterceptor()),
|
|
|
|
grpc.StreamInterceptor(server.AuthStreamInterceptor()),
|
2024-08-16 15:01:08 +00:00
|
|
|
)
|
|
|
|
|
2024-08-18 00:07:31 +00:00
|
|
|
problemv1.RegisterProblemServiceServer(grpcServer, server)
|
2024-07-27 07:31:04 +00:00
|
|
|
|
2024-08-20 22:54:46 +00:00
|
|
|
server.grpcServer = grpcServer
|
|
|
|
|
2024-08-16 11:05:29 +00:00
|
|
|
return server
|
2024-07-27 07:31:04 +00:00
|
|
|
}
|
|
|
|
|
2024-08-20 22:54:46 +00:00
|
|
|
func (s *TesterServer) Start(lis net.Listener) error {
|
|
|
|
return s.grpcServer.Serve(lis)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TesterServer) Stop() {
|
|
|
|
s.grpcServer.GracefulStop()
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|