proxy

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 23, 2020 License: MIT Imports: 7 Imported by: 0

README

Proxy

Release Discord Test Security Linter

Simple reverse proxy

Install
go get -u github.com/gofiber/fiber
go get -u github.com/gofiber/proxy
Signature
proxy.New(target string) func(*fiber.Ctx)
proxy.Forward(c *fiber.Ctx, target string) error
Functions
Name Signature Description
New New(config ...Config) func(*fiber.Ctx) Returns a middleware that proxies the request
Handler Handler(target string) func(*fiber.Ctx) Returns a handler that proxies the request
Forward func Forward(c *fiber.Ctx, target string) error A function that proxies the requests
Example
package main

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

func main() {
	go proxy()       // Reverse proxy running on port 3000
	go backend3001() // Backend dummy running on port 3001
	go backend3002() // Backend dummy running on port 3002
}

func proxy() {
	app := fiber.New()

	app.Use("/proxy", proxy.New(proxy.Config{
		Targets: []string{
			"127.0.0.1:3001",
			"127.0.0.1:3002",
		},
		Rules: map[string]string{
			"/proxy": "/",
		},
		Methods: []string{"GET"},
	}))

	app.Get("/3001", func(ctx *fiber.Ctx) {
		// Alter request
		ctx.Set("X-Forwarded-For", "3001")
		// Forward request using proxy mw function
		if err := proxy.Forward(ctx, "127.0.0.1:3001"); err != nil {
			ctx.SendStatus(503)
			return
		}
		// Alter response
		ctx.Set("X-Forwarded-By", "3001")
	})

	app.Get("/3002", proxy.Handler("127.0.0.1:3002")) // handler

	app.Listen(3000)
}

func backend3001() {
	app := fiber.New()
	app.Get("/", func(c *fiber.Ctx) {
		c.Send("Hello from the backend server running on port 3001")
	})
	app.Listen(3001)
}

func backend3002() {
	app := fiber.New()
	app.Get("/", func(c *fiber.Ctx) {
		c.Send("Hello from the backend server running on port 3002")
	})
	app.Listen(3002)
}

Test
curl http://localhost:3000/3001
curl http://localhost:3000/3002

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Forward

func Forward(c *fiber.Ctx, target string) error

Forward proxies the Ctx to the target

func Handler

func Handler(target string) func(*fiber.Ctx)

Handler returns a reverse proxy handler

func New

func New(config ...Config) func(*fiber.Ctx)

New returns a new reverse proxy middleware

Types

type Config

type Config struct {
	// Targets is list of backend hosts used to proxy the request.
	// Backend hosts is selected by Round-Robin scheduling.
	// Required. Default: nil
	Targets []string

	// Methods is list of HTTP methods allowed for proxying.
	// Optional. Default: nil
	Methods []string

	// Filter defines a function to skip middleware.
	// Optional. Default: nil
	Filter func(*fiber.Ctx) bool

	// ErrorHandler is a function for handling unexpected errors.
	// Optional. Default: StatusServiceUnavailable
	ErrorHandler func(*fiber.Ctx, error)

	// Rules defines the URL path rewrite rules. The values captured in asterisk can be
	// retrieved by index e.g. $1, $2 and so on.
	// Optional. Default: nil
	// Example:
	// "/old":              "/new",
	// "/api/*":            "/$1",
	// "/js/*":             "/public/javascripts/$1",
	// "/users/*/orders/*": "/user/$1/order/$2",
	Rules map[string]string
	// contains filtered or unexported fields
}

Config holds configuration for the middleware

Jump to

Keyboard shortcuts

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