124 lines
3.1 KiB
Go
124 lines
3.1 KiB
Go
|
package transport
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"go.uber.org/zap"
|
||
|
"google.golang.org/protobuf/types/known/timestamppb"
|
||
|
"ms-auth/internal/storage"
|
||
|
emailv1 "ms-auth/pkg/go/gen/email/v1"
|
||
|
sessionv1 "ms-auth/pkg/go/gen/session/v1"
|
||
|
userv1 "ms-auth/pkg/go/gen/user/v1"
|
||
|
"net"
|
||
|
"time"
|
||
|
|
||
|
"google.golang.org/grpc"
|
||
|
)
|
||
|
|
||
|
type SessionServiceI 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 UserServiceI interface {
|
||
|
CreateUser(ctx context.Context, token, username, password string, email *string, expiresAt *time.Time, role *int32) (*int32, error)
|
||
|
ReadUser(ctx context.Context, token string, id int32) (*storage.User, error)
|
||
|
UpdateUser(ctx context.Context, token string, id int32, username *string, password *string, email *string, expiresAt *time.Time, role *int32) error
|
||
|
DeleteUser(ctx context.Context, token string, id int32) error
|
||
|
}
|
||
|
|
||
|
type AuthServer struct {
|
||
|
emailv1.UnimplementedEmailServiceServer
|
||
|
|
||
|
sessionv1.UnimplementedSessionServiceServer
|
||
|
sessionService SessionServiceI
|
||
|
|
||
|
userv1.UnimplementedUserServiceServer
|
||
|
userService UserServiceI
|
||
|
|
||
|
gRPCServer *grpc.Server
|
||
|
logger *zap.Logger
|
||
|
}
|
||
|
|
||
|
// NewAuthServer creates a new instance of the AuthServer struct.
|
||
|
//
|
||
|
// Parameters:
|
||
|
// - sessionService: A pointer to the SessionServiceI interface.
|
||
|
// - gRPCServer: A pointer to the grpc.Server struct.
|
||
|
// - logger: A pointer to the zap.Logger struct.
|
||
|
//
|
||
|
// Returns:
|
||
|
// - *AuthServer: A pointer to the AuthServer struct.
|
||
|
func NewAuthServer(sessionService SessionServiceI, userService UserServiceI, gRPCServer *grpc.Server, logger *zap.Logger) *AuthServer {
|
||
|
return &AuthServer{
|
||
|
sessionService: sessionService,
|
||
|
userService: userService,
|
||
|
gRPCServer: gRPCServer,
|
||
|
logger: logger,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// Start starts the AuthServer and listens on port :8090.
|
||
|
//
|
||
|
// It creates a listener on the specified address and starts serving incoming requests.
|
||
|
// It also logs the server start and any errors that occur during serving.
|
||
|
//
|
||
|
// No parameters.
|
||
|
// No return values.
|
||
|
func (s *AuthServer) Start() {
|
||
|
lis, err := net.Listen("tcp", ":8090")
|
||
|
if err != nil {
|
||
|
s.logger.Fatal("")
|
||
|
}
|
||
|
|
||
|
sessionv1.RegisterSessionServiceServer(s.gRPCServer, s)
|
||
|
go func() {
|
||
|
s.logger.Info("Listening on :8090")
|
||
|
if err := s.gRPCServer.Serve(lis); err != nil {
|
||
|
panic(err.Error())
|
||
|
}
|
||
|
}()
|
||
|
s.logger.Info("server started")
|
||
|
}
|
||
|
|
||
|
// GracefullyStop stops the server gracefully.
|
||
|
//
|
||
|
// No parameters.
|
||
|
// No return values.
|
||
|
func (s *AuthServer) GracefullyStop() {
|
||
|
s.gRPCServer.GracefulStop()
|
||
|
s.logger.Info("server stopped")
|
||
|
}
|
||
|
|
||
|
func AsTimeP(t *timestamppb.Timestamp) *time.Time {
|
||
|
if t == nil {
|
||
|
return nil
|
||
|
}
|
||
|
tt := t.AsTime()
|
||
|
return &tt
|
||
|
}
|
||
|
|
||
|
func AsInt32P(v *userv1.Role) *int32 {
|
||
|
if v == nil {
|
||
|
return nil
|
||
|
}
|
||
|
vv := int32(v.Number())
|
||
|
return &vv
|
||
|
}
|
||
|
|
||
|
func AsTimestampP(t *time.Time) *timestamppb.Timestamp {
|
||
|
if t == nil {
|
||
|
return nil
|
||
|
}
|
||
|
return timestamppb.New(*t)
|
||
|
}
|
||
|
|
||
|
func AsRoleP(r *int32) *userv1.Role {
|
||
|
if r == nil {
|
||
|
return nil
|
||
|
}
|
||
|
rr := userv1.Role(*r)
|
||
|
return &rr
|
||
|
}
|