FileSystem

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

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. Index: "index.html",
  9. Browse: true,
  10. NotFoundFile: "404.html"
  11. }))

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
  12. // Index file for serving a directory.
  13. //
  14. // Optional. Default: "index.html"
  15. Index string
  16. // Enable directory browsing.
  17. //
  18. // Optional. Default: false
  19. Browse bool
  20. // File to return if path is not found. Useful for SPA's.
  21. //
  22. // Optional. Default: ""
  23. NotFoundFile string
  24. }

Default Config

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

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. 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. 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. 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. app.Listen(":3000")
  18. }