@@ -12,9 +12,16 @@ Proxy middleware for [Fiber](https://github.com/gofiber/fiber) that allows you t
1212### Signatures
1313
1414``` go
15+ // Balancer create a load balancer among multiple upstrem servers.
1516func Balancer (config Config ) fiber .Handler
17+ // Forward performs the given http request and fills the given http response.
1618func Forward(addr string, clients ...*fasthttp.Client) fiber.Handler
19+ // Do performs the given http request and fills the given http response.
1720func Do(c *fiber.Ctx, addr string, clients ...*fasthttp.Client) error
21+ // DomainForward the given http request based on the given domain and fills the given http response
22+ func DomainForward(hostname string, addr string, clients ...*fasthttp.Client) fiber.Handler
23+ // BalancerForward performs the given http request based round robin balancer and fills the given http response
24+ func BalancerForward(servers []string, clients ...*fasthttp.Client) fiber.Handler
1825```
1926
2027### Examples
@@ -23,8 +30,8 @@ Import the middleware package that is part of the Fiber web framework
2330
2431```go
2532import (
26- "github.com/gofiber/fiber/v2"
27- "github.com/gofiber/fiber/v2/middleware/proxy"
33+ "github.com/gofiber/fiber/v2"
34+ "github.com/gofiber/fiber/v2/middleware/proxy"
2835)
2936```
3037
@@ -39,54 +46,64 @@ proxy.WithTlsConfig(&tls.Config{
3946
4047// if you need to use global self-custom client, you should use proxy.WithClient.
4148proxy.WithClient (&fasthttp.Client {
42- NoDefaultUserAgentHeader : true ,
43- DisablePathNormalizing : true ,
49+ NoDefaultUserAgentHeader : true ,
50+ DisablePathNormalizing : true ,
4451})
4552
4653// Forward to url
4754app.Get (" /gif" , proxy.Forward (" https://i.imgur.com/IWaBepg.gif" ))
4855
56+ // If you want to forward with a specific domain. You have to use proxy.DomainForward.
57+ app.Get (" /payments" , proxy.DomainForward (" docs.gofiber.io" , " http://localhost:8000" ))
58+
4959// Forward to url with local custom client
5060app.Get (" /gif" , proxy.Forward (" https://i.imgur.com/IWaBepg.gif" , &fasthttp.Client {
51- NoDefaultUserAgentHeader : true ,
52- DisablePathNormalizing : true ,
61+ NoDefaultUserAgentHeader : true ,
62+ DisablePathNormalizing : true ,
5363}))
5464
5565// Make request within handler
5666app.Get (" /:id" , func (c *fiber.Ctx ) error {
57- url := " https://i.imgur.com/" +c.Params (" id" )+" .gif"
58- if err := proxy.Do (c, url); err != nil {
59- return err
60- }
61- // Remove Server header from response
62- c.Response ().Header .Del (fiber.HeaderServer )
63- return nil
67+ url := " https://i.imgur.com/" +c.Params (" id" )+" .gif"
68+ if err := proxy.Do (c, url); err != nil {
69+ return err
70+ }
71+ // Remove Server header from response
72+ c.Response ().Header .Del (fiber.HeaderServer )
73+ return nil
6474})
6575
6676// Minimal round robin balancer
6777app.Use (proxy.Balancer (proxy.Config {
68- Servers : []string {
69- " http://localhost:3001" ,
70- " http://localhost:3002" ,
71- " http://localhost:3003" ,
72- },
78+ Servers : []string {
79+ " http://localhost:3001" ,
80+ " http://localhost:3002" ,
81+ " http://localhost:3003" ,
82+ },
7383}))
7484
7585// Or extend your balancer for customization
7686app.Use (proxy.Balancer (proxy.Config {
77- Servers : []string {
78- " http://localhost:3001" ,
79- " http://localhost:3002" ,
80- " http://localhost:3003" ,
81- },
82- ModifyRequest : func (c *fiber.Ctx ) error {
83- c.Request ().Header .Add (" X-Real-IP" , c.IP ())
84- return nil
85- },
86- ModifyResponse : func (c *fiber.Ctx ) error {
87- c.Response ().Header .Del (fiber.HeaderServer )
88- return nil
89- },
87+ Servers : []string {
88+ " http://localhost:3001" ,
89+ " http://localhost:3002" ,
90+ " http://localhost:3003" ,
91+ },
92+ ModifyRequest : func (c *fiber.Ctx ) error {
93+ c.Request ().Header .Add (" X-Real-IP" , c.IP ())
94+ return nil
95+ },
96+ ModifyResponse : func (c *fiber.Ctx ) error {
97+ c.Response ().Header .Del (fiber.HeaderServer )
98+ return nil
99+ },
100+ }))
101+
102+ // Or this way if the balancer is using https and the destination server is only using http.
103+ app.Use (proxy.BalancerForward ([]string {
104+ " http://localhost:3001" ,
105+ " http://localhost:3002" ,
106+ " http://localhost:3003" ,
90107}))
91108```
92109
@@ -95,50 +112,50 @@ app.Use(proxy.Balancer(proxy.Config{
95112``` go
96113// Config defines the config for middleware.
97114type Config struct {
98- // Next defines a function to skip this middleware when returned true.
99- //
100- // Optional. Default: nil
101- Next func (c *fiber.Ctx ) bool
102-
103- // Servers defines a list of <scheme>://<host> HTTP servers,
104- //
105- // which are used in a round-robin manner.
106- // i.e.: "https://foobar.com, http://www.foobar.com"
107- //
108- // Required
109- Servers []string
110-
111- // ModifyRequest allows you to alter the request
112- //
113- // Optional. Default: nil
114- ModifyRequest fiber.Handler
115-
116- // ModifyResponse allows you to alter the response
117- //
118- // Optional. Default: nil
119- ModifyResponse fiber.Handler
120-
121- // Timeout is the request timeout used when calling the proxy client
122- //
123- // Optional. Default: 1 second
124- Timeout time.Duration
125-
126- // Per-connection buffer size for requests' reading.
127- // This also limits the maximum header size.
128- // Increase this buffer if your clients send multi-KB RequestURIs
129- // and/or multi-KB headers (for example, BIG cookies).
130- ReadBufferSize int
115+ // Next defines a function to skip this middleware when returned true.
116+ //
117+ // Optional. Default: nil
118+ Next func (c *fiber.Ctx ) bool
119+
120+ // Servers defines a list of <scheme>://<host> HTTP servers,
121+ //
122+ // which are used in a round-robin manner.
123+ // i.e.: "https://foobar.com, http://www.foobar.com"
124+ //
125+ // Required
126+ Servers []string
127+
128+ // ModifyRequest allows you to alter the request
129+ //
130+ // Optional. Default: nil
131+ ModifyRequest fiber.Handler
132+
133+ // ModifyResponse allows you to alter the response
134+ //
135+ // Optional. Default: nil
136+ ModifyResponse fiber.Handler
137+
138+ // Timeout is the request timeout used when calling the proxy client
139+ //
140+ // Optional. Default: 1 second
141+ Timeout time.Duration
142+
143+ // Per-connection buffer size for requests' reading.
144+ // This also limits the maximum header size.
145+ // Increase this buffer if your clients send multi-KB RequestURIs
146+ // and/or multi-KB headers (for example, BIG cookies).
147+ ReadBufferSize int
131148
132- // Per-connection buffer size for responses' writing.
133- WriteBufferSize int
134-
135- // tls config for the http client.
136- TlsConfig *tls.Config
137-
138- // Client is custom client when client config is complex.
139- // Note that Servers, Timeout, WriteBufferSize, ReadBufferSize and TlsConfig
140- // will not be used if the client are set.
141- Client *fasthttp.LBClient
149+ // Per-connection buffer size for responses' writing.
150+ WriteBufferSize int
151+
152+ // tls config for the http client.
153+ TlsConfig *tls.Config
154+
155+ // Client is custom client when client config is complex.
156+ // Note that Servers, Timeout, WriteBufferSize, ReadBufferSize and TlsConfig
157+ // will not be used if the client are set.
158+ Client *fasthttp.LBClient
142159}
143160```
144161
0 commit comments