Skip to content

Commit

Permalink
✏️ FS MW: add example of embedding directory (#1197)
Browse files Browse the repository at this point in the history
Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com>
  • Loading branch information
iredmail and hi019 committed Mar 2, 2021
1 parent 983919f commit a179c66
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions middleware/filesystem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,38 @@ package main

import (
"embed"
"io/fs"
"log"
"net/http"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
)

// Embed a single file
//go:embed index.html
var f embed.FS

// Embed a directory
//go:embed static/*
var embedDirStatic embed.FS

func main() {
app := fiber.New()

app.Use("/", filesystem.New(filesystem.Config{
Root: http.FS(f),
}))

// Access file "image.png" under `static/` directory via URL: `http://<server>/static/image.png`.
// With `http.FS(embedDirStatic)`, you have to access it via URL:
// `http://<server>/static/static/image.png`.
subFS, _ := fs.Sub(embedDirStatic, "static")
app.Use("/static", filesystem.New(filesystem.Config{
Root: http.FS(subFS),
Browse: true,
}))

log.Fatal(app.Listen(":3000"))
}
```
Expand Down

3 comments on commit a179c66

@dechristopher
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth noting that http.FS() isn't available before go 1.16.

@iredmail
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth noting that http.FS() isn't available before go 1.16.

Both embed and http.FS are introduced in Go 1.16, so it's ok. :)

@dechristopher
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Worth noting that http.FS() isn't available before go 1.16.

Both embed and http.FS are introduced in Go 1.16, so it's ok. :)

Truuuuuue haha :)

Please sign in to comment.