Middleware

middleware/static

github.com/gofiber/fiber/v3/middleware/static

import "github.com/gofiber/fiber/v3/middleware/static"

Variables

ConfigDefault is the default config

Source: middleware/static/config.go:80

var ConfigDefault = Config{
	IndexNames:    []string{"index.html"},
	CacheDuration: 10 * time.Second,
}

Source: middleware/static/static.go:26

var ErrInvalidPath = errors.New("invalid path")

Functions

func New(root string, cfg ...Config) fiber.Handler

Source: middleware/static/static.go:224

New creates a new middleware handler. The root argument specifies the root directory from which to serve static assets.

Note: Root has to be string or fs.FS; otherwise, it will panic.

Types

type Config

Source: middleware/static/config.go:11

Config defines the config for middleware.

type Config struct {
	// FS is the file system to serve the static files from.
	// You can use interfaces compatible with fs.FS like embed.FS, os.DirFS etc.
	//
	// Optional. Default: nil
	FS fs.FS

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

	// ModifyResponse defines a function that allows you to alter the response.
	//
	// Optional. Default: nil
	ModifyResponse fiber.Handler

	// NotFoundHandler defines a function to handle when the path is not found.
	//
	// Optional. Default: nil
	NotFoundHandler fiber.Handler

	// The names of the index files for serving a directory.
	//
	// Optional. Default: []string{"index.html"}.
	IndexNames []string `json:"index"`

	// Expiration duration for inactive file handlers.
	// Use a negative time.Duration to disable it.
	//
	// Optional. Default: 10 * time.Second.
	CacheDuration time.Duration `json:"cache_duration"`

	// The value for the Cache-Control HTTP-header
	// that is set on the file response. MaxAge is defined in seconds.
	//
	// Optional. Default: 0.
	MaxAge int `json:"max_age"`

	// When set to true, the server tries minimizing CPU usage by caching compressed files.
	// This works differently than the github.com/gofiber/compression middleware.
	//
	// Optional. Default: false
	Compress bool `json:"compress"`

	// When set to true, enables byte range requests. A request for several
	// ranges, or whose If-Range does not match, gets the whole file.
	//
	// Optional. Default: false
	ByteRange bool `json:"byte_range"`

	// When set to true, enables directory browsing.
	//
	// Optional. Default: false.
	Browse bool `json:"browse"`

	// When set to true, enables direct download.
	//
	// Optional. Default: false.
	Download bool `json:"download"`

	// When set to true, symbolic links that leave the root, and absolute ones,
	// are followed. Without FS only; leave it off unless such links are intended.
	//
	// Optional. Default: false.
	AllowSymlinksOutsideRoot bool `json:"allow_symlinks_outside_root"`
}
Fields
  • FS fs.FS

    FS is the file system to serve the static files from. You can use interfaces compatible with fs.FS like embed.FS, os.DirFS etc.

    Optional. Default: nil

  • Next func(c fiber.Ctx) bool

    Next defines a function to skip this middleware when returned true.

    Optional. Default: nil

  • ModifyResponse fiber.Handler

    ModifyResponse defines a function that allows you to alter the response.

    Optional. Default: nil

  • NotFoundHandler fiber.Handler

    NotFoundHandler defines a function to handle when the path is not found.

    Optional. Default: nil

  • IndexNames []string `json:"index"`

    The names of the index files for serving a directory.

    Optional. Default: []string{"index.html"}.

  • CacheDuration time.Duration `json:"cache_duration"`

    Expiration duration for inactive file handlers. Use a negative time.Duration to disable it.

    Optional. Default: 10 * time.Second.

  • MaxAge int `json:"max_age"`

    The value for the Cache-Control HTTP-header that is set on the file response. MaxAge is defined in seconds.

    Optional. Default: 0.

  • Compress bool `json:"compress"`

    When set to true, the server tries minimizing CPU usage by caching compressed files. This works differently than the github.com/gofiber/compression middleware.

    Optional. Default: false

  • ByteRange bool `json:"byte_range"`

    When set to true, enables byte range requests. A request for several ranges, or whose If-Range does not match, gets the whole file.

    Optional. Default: false

  • Browse bool `json:"browse"`

    When set to true, enables directory browsing.

    Optional. Default: false.

  • Download bool `json:"download"`

    When set to true, enables direct download.

    Optional. Default: false.

  • AllowSymlinksOutsideRoot bool `json:"allow_symlinks_outside_root"`

    When set to true, symbolic links that leave the root, and absolute ones, are followed. Without FS only; leave it off unless such links are intended.

    Optional. Default: false.