Back to Fiber

EnvVar

docs/middleware/envvar.md

3.2.01.4 KB
Original Source

EnvVar

EnvVar middleware for Fiber exposes environment variables with configurable options.

Signatures

go
func New(config ...Config) fiber.Handler

Examples

Import the middleware package:

go
import (
    "github.com/gofiber/fiber/v3"
    "github.com/gofiber/fiber/v3/middleware/envvar"
)

Once your Fiber app is initialized, configure the middleware as shown:

go
// Initialize default config (exports no variables)
app.Use("/expose/envvars", envvar.New())

// Or extend your config for customization
app.Use("/expose/envvars", envvar.New(
    envvar.Config{
        ExportVars: map[string]string{"testKey": "", "testDefaultKey": "testDefaultVal"},
    }),
)

:::note Mount the middleware on a path; it cannot be used without one. :::

Response

Sample response:

json
{
  "vars": {
    "someEnvVariable": "someValue",
    "anotherEnvVariable": "anotherValue"
  }
}

Config

PropertyTypeDescriptionDefault
ExportVarsmap[string]stringExportVars lists the environment variables to expose.nil

Default Config

go
Config{}
// Exports no environment variables