Skip to content

K0enM/fiber_vhost

Repository files navigation

fiber_vhost

Vhost (Virtual host) middleware for Fiber that enables the use of virtual hosts based on the Host Header. It is based on the Express vhost middleware.

Table of Contents

Signatures

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

Data added to fiber context

type struct Vhost {
	Host string
	Hostname string
	HostnameRegexpString
}

Examples

First ensure that the appropiate packages are imported

import (
	"github.com/gofiber/fiber/v2"
	"github.com/K0enM/fiber_vhost"
)

Initialization / Default Config

// Default middleware config
app.Use(fiber_vhost.New())

Matching example.com and define basic Handler function

app.Use(fiber_vhost.New(fiber_vhost.Config{
	Hostname: "example.com",
	Handler: func(c *fiber.Ctx) error {
		return c.SendString("Inside the Vhost Handler")
	},
}))

Matching with a wildcard in the hostname

app.Use(fiber_vhost.New(fiber_vhost.Config{
	Hostname: "*.example.com",
	Handler: func(c *fiber.Ctx) error {
		return c.SendString("Matched with a wildcard")
	},
}))

Matching with a regexp

app.Use(fiber_vhost.New(fiber_vhost.Config{
	HostnameRegexp: "",
	Handler: func(c *fiber.Ctx) error {
		return c.SendString("Matched with a regexp")
	},
}))

Define Next function to decide if to skip this middleware

app.Use(fiber_vhost.New(fiber_vhost.Config{
	Next: func(c *fiber.Ctx) bool {
		if c.Get("X-Skip-Vhost") == "true" {
			return true
		}

		return false
	},	
	Hostname: "example.com",
	Handler: func(c *fiber.Ctx) error {
		return c.SendString("Inside the Vhost Handler")
	},
}))

Config

type Config struct {
	Next func(c *fiber.Ctx) bool

	Hostname string

	Handler func(c *fiber.Ctx) error

	HostnameRegexp string
}

Default Config

var ConfigDefault = Config{
	Next: nil,
	Hostname: "vhost.local",
	Handler: func(c *fiber.Ctx) error {
		return nil
	},
	HostnameRegexpString: "",
}

TODO

  • Create documentation
  • Create & check workflows`

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages