fix issues

This commit is contained in:
Timur Kh.
2025-12-17 09:38:23 +03:00
parent 9f585a3a90
commit 1f2e197dc4
4 changed files with 11 additions and 241 deletions
-46
View File
@@ -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)