mirror of
https://github.com/gofiber/fiber.git
synced 2025-02-22 09:33:21 +00:00
✏️ FS MW: add example of embedding directory (#1197)
Co-authored-by: hi019 <65871571+hi019@users.noreply.github.com>
This commit is contained in:
parent
983919fd18
commit
a179c6665c
@ -68,6 +68,7 @@ package main
|
||||
|
||||
import (
|
||||
"embed"
|
||||
"io/fs"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
@ -75,9 +76,14 @@ import (
|
||||
"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()
|
||||
|
||||
@ -85,6 +91,15 @@ func main() {
|
||||
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"))
|
||||
}
|
||||
```
|
||||
|
Loading…
x
Reference in New Issue
Block a user