github.com/blend/go-sdk@v1.20220411.3/reverseproxy/proxy_option.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package reverseproxy
     9  
    10  import (
    11  	"net/http"
    12  
    13  	"github.com/blend/go-sdk/logger"
    14  	"github.com/blend/go-sdk/webutil"
    15  )
    16  
    17  // ProxyOption is a function that mutates a proxy.
    18  type ProxyOption func(*Proxy) error
    19  
    20  // OptProxyLog sets the proxy logger, as well
    21  // as the logger on any upstreams that are configured.
    22  func OptProxyLog(log logger.Log) ProxyOption {
    23  	return func(p *Proxy) error {
    24  		p.Log = log
    25  		for _, us := range p.Upstreams {
    26  			us.Log = log
    27  		}
    28  		return nil
    29  	}
    30  }
    31  
    32  // OptProxyResolver sets the proxy resolver.
    33  func OptProxyResolver(resolver Resolver) ProxyOption {
    34  	return func(p *Proxy) error {
    35  		p.Resolver = resolver
    36  		return nil
    37  	}
    38  }
    39  
    40  // OptProxyUpstream adds a proxy upstream.
    41  func OptProxyUpstream(upstream *Upstream) ProxyOption {
    42  	return func(p *Proxy) error {
    43  		p.Upstreams = append(p.Upstreams, upstream)
    44  		return nil
    45  	}
    46  }
    47  
    48  // OptProxyAddHeaderValue adds a proxy upstream.
    49  func OptProxyAddHeaderValue(key, value string) ProxyOption {
    50  	return func(p *Proxy) error {
    51  		if p.Headers == nil {
    52  			p.Headers = http.Header{}
    53  		}
    54  		p.Headers.Add(key, value)
    55  		return nil
    56  	}
    57  }
    58  
    59  // OptProxySetHeaderValue adds a proxy upstream.
    60  func OptProxySetHeaderValue(key, value string) ProxyOption {
    61  	return func(p *Proxy) error {
    62  		if p.Headers == nil {
    63  			p.Headers = http.Header{}
    64  		}
    65  		p.Headers.Set(key, value)
    66  		return nil
    67  	}
    68  }
    69  
    70  // OptProxyDeleteHeader adds a proxy upstream.
    71  func OptProxyDeleteHeader(key string) ProxyOption {
    72  	return func(p *Proxy) error {
    73  		if p.Headers == nil {
    74  			p.Headers = http.Header{}
    75  		}
    76  		p.Headers.Del(key)
    77  		return nil
    78  	}
    79  }
    80  
    81  // OptProxyTracer adds a proxy tracer.
    82  func OptProxyTracer(tracer webutil.HTTPTracer) ProxyOption {
    83  	return func(p *Proxy) error {
    84  		p.Tracer = tracer
    85  		return nil
    86  	}
    87  }
    88  
    89  // OptProxyTransformRequest sets the `TransformRequest` on a `Proxy`.
    90  func OptProxyTransformRequest(tr TransformRequest) ProxyOption {
    91  	return func(p *Proxy) error {
    92  		p.TransformRequest = tr
    93  		return nil
    94  	}
    95  }