proxy

package
v2.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 3, 2020 License: MIT Imports: 5 Imported by: 47

README

Proxy

Proxy middleware for Fiber that allows you to proxy requests to multiple servers.

Table of Contents
Signatures
func Balancer(config Config) fiber.Handler
func Forward(addr string) fiber.Handler
func Do(c *fiber.Ctx, addr string) error
Examples

Import the middleware package that is part of the Fiber web framework

import (
	"github.com/gofiber/fiber/v2"
	"github.com/gofiber/fiber/v2/middleware/proxy"
)

After you initiate your Fiber app, you can use the following possibilities:

// Forward to url
app.Get("/gif", proxy.Forward("https://i.imgur.com/IWaBepg.gif"))

// Make request within handler
app.Get("/:id", func(c *fiber.Ctx) error {
	url := "https://i.imgur.com/"+c.Params("id")+".gif"
	if err := proxy.Do(c, url); err != nil {
		return err
	}
	// Remove Server header from response
	c.Response().Header.Del(fiber.HeaderServer)
	return nil
})

// Minimal round robin balancer
app.Use(proxy.Balancer(proxy.Config{
	Servers: []string{
		"http://localhost:3001",
		"http://localhost:3002",
		"http://localhost:3003",
	},
}))

// Or extend your balancer for customization
app.Use(proxy.Balancer(proxy.Config{
	Servers: []string{
		"http://localhost:3001",
		"http://localhost:3002",
		"http://localhost:3003",
	},
	ModifyRequest: func(c *fiber.Ctx) error {
		c.Request().Header.Add("X-Real-IP", c.IP())
		return nil
	},
	ModifyResponse: func(c *fiber.Ctx) error {
		c.Response().Header.Del(fiber.HeaderServer)
		return nil
	},
}))
Config
// Config defines the config for middleware.
type Config struct {
	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool

	// Servers defines a list of <scheme>://<host> HTTP servers,
	//
	// which are used in a round-robin manner.
	// i.e.: "https://foobar.com, http://www.foobar.com"
	//
	// Required
	Servers []string

	// ModifyRequest allows you to alter the request
	//
	// Optional. Default: nil
	ModifyRequest fiber.Handler

	// ModifyResponse allows you to alter the response
	//
	// Optional. Default: nil
	ModifyResponse fiber.Handler
}
Default Config
// ConfigDefault is the default config
var ConfigDefault = Config{
	Next: nil,
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigDefault = Config{
	Next:           nil,
	ModifyRequest:  nil,
	ModifyResponse: nil,
}

ConfigDefault is the default config

Functions

func Balancer added in v2.0.3

func Balancer(config Config) fiber.Handler

Balancer creates a load balancer among multiple upstream servers

func Do added in v2.0.3

func Do(c *fiber.Ctx, addr string) error

Do performs the given http request and fills the given http response. This method can be used within a fiber.Handler

func Forward added in v2.0.3

func Forward(addr string) fiber.Handler

Forward performs the given http request and fills the given http response. This method will return an fiber.Handler

func New

func New(config Config) fiber.Handler

New is deprecated

Types

type Config

type Config struct {
	// Next defines a function to skip this middleware when returned true.
	//
	// Optional. Default: nil
	Next func(c *fiber.Ctx) bool

	// Servers defines a list of <scheme>://<host> HTTP servers,
	//
	// which are used in a round-robin manner.
	// i.e.: "https://foobar.com, http://www.foobar.com"
	//
	// Required
	Servers []string

	// ModifyRequest allows you to alter the request
	//
	// Optional. Default: nil
	ModifyRequest fiber.Handler

	// ModifyResponse allows you to alter the response
	//
	// Optional. Default: nil
	ModifyResponse fiber.Handler
}

Config defines the config for middleware.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL