diff --git a/docs/whats_new.md b/docs/whats_new.md index fe193485045..ca352d70f40 100644 --- a/docs/whats_new.md +++ b/docs/whats_new.md @@ -47,6 +47,7 @@ DRAFT section We have made several changes to the Fiber app, including: * Listen -> unified with config +* Static -> has been removed and moved to [static middleware](./middleware/static.md) * app.Config properties moved to listen config * DisableStartupMessage * EnablePrefork -> previously Prefork @@ -270,9 +271,8 @@ DRAFT section ### Filesystem -:::caution -DRAFT section -::: +We've decided to remove filesystem middleware to clear up the confusion between static and filesystem middleware. +Now, static middleware can do everything that filesystem middleware and static do. You can check out [static middleware](./middleware/static.md) or [migration guide](#📋-migration-guide) to see what has been changed. ### Monitor @@ -295,6 +295,34 @@ Monitor middleware is now in Contrib package. ### 🚀 App +#### Static + +Since we've removed `app.Static()`, you need to move methods to static middleware like the example below: + +```go +// Before +app.Static("/", "./public") +app.Static("/prefix", "./public") +app.Static("/prefix", "./public", Static{ + Index: "index.htm", +}) +app.Static("*", "./public/index.html") +``` + +```go +// After +app.Get("/*", ,static.New("./public")) +app.Get("/prefix*", static.New("./public")) +app.Get("/prefix*", static.New("./public", static.Config{ + IndexNames: []string{"index.htm", "index.html"}, +})) +app.Get("*", static.New("./public/index.html")) +``` + +:::caution +You have to put `*` to the end of the route if you don't define static route with `app.Use`. +::: + ### 🗺 Router ### 🧠 Context @@ -328,4 +356,35 @@ app.Use(cors.New(cors.Config{ ExposeHeaders: []string{"Content-Length"}, })) ``` -... + +#### Filesystem + +You need to move filesystem middleware to static middleware due to it has been removed from the core. + +```go +// Before +app.Use(filesystem.New(filesystem.Config{ + Root: http.Dir("./assets"), +})) + +app.Use(filesystem.New(filesystem.Config{ + Root: http.Dir("./assets"), + Browse: true, + Index: "index.html", + MaxAge: 3600, +})) +``` + +```go +// After +app.Use(static.New("", static.Config{ + FS: os.DirFS("./assets"), +})) + +app.Use(static.New("", static.Config{ + FS: os.DirFS("./assets"), + Browse: true, + IndexNames: []string{"index.html"}, + MaxAge: 3600, +})) +``` \ No newline at end of file diff --git a/middleware/static/static_test.go b/middleware/static/static_test.go index 63681911516..50726cf3c1a 100644 --- a/middleware/static/static_test.go +++ b/middleware/static/static_test.go @@ -126,7 +126,9 @@ func Test_Static_Disable_Cache(t *testing.T) { require.NoError(t, err) // Remove the file even if the test fails - defer os.Remove("../../.github/test.txt") + defer func() { + require.NoError(t, os.Remove("../../.github/test.txt")) + }() app.Get("/*", New("../../.github/", Config{ CacheDuration: -1, @@ -148,7 +150,7 @@ func Test_Static_Disable_Cache(t *testing.T) { body, err = io.ReadAll(resp.Body) require.NoError(t, err) - require.Equal(t, string(body), "Cannot GET /test.txt") + require.Equal(t, "Cannot GET /test.txt", string(body)) } // go test -run Test_Static_Download