github.com/gofiber/fiber/v2@v2.47.0/middleware/proxy/config.go (about)

     1  package proxy
     2  
     3  import (
     4  	"crypto/tls"
     5  	"time"
     6  
     7  	"github.com/gofiber/fiber/v2"
     8  
     9  	"github.com/valyala/fasthttp"
    10  )
    11  
    12  // Config defines the config for middleware.
    13  type Config struct {
    14  	// Next defines a function to skip this middleware when returned true.
    15  	//
    16  	// Optional. Default: nil
    17  	Next func(c *fiber.Ctx) bool
    18  
    19  	// Servers defines a list of <scheme>://<host> HTTP servers,
    20  	//
    21  	// which are used in a round-robin manner.
    22  	// i.e.: "https://foobar.com, http://www.foobar.com"
    23  	//
    24  	// Required
    25  	Servers []string
    26  
    27  	// ModifyRequest allows you to alter the request
    28  	//
    29  	// Optional. Default: nil
    30  	ModifyRequest fiber.Handler
    31  
    32  	// ModifyResponse allows you to alter the response
    33  	//
    34  	// Optional. Default: nil
    35  	ModifyResponse fiber.Handler
    36  
    37  	// Timeout is the request timeout used when calling the proxy client
    38  	//
    39  	// Optional. Default: 1 second
    40  	Timeout time.Duration
    41  
    42  	// Per-connection buffer size for requests' reading.
    43  	// This also limits the maximum header size.
    44  	// Increase this buffer if your clients send multi-KB RequestURIs
    45  	// and/or multi-KB headers (for example, BIG cookies).
    46  	ReadBufferSize int
    47  
    48  	// Per-connection buffer size for responses' writing.
    49  	WriteBufferSize int
    50  
    51  	// tls config for the http client.
    52  	TlsConfig *tls.Config //nolint:stylecheck,revive // TODO: Rename to "TLSConfig" in v3
    53  
    54  	// Client is custom client when client config is complex.
    55  	// Note that Servers, Timeout, WriteBufferSize, ReadBufferSize and TlsConfig
    56  	// will not be used if the client are set.
    57  	Client *fasthttp.LBClient
    58  }
    59  
    60  // ConfigDefault is the default config
    61  var ConfigDefault = Config{
    62  	Next:           nil,
    63  	ModifyRequest:  nil,
    64  	ModifyResponse: nil,
    65  	Timeout:        fasthttp.DefaultLBClientTimeout,
    66  }
    67  
    68  // configDefault function to set default values
    69  func configDefault(config ...Config) Config {
    70  	// Return default config if nothing provided
    71  	if len(config) < 1 {
    72  		return ConfigDefault
    73  	}
    74  
    75  	// Override default config
    76  	cfg := config[0]
    77  
    78  	// Set default values
    79  	if cfg.Timeout <= 0 {
    80  		cfg.Timeout = ConfigDefault.Timeout
    81  	}
    82  
    83  	// Set default values
    84  	if len(cfg.Servers) == 0 && cfg.Client == nil {
    85  		panic("Servers cannot be empty")
    86  	}
    87  	return cfg
    88  }