package gateway import ( "context" "fmt" "log" "net" "net/http" "time" authPb "datarush/pkg/api/auth" "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" "google.golang.org/grpc" "google.golang.org/grpc/credentials/insecure" ) const ( httpReadTimeout = 10 * time.Second httpWriteTimeout = 10 * time.Second httpIdleTimeout = 60 * time.Second ) func StartGateway(grpcPort, httpPort int, authGrpcAddr string) error { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() gwmux := runtime.NewServeMux() opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())} // Register auth service if err := authPb.RegisterAuthServiceHandlerFromEndpoint(ctx, gwmux, authGrpcAddr, opts); err != nil { return fmt.Errorf("failed to register auth service: %w", err) } srv := &http.Server{ Addr: fmt.Sprintf(":%d", httpPort), Handler: gwmux, ReadTimeout: httpReadTimeout, WriteTimeout: httpWriteTimeout, IdleTimeout: httpIdleTimeout, } log.Printf("starting gRPC-Gateway on port %d", httpPort) return srv.ListenAndServe() } func GetGRPCListener(port int) (net.Listener, error) { return net.Listen("tcp", fmt.Sprintf(":%d", port)) }