gofiber / fiber

⚡️ Express inspired web framework written in Go
https://gofiber.io
MIT License
33.8k stars 1.66k forks source link

🤗 [Question]: FileSystem does not work as expected #2506

Closed rngallen closed 1 year ago

rngallen commented 1 year ago

Question Description

Am using file system to save html and static files. Server returns 404 for static files

Code Snippet (optional)

package main

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

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

//go:embed views/*
var viewsfs embed.FS

// var staticfs embed.FS
var embedDirStatic embed.FS

func main() {

    engine := html.NewFileSystem(http.FS(viewsfs), ".html")

    app := fiber.New(fiber.Config{
        ServerHeader:  "GOFIBER",
        StrictRouting: true,
        CaseSensitive: true,
        AppName:       "Go Htmx",
        Views:         engine,
        ViewsLayout:   "views/base/main",
    })

    app.Use("/static", filesystem.New(filesystem.Config{
        Root:       http.FS(embedDirStatic),
        PathPrefix: "static",
        Browse:     true,
    }))

    app.Get("", func(c *fiber.Ctx) error {
        return c.Render("views/index", fiber.Map{"Title": "Hello Raheel"})
    })

    log.Fatal(app.Listen(":3030"))
}

Checklist:

ReneWerner87 commented 1 year ago

Embed comment for the embedDirStatic was missing