github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/tests/integration_tests/http_proxies/run-proxy.go (about)

     1  // Copyright 2021 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package main
    15  
    16  import (
    17  	"flag"
    18  	"fmt"
    19  
    20  	grpc_proxy "github.com/bradleyjkemp/grpc-tools/grpc-proxy"
    21  	"github.com/pingcap/log"
    22  	"go.uber.org/zap"
    23  	"google.golang.org/grpc"
    24  	"google.golang.org/grpc/metadata"
    25  )
    26  
    27  func main() {
    28  	defer func() {
    29  		fmt.Println("proxy stopped")
    30  	}()
    31  
    32  	grpc_proxy.RegisterDefaultFlags()
    33  	flag.Parse()
    34  
    35  	log.Info("starting proxy", zap.Any("flags", flag.Args()))
    36  
    37  	proxy, err := grpc_proxy.New(
    38  		grpc_proxy.WithInterceptor(intercept),
    39  		grpc_proxy.DefaultFlags(),
    40  	)
    41  	if err != nil {
    42  		log.Fatal("failed to create proxy", zap.Error(err))
    43  	}
    44  	err = proxy.Start()
    45  	if err != nil {
    46  		log.Fatal("failed to start proxy", zap.Error(err))
    47  	}
    48  	fmt.Println("proxy started")
    49  }
    50  
    51  func intercept(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    52  	fmt.Println(info.FullMethod)
    53  	err := handler(srv, ss)
    54  	if err != nil {
    55  		md, ok := metadata.FromIncomingContext(ss.Context())
    56  		log.Error("failed to handle stream",
    57  			zap.String("method", info.FullMethod),
    58  			zap.Bool("ok", ok),
    59  			zap.Any("metadata", md),
    60  			zap.Error(err))
    61  	}
    62  	return err
    63  }