From 1f2e197dc430facb68a608edeab68fda4c6d24cd Mon Sep 17 00:00:00 2001 From: "Timur Kh." Date: Wed, 17 Dec 2025 09:38:23 +0300 Subject: [PATCH] fix issues --- .../competition/handler/grpc/competition.go | 10 +- .../repository/postgres/competition.go | 12 +- internal/competition/server/server.go | 46 ----- pkg/api/competition/competition.pb.go | 184 ------------------ 4 files changed, 11 insertions(+), 241 deletions(-) diff --git a/internal/competition/handler/grpc/competition.go b/internal/competition/handler/grpc/competition.go index 5869479..427276a 100644 --- a/internal/competition/handler/grpc/competition.go +++ b/internal/competition/handler/grpc/competition.go @@ -5,11 +5,13 @@ import ( "datarush/internal/competition/domain" pb "datarush/pkg/api/competition" + "strings" + "github.com/google/uuid" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" + "google.golang.org/protobuf/types/known/emptypb" "google.golang.org/protobuf/types/known/timestamppb" - "strings" ) // ICompetitionService defines the interface for business logic. @@ -72,7 +74,7 @@ func (h *CompetitionHandler) EditCompetition(ctx context.Context, req *pb.Compet return fromDomainCompetition(updatedComp), nil } -func (h *CompetitionHandler) DeleteCompetition(ctx context.Context, req *pb.DeleteCompetitionRequest) (*pb.DeleteCompetitionResponse, error) { +func (h *CompetitionHandler) DeleteCompetition(ctx context.Context, req *pb.DeleteCompetitionRequest) (*emptypb.Empty, error) { id, err := uuid.Parse(req.GetCompetitionId()) if err != nil { return nil, status.Errorf(codes.InvalidArgument, "invalid competition id format") @@ -80,7 +82,7 @@ func (h *CompetitionHandler) DeleteCompetition(ctx context.Context, req *pb.Dele if err := h.service.DeleteCompetition(ctx, id); err != nil { return nil, status.Errorf(codes.Internal, "failed to delete competition: %v", err) } - return &pb.DeleteCompetitionResponse{Success: true}, nil + return &emptypb.Empty{}, nil } func (h *CompetitionHandler) ListCompetitions(ctx context.Context, req *pb.ListCompetitionsRequest) (*pb.ListCompetitionsResponse, error) { @@ -196,4 +198,4 @@ func toDomainCompetitionType(ct pb.CompetitionType) domain.CompetitionType { func fromDomainCompetitionType(ct domain.CompetitionType) pb.CompetitionType { return pb.CompetitionType(pb.CompetitionType_value["COMPETITION_TYPE_"+string(ct)]) -} +} \ No newline at end of file diff --git a/internal/competition/repository/postgres/competition.go b/internal/competition/repository/postgres/competition.go index f33e6b8..5832a88 100644 --- a/internal/competition/repository/postgres/competition.go +++ b/internal/competition/repository/postgres/competition.go @@ -18,14 +18,12 @@ const ( competitionCachePrefix = "competition:" ) - type CompetitionRepository struct { - db *sql.DB + db *sql.DB redisClient *redis.Client cacheEnabled bool } - func NewCompetitionRepository(db *sql.DB, redisClient *redis.Client, cacheEnabled bool) repository.CompetitionRepository { return &CompetitionRepository{ db: db, @@ -149,7 +147,7 @@ func (r *CompetitionRepository) Delete(ctx context.Context, id uuid.UUID) error return nil } -func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCompetitionsOptions) ([]*domain.Competition, int, error) { +func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCompetitionsOptions) ([]domain.Competition, int, error) { var args []interface{} var whereClauses []string argId := 1 @@ -200,7 +198,7 @@ func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCo } defer rows.Close() - var competitions []*domain.Competition + var competitions []domain.Competition for rows.Next() { var c domain.Competition err := rows.Scan( @@ -219,7 +217,7 @@ func (r *CompetitionRepository) List(ctx context.Context, opts repository.ListCo if err != nil { return nil, 0, err } - competitions = append(competitions, &c) + competitions = append(competitions, c) } return competitions, total, nil @@ -237,4 +235,4 @@ func (r *CompetitionRepository) ChangeState(ctx context.Context, id uuid.UUID, s } return r.Get(ctx, id) -} +} \ No newline at end of file diff --git a/internal/competition/server/server.go b/internal/competition/server/server.go index 4eed2c3..eb6f4b6 100644 --- a/internal/competition/server/server.go +++ b/internal/competition/server/server.go @@ -1,7 +1,6 @@ package server import ( - "context" "fmt" "log" "net" @@ -14,11 +13,9 @@ import ( "datarush/internal/competition/service" pb "datarush/pkg/api/competition" - "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "github.com/jmoiron/sqlx" _ "github.com/lib/pq" "google.golang.org/grpc" - "google.golang.org/grpc/credentials/insecure" "google.golang.org/grpc/reflection" ) @@ -54,12 +51,6 @@ func (s *Server) Start() error { } }() - go func() { - if err := s.startHTTPServer(); err != nil { - log.Fatalf("failed to start HTTP server: %v", err) - } - }() - log.Println("competition service started") return nil } @@ -89,50 +80,13 @@ func (s *Server) registerGRPCServices() { } } -func (s *Server) startHTTPServer() error { - ctx := context.Background() - ctx, cancel := context.WithCancel(ctx) - defer cancel() - - mux := runtime.NewServeMux() - opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())} - grpcEndpoint := fmt.Sprintf("localhost:%d", s.config.GRPCPort) - - err := pb.RegisterCompetitionServiceHandlerFromEndpoint(ctx, mux, grpcEndpoint, opts) - if err != nil { - return fmt.Errorf("failed to register http handlers: %w", err) - } - - s.httpServer = &http.Server{ - Addr: fmt.Sprintf(":%d", s.config.HTTPPort), - Handler: mux, - ReadTimeout: httpReadTimeout, - WriteTimeout: httpWriteTimeout, - IdleTimeout: httpIdleTimeout, - } - - log.Printf("starting HTTP server on port %d", s.config.HTTPPort) - return s.httpServer.ListenAndServe() -} - func (s *Server) Stop() { log.Println("shutting down competition server...") - // Shutdown HTTP server - if s.httpServer != nil { - ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) - defer cancel() - if err := s.httpServer.Shutdown(ctx); err != nil { - log.Printf("failed to shutdown HTTP server gracefully: %v", err) - } - } - - // Shutdown gRPC server if s.grpcServer != nil { s.grpcServer.GracefulStop() } - // Close database connection if s.db != nil { if err := s.db.Close(); err != nil { log.Printf("failed to close database connection: %v", err) diff --git a/pkg/api/competition/competition.pb.go b/pkg/api/competition/competition.pb.go index a23b474..778c5c9 100644 --- a/pkg/api/competition/competition.pb.go +++ b/pkg/api/competition/competition.pb.go @@ -16,7 +16,6 @@ import ( "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/grpclog" - "google.golang.org/grpc/metadata" "google.golang.org/grpc/status" "google.golang.org/protobuf/proto" protoreflect "google.golang.org/protobuf/reflect/protoreflect" @@ -742,189 +741,6 @@ func file_api_proto_competition_proto_init() { file_api_proto_competition_proto_depIdxs = nil } -func RegisterCompetitionServiceHandler(ctx context.Context, mux *runtime.ServeMux, conn *grpc.ClientConn) error { - return RegisterCompetitionServiceHandlerClient(ctx, mux, NewCompetitionServiceClient(conn)) -} - - -func RegisterCompetitionServiceHandlerClient(ctx context.Context, mux *runtime.ServeMux, client CompetitionServiceClient) error { - var err error - err = mux.Handle("POST", runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"v1", "competitions"}, "")), func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { - ctx, cancel := context.WithCancel(req.Context()) - defer cancel() - inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) - var err error - var annotatedContext context.Context - annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/competition.CompetitionService/CreateCompetition", runtime.WithHTTPPathPattern("/v1/competitions")) - if err != nil { - runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) - return - } - resp, md, err := request_CompetitionService_CreateCompetition_0(annotatedContext, inboundMarshaler, client, req, pathParams) - annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) - if err != nil { - runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) - return - } - - forward_CompetitionService_CreateCompetition_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) - - }) - if err != nil { - return err - } - - err = mux.Handle("GET", runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"v1", "competitions", "competition_id"}, "")), func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { - ctx, cancel := context.WithCancel(req.Context()) - defer cancel() - inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) - var err error - var annotatedContext context.Context - annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/competition.CompetitionService/GetCompetition", runtime.WithHTTPPathPattern("/v1/competitions/{competition_id}")) - if err != nil { - runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) - return - } - resp, md, err := request_CompetitionService_GetCompetition_0(annotatedContext, inboundMarshaler, client, req, pathParams) - annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) - if err != nil { - runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) - return - } - - forward_CompetitionService_GetCompetition_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) - - }) - if err != nil { - return err - } - - err = mux.Handle("PUT", runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"v1", "competitions", "id"}, "")), func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { - ctx, cancel := context.WithCancel(req.Context()) - defer cancel() - inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) - var err error - var annotatedContext context.Context - annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/competition.CompetitionService/EditCompetition", runtime.WithHTTPPathPattern("/v1/competitions/{id}")) - if err != nil { - runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) - return - } - resp, md, err := request_CompetitionService_EditCompetition_0(annotatedContext, inboundMarshaler, client, req, pathParams) - annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) - if err != nil { - runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) - return - } - - forward_CompetitionService_EditCompetition_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) - - }) - if err != nil { - return err - } - - err = mux.Handle("DELETE", runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2}, []string{"v1", "competitions", "competition_id"}, "")), func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { - ctx, cancel := context.WithCancel(req.Context()) - defer cancel() - inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) - var err error - var annotatedContext context.Context - annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/competition.CompetitionService/DeleteCompetition", runtime.WithHTTPPathPattern("/v1/competitions/{competition_id}")) - if err != nil { - runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) - return - } - resp, md, err := request_CompetitionService_DeleteCompetition_0(annotatedContext, inboundMarshaler, client, req, pathParams) - annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) - if err != nil { - runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) - return - } - - forward_CompetitionService_DeleteCompetition_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) - - }) - if err != nil { - return err - } - - err = mux.Handle("GET", runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1}, []string{"v1", "competitions"}, "")), func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { - ctx, cancel := context.WithCancel(req.Context()) - defer cancel() - inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) - var err error - var annotatedContext context.Context - annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/competition.CompetitionService/ListCompetitions", runtime.WithHTTPPathPattern("/v1/competitions")) - if err != nil { - runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) - return - } - resp, md, err := request_CompetitionService_ListCompetitions_0(annotatedContext, inboundMarshaler, client, req, pathParams) - annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) - if err != nil { - runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) - return - } - - forward_CompetitionService_ListCompetitions_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) - - }) - if err != nil { - return err - } - - err = mux.Handle("POST", runtime.MustPattern(runtime.NewPattern(1, []int{2, 0, 2, 1, 1, 0, 4, 1, 5, 2, 2, 3}, []string{"v1", "competitions", "competition_id", "state"}, "")), func(w http.ResponseWriter, req *http.Request, pathParams map[string]string) { - ctx, cancel := context.WithCancel(req.Context()) - defer cancel() - inboundMarshaler, outboundMarshaler := runtime.MarshalerForRequest(mux, req) - var err error - var annotatedContext context.Context - annotatedContext, err = runtime.AnnotateContext(ctx, mux, req, "/competition.CompetitionService/ChangeCompetitionState", runtime.WithHTTPPathPattern("/v1/competitions/{competition_id}/state")) - if err != nil { - runtime.HTTPError(ctx, mux, outboundMarshaler, w, req, err) - return - } - resp, md, err := request_CompetitionService_ChangeCompetitionState_0(annotatedContext, inboundMarshaler, client, req, pathParams) - annotatedContext = runtime.NewServerMetadataContext(annotatedContext, md) - if err != nil { - runtime.HTTPError(annotatedContext, mux, outboundMarshaler, w, req, err) - return - } - - forward_CompetitionService_ChangeCompetitionState_0(annotatedContext, mux, outboundMarshaler, w, req, resp, mux.GetForwardResponseOptions()...) - - }) - if err != nil { - return err - } - - return nil -} - -func RegisterCompetitionServiceHandlerFromEndpoint(ctx context.Context, mux *runtime.ServeMux, endpoint string, opts []grpc.DialOption) (err error) { - conn, err := grpc.DialContext(ctx, endpoint, opts...) - if err != nil { - return err - } - defer func() { - if err != nil { - if cerr := conn.Close(); cerr != nil { - grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) - } - return - } - go func() { - <-ctx.Done() - if cerr := conn.Close(); cerr != nil { - grpclog.Infof("Failed to close conn to %s: %v", endpoint, cerr) - } - }() - }() - - return RegisterCompetitionServiceHandler(ctx, mux, conn) -} - func request_CompetitionService_CreateCompetition_0(ctx context.Context, marshaler runtime.Marshaler, client CompetitionServiceClient, req *http.Request, pathParams map[string]string) (proto.Message, runtime.ServerMetadata, error) { var protoReq Competition var metadata runtime.ServerMetadata