ms-auth/internal/transport/server.go
2024-08-16 21:43:31 +05:00

94 lines
2 KiB
Go

package transport
import (
"context"
"git.sch9.ru/new_gate/ms-auth/internal/models"
sessionv1 "git.sch9.ru/new_gate/ms-auth/pkg/go/gen/proto/session/v1"
userv1 "git.sch9.ru/new_gate/ms-auth/pkg/go/gen/proto/user/v1"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/timestamppb"
"net"
"time"
"google.golang.org/grpc"
)
type SessionService interface {
Create(ctx context.Context, handle, password string) (*string, error)
Read(ctx context.Context, token string) (*int32, error)
Update(ctx context.Context, token string) error
Delete(ctx context.Context, token string) error
}
type UserService interface {
CreateUser(ctx context.Context, user *models.User) (int32, error)
ReadUser(ctx context.Context, id int32) (*models.User, error)
UpdateUser(ctx context.Context, user *models.User) error
DeleteUser(ctx context.Context, id int32) error
}
type AuthServer struct {
sessionv1.UnimplementedSessionServiceServer
sessionService SessionService
userv1.UnimplementedUserServiceServer
userService UserService
GRPCServer *grpc.Server
logger *zap.Logger
ln net.Listener
}
func NewAuthServer(
sessionService SessionService,
userService UserService,
logger *zap.Logger,
ln net.Listener,
) *AuthServer {
server := &AuthServer{
sessionService: sessionService,
userService: userService,
logger: logger,
ln: ln,
}
grpcServer := grpc.NewServer(
grpc.UnaryInterceptor(server.AuthInterceptor()),
)
server.GRPCServer = grpcServer
sessionv1.RegisterSessionServiceServer(server.GRPCServer, server)
return server
}
func AsTimeP(t *timestamppb.Timestamp) *time.Time {
if t == nil {
return nil
}
tt := t.AsTime()
return &tt
}
func AsMRoleP(v userv1.Role) *models.Role {
vv := models.Role(v.Number())
return &vv
}
func AsTimestampP(t *time.Time) *timestamppb.Timestamp {
if t == nil {
return nil
}
return timestamppb.New(*t)
}
func AsRoleP(r *models.Role) *userv1.Role {
if r == nil {
return nil
}
rr := userv1.Role(*r)
return &rr
}