github.com/clubpay/ronykit/kit@v0.14.4-0.20240515065620-d0dace45cbc7/stub/option.go (about)

     1  package stub
     2  
     3  import (
     4  	"io"
     5  	"time"
     6  
     7  	"github.com/clubpay/ronykit/kit"
     8  	"github.com/valyala/fasthttp"
     9  	"github.com/valyala/fasthttp/fasthttpproxy"
    10  	"golang.org/x/net/http/httpproxy"
    11  )
    12  
    13  type Option func(cfg *config)
    14  
    15  type config struct {
    16  	name          string
    17  	hostPort      string
    18  	secure        bool
    19  	skipVerifyTLS bool
    20  	dumpReq       io.Writer
    21  	dumpRes       io.Writer
    22  	l             kit.Logger
    23  	tp            kit.TracePropagator
    24  
    25  	readTimeout, writeTimeout, dialTimeout time.Duration
    26  	proxy                                  *httpproxy.Config
    27  	dialFunc                               fasthttp.DialFunc
    28  	codec                                  kit.MessageCodec
    29  }
    30  
    31  func Secure() Option {
    32  	return func(cfg *config) {
    33  		cfg.secure = true
    34  	}
    35  }
    36  
    37  func SkipTLSVerify() Option {
    38  	return func(s *config) {
    39  		s.skipVerifyTLS = true
    40  	}
    41  }
    42  
    43  func Name(name string) Option {
    44  	return func(cfg *config) {
    45  		cfg.name = name
    46  	}
    47  }
    48  
    49  func WithReadTimeout(timeout time.Duration) Option {
    50  	return func(cfg *config) {
    51  		cfg.readTimeout = timeout
    52  	}
    53  }
    54  
    55  func WithWriteTimeout(timeout time.Duration) Option {
    56  	return func(cfg *config) {
    57  		cfg.writeTimeout = timeout
    58  	}
    59  }
    60  
    61  func WithDialTimeout(timeout time.Duration) Option {
    62  	return func(cfg *config) {
    63  		cfg.dialTimeout = timeout
    64  	}
    65  }
    66  
    67  func WithMessageCodec(c kit.MessageCodec) Option {
    68  	return func(cfg *config) {
    69  		cfg.codec = c
    70  	}
    71  }
    72  
    73  func WithLogger(l kit.Logger) Option {
    74  	return func(cfg *config) {
    75  		cfg.l = l
    76  	}
    77  }
    78  
    79  func DumpTo(w io.Writer) Option {
    80  	return func(cfg *config) {
    81  		cfg.dumpReq = w
    82  		cfg.dumpRes = w
    83  	}
    84  }
    85  
    86  func WithTracePropagator(tp kit.TracePropagator) Option {
    87  	return func(cfg *config) {
    88  		cfg.tp = tp
    89  	}
    90  }
    91  
    92  // WithHTTPProxy returns an Option that sets the dialer to the provided HTTP proxy.
    93  // example formats:
    94  //
    95  //	localhost:9050
    96  //	username:password@localhost:9050
    97  //	localhost:9050
    98  func WithHTTPProxy(proxyURL string, timeout time.Duration) Option {
    99  	return func(cfg *config) {
   100  		cfg.proxy = httpproxy.FromEnvironment()
   101  		cfg.proxy.HTTPProxy = proxyURL
   102  		cfg.proxy.HTTPSProxy = proxyURL
   103  		cfg.dialFunc = fasthttpproxy.FasthttpHTTPDialerTimeout(proxyURL, timeout)
   104  	}
   105  }
   106  
   107  // WithSocksProxy returns an Option that sets the dialer to the provided SOCKS5 proxy.
   108  // example format: localhost:9050
   109  func WithSocksProxy(proxyURL string) Option {
   110  	return func(cfg *config) {
   111  		cfg.proxy = httpproxy.FromEnvironment()
   112  		cfg.proxy.HTTPProxy = proxyURL
   113  		cfg.proxy.HTTPSProxy = proxyURL
   114  		cfg.dialFunc = fasthttpproxy.FasthttpSocksDialer(proxyURL)
   115  	}
   116  }