Embed
FileServer middleware for Fiber
Special thanks and credits to Alireza Salary
Install
go get -u github.com/gofiber/fiber
go get -u github.com/gofiber/embed
Signature
embed.New(config ...embed.Config) func(c *fiber.Ctx)
Config
Property |
Type |
Description |
Default |
Index |
string |
Index file name |
index.html |
Browse |
bool |
Enable directory browsing |
false |
Root |
http.FileSystem |
http.FileSystem to use |
nil |
ErrorHandler |
func(*fiber.Ctx, error) |
Error handler |
InternalServerError |
pkger
package main
import (
"github.com/gofiber/fiber"
"github.com/gofiber/embed"
"github.com/markbates/pkger"
)
func main() {
app := fiber.New()
app.Use("/assets", embed.New(embed.Config{
Root: pkger.Dir("/assets"),
}))
app.Listen(8080)
}
packr
package main
import (
"github.com/gofiber/fiber"
"github.com/gofiber/embed"
"github.com/gobuffalo/packr/v2"
)
func main() {
app := fiber.New()
app.Use("/assets", embed.New(embed.Config{
Root: packr.New("Assets Box", "/assets"),
}))
app.Listen(8080)
}
go.rice
package main
import (
"github.com/gofiber/fiber"
"github.com/gofiber/embed"
"github.com/GeertJohan/go.rice"
)
func main() {
app := fiber.New()
app.Use("/assets", embed.New(embed.Config{
Root: rice.MustFindBox("assets").HTTPBox(),
}))
app.Listen(8080)
}
fileb0x
package main
import (
"github.com/gofiber/fiber"
"github.com/gofiber/embed"
"<Your go module>/myEmbeddedFiles"
)
func main() {
app := fiber.New()
app.Use("/assets", embed.New(embed.Config{
Root: myEmbeddedFiles.HTTP,
}))
app.Listen(8080)
}
statik
package main
import (
"log"
"github.com/gofiber/fiber"
"github.com/gofiber/embed"
"<Your go module>/statik"
fs "github.com/rakyll/statik/fs"
)
func main() {
statik, err := fs.New()
if err != nil {
log.Fatal(err)
}
app := fiber.New()
app.Use("/", embed.New(embed.Config{
Root: statikFS,
}))
app.Listen(8080)
}