use auth service for validating user in competitions service
This commit is contained in:
@@ -8,14 +8,17 @@ import (
|
||||
|
||||
"datarush/internal/competition/config"
|
||||
grpcHandlers "datarush/internal/competition/handler/grpc"
|
||||
"datarush/internal/competition/middleware"
|
||||
"datarush/internal/competition/repository/postgres"
|
||||
"datarush/internal/competition/service"
|
||||
authpb "datarush/pkg/api/auth"
|
||||
pb "datarush/pkg/api/competition"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/redis/go-redis/v9"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"google.golang.org/grpc/reflection"
|
||||
)
|
||||
|
||||
@@ -30,6 +33,7 @@ type Server struct {
|
||||
config *config.Config
|
||||
db *sqlx.DB
|
||||
redisClient *redis.Client
|
||||
authConn *grpc.ClientConn
|
||||
}
|
||||
|
||||
func New(cfg *config.Config) *Server {
|
||||
@@ -51,6 +55,12 @@ func (s *Server) Start() error {
|
||||
DB: s.config.RedisDB,
|
||||
})
|
||||
|
||||
authConn, err := grpc.Dial(s.config.AuthSvcAddr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to connect to auth service: %w", err)
|
||||
}
|
||||
s.authConn = authConn
|
||||
|
||||
if err := s.registerGRPCServices(); err != nil {
|
||||
return fmt.Errorf("failed to register gRPC services: %w", err)
|
||||
}
|
||||
@@ -71,6 +81,13 @@ func (s *Server) Start() error {
|
||||
}
|
||||
|
||||
func (s *Server) registerGRPCServices() error {
|
||||
authClient := authpb.NewAuthServiceClient(s.authConn)
|
||||
authInterceptor := middleware.NewAuthInterceptor(authClient)
|
||||
|
||||
s.grpcServer = grpc.NewServer(
|
||||
grpc.UnaryInterceptor(authInterceptor.Unary()),
|
||||
)
|
||||
|
||||
compRepo := postgres.NewCompetitionRepository(s.db, s.redisClient, s.config.CacheEnabled)
|
||||
compService := service.NewCompetitionService(compRepo)
|
||||
compHandler := grpcHandlers.NewCompetitionHandler(compService)
|
||||
@@ -91,6 +108,12 @@ func (s *Server) Stop() {
|
||||
s.grpcServer.GracefulStop()
|
||||
}
|
||||
|
||||
if s.authConn != nil {
|
||||
if err := s.authConn.Close(); err != nil {
|
||||
log.Printf("failed to close auth service connection: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
if s.db != nil {
|
||||
if err := s.db.Close(); err != nil {
|
||||
log.Printf("failed to close database: %v", err)
|
||||
|
||||
Reference in New Issue
Block a user