FileSystem

Filesystem middleware for Fiber that enables you to serve files from a directory.

⚠️ :params & :optionals? within the prefix path are not supported!

Signatures

  1. func New(config Config) fiber.Handler

Examples

Import the middleware package that is part of the Fiber web framework

  1. import (
  2. "github.com/gofiber/fiber/v2"
  3. "github.com/gofiber/fiber/v2/middleware/filesystem"
  4. )

After you initiate your Fiber app, you can use the following possibilities:

  1. // Provide a minimal config
  2. app.Use(filesystem.New(filesystem.Config{
  3. Root: http.Dir("./assets")
  4. }))
  5. // Or extend your config for customization
  6. app.Use(filesystem.New(filesystem.Config{
  7. Root: http.Dir("./assets"),
  8. Browse: true,
  9. Index: "index.html",
  10. NotFoundFile: "404.html",
  11. MaxAge: 3600,
  12. }))

pkger

https://github.com/markbates/pkger

  1. package main
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. "github.com/gofiber/fiber/v2/middleware/filesystem"
  5. "github.com/markbates/pkger"
  6. )
  7. func main() {
  8. app := fiber.New()
  9. app.Use("/assets", filesystem.New(filesystem.Config{
  10. Root: pkger.Dir("/assets"),
  11. })
  12. log.Fatal(app.Listen(":3000"))
  13. }

packr

https://github.com/gobuffalo/packr

  1. package main
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. "github.com/gofiber/fiber/v2/middleware/filesystem"
  5. "github.com/gobuffalo/packr/v2"
  6. )
  7. func main() {
  8. app := fiber.New()
  9. app.Use("/assets", filesystem.New(filesystem.Config{
  10. Root: packr.New("Assets Box", "/assets"),
  11. })
  12. log.Fatal(app.Listen(":3000"))
  13. }

go.rice

https://github.com/GeertJohan/go.rice

  1. package main
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. "github.com/gofiber/fiber/v2/middleware/filesystem"
  5. "github.com/GeertJohan/go.rice"
  6. )
  7. func main() {
  8. app := fiber.New()
  9. app.Use("/assets", filesystem.New(filesystem.Config{
  10. Root: rice.MustFindBox("assets").HTTPBox(),
  11. })
  12. log.Fatal(app.Listen(":3000"))
  13. }

fileb0x

https://github.com/UnnoTed/fileb0x

  1. package main
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. "github.com/gofiber/fiber/v2/middleware/filesystem"
  5. "<Your go module>/myEmbeddedFiles"
  6. )
  7. func main() {
  8. app := fiber.New()
  9. app.Use("/assets", filesystem.New(filesystem.Config{
  10. Root: myEmbeddedFiles.HTTP,
  11. })
  12. log.Fatal(app.Listen(":3000"))
  13. }

statik

https://github.com/rakyll/statik

  1. package main
  2. import (
  3. "github.com/gofiber/fiber/v2"
  4. "github.com/gofiber/fiber/v2/middleware/filesystem"
  5. "<Your go module>/statik"
  6. fs "github.com/rakyll/statik/fs"
  7. )
  8. func main() {
  9. statik, err := fs.New()
  10. if err != nil {
  11. panic(err)
  12. }
  13. app := fiber.New()
  14. app.Use("/", filesystem.New(filesystem.Config{
  15. Root: statikFS,
  16. })
  17. log.Fatal(app.Listen(":3000"))
  18. }

Config

  1. // Config defines the config for middleware.
  2. type Config struct {
  3. // Next defines a function to skip this middleware when returned true.
  4. //
  5. // Optional. Default: nil
  6. Next func(c *fiber.Ctx) bool
  7. // Root is a FileSystem that provides access
  8. // to a collection of files and directories.
  9. //
  10. // Required. Default: nil
  11. Root http.FileSystem `json:"-"`
  12. // Enable directory browsing.
  13. //
  14. // Optional. Default: false
  15. Browse bool `json:"browse"`
  16. // Index file for serving a directory.
  17. //
  18. // Optional. Default: "index.html"
  19. Index string `json:"index"`
  20. // The value for the Cache-Control HTTP-header
  21. // that is set on the file response. MaxAge is defined in seconds.
  22. //
  23. // Optional. Default value 0.
  24. MaxAge int `json:"max_age"`
  25. // File to return if path is not found. Useful for SPA's.
  26. //
  27. // Optional. Default: ""
  28. NotFoundFile string `json:"not_found_file"`
  29. }

Default Config

  1. var ConfigDefault = Config{
  2. Next: nil,
  3. Root: nil,
  4. Browse: false,
  5. Index: "/index.html",
  6. MaxAge: 0,
  7. }