Categorygithub.com/gofiber/proxy
modulepackage
0.1.2
Repository: https://github.com/gofiber/proxy.git
Documentation: pkg.go.dev

# 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

NameSignatureDescription
NewNew(config ...Config) func(*fiber.Ctx)Returns a middleware that proxies the request
HandlerHandler(target string) func(*fiber.Ctx)Returns a handler that proxies the request
Forwardfunc Forward(c *fiber.Ctx, target string) errorA 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

# Functions

Forward proxies the Ctx to the target.
Handler returns a reverse proxy handler.
New returns a new reverse proxy middleware.

# Structs

Config holds configuration for the middleware.