embed files
1 file changed, 14 insertions(+), 2 deletions(-)
changed files
M cmd/server/server.go → server.go
@@ -1,6 +1,7 @@ package main import ( + "embed" "fmt" "log" "net/http"@@ -30,6 +31,9 @@ type Host struct { Fiber *fiber.App } + +//go:embed all:public/* +var fs embed.FS func main() { err := sentry.Init(sentry.ClientOptions{@@ -93,12 +97,20 @@ })) website.Use(recover.New(recover.Config{})) + files := http.FS(fs) notFoundHandler := func(c *fiber.Ctx) error { - return c.Status(fiber.StatusNotFound).SendFile("public/404.html") + c.Status(fiber.StatusNotFound).Type("html", "utf-8") + content, err := fs.ReadFile("public/404.html") + if err != nil { + content = []byte("404 Not Found") + c.Type("txt") + } + return c.Send(content) } website.Get("/404.html", notFoundHandler) website.Use("/", filesystem.New(filesystem.Config{ - Root: http.Dir("./public"), + Root: files, + PathPrefix: "public", ContentTypeCharset: "utf-8", MaxAge: int((24 * time.Hour).Seconds()), }))