π Whats New in v3
π Welcomeβ
We are excited to announce the release of Fiber v3! π
In this guide, we'll walk you through the most important changes in Fiber v3
and show you how to migrate your existing Fiber v2
applications to Fiber v3
.
Here's a quick overview of the changes in Fiber v3
:
- π App
- πΊοΈ Router
- π§ Context
- π Binding
- ποΈ Redirect
- π Client package
- π§° Generic functions
- π Log
- 𧬠Middlewares
- π Migration guide
Drop for old Go versionsβ
Fiber v3
drops support for Go versions below 1.23
. We recommend upgrading to Go 1.23
or higher to use Fiber v3
.
π Appβ
We have made several changes to the Fiber app, including:
- Listen: The
Listen
method has been unified with the configuration, allowing for more streamlined setup. - Static: The
Static
method has been removed and its functionality has been moved to the static middleware. - app.Config properties: Several properties have been moved to the listen configuration:
DisableStartupMessage
EnablePrefork
(previouslyPrefork
)EnablePrintRoutes
ListenerNetwork
(previouslyNetwork
)
- Trusted Proxy Configuration: The
EnabledTrustedProxyCheck
has been moved toapp.Config.TrustProxy
, andTrustedProxies
has been moved toTrustProxyConfig.Proxies
. - XMLDecoder Config Property: The
XMLDecoder
property has been added to allow usage of 3rd-party XML libraries in XML binder.
New Methodsβ
- RegisterCustomBinder: Allows for the registration of custom binders.
- RegisterCustomConstraint: Allows for the registration of custom constraints.
- NewCtxFunc: Introduces a new context function.
Removed Methodsβ
- Mount: Use
app.Use()
instead. - ListenTLS: Use
app.Listen()
withtls.Config
. - ListenTLSWithCertificate: Use
app.Listen()
withtls.Config
. - ListenMutualTLS: Use
app.Listen()
withtls.Config
. - ListenMutualTLSWithCertificate: Use
app.Listen()
withtls.Config
.
Method Changesβ
- Test: The
Test
method has replaced the timeout parameter with a configuration parameter.-1
represents no timeout, and0
represents no timeout. - Listen: Now has a configuration parameter.
- Listener: Now has a configuration parameter.
Custom Ctx Interface in Fiber v3β
Fiber v3 introduces a customizable Ctx
interface, allowing developers to extend and modify the context to fit their needs. This feature provides greater flexibility and control over request handling.
Idea Behind Custom Ctx Classesβ
The idea behind custom Ctx
classes is to give developers the ability to extend the default context with additional methods and properties tailored to the specific requirements of their application. This allows for better request handling and easier implementation of specific logic.
NewCtxFuncβ
The NewCtxFunc
method allows you to customize the Ctx
struct as needed.
func (app *App) NewCtxFunc(function func(app *App) CustomCtx)
Example
Hereβs an example of how to customize the Ctx
interface:
package main
import (
"log"
"github.com/gofiber/fiber/v3"
)
type CustomCtx struct {
fiber.Ctx
}
// Custom method
func (c *CustomCtx) CustomMethod() string {
return "custom value"
}
func main() {
app := fiber.New()
app.NewCtxFunc(func(app *fiber.App) fiber.Ctx {
return &CustomCtx{
Ctx: *fiber.NewCtx(app),
}
})
app.Get("/", func(c fiber.Ctx) error {
customCtx := c.(*CustomCtx)
return c.SendString(customCtx.CustomMethod())
})
log.Fatal(app.Listen(":3000"))
}
In this example, a custom context CustomCtx
is created with an additional method CustomMethod
. The NewCtxFunc
method is used to replace the default context with the custom one.
Configurable TLS Minimum Versionβ
We have added support for configuring the TLS minimum version. This field allows you to set the TLS minimum version for TLSAutoCert and the server listener.
app.Listen(":444", fiber.ListenConfig{TLSMinVersion: tls.VersionTLS12})
TLS AutoCert support (ACME / Let's Encrypt)β
We have added native support for automatic certificates management from Let's Encrypt and any other ACME-based providers.
// Certificate manager
certManager := &autocert.Manager{
Prompt: autocert.AcceptTOS,
// Replace with your domain name
HostPolicy: autocert.HostWhitelist("example.com"),
// Folder to store the certificates
Cache: autocert.DirCache("./certs"),
}
app.Listen(":444", fiber.ListenConfig{
AutoCertManager: certManager,
})
πΊ Routerβ
We have slightly adapted our router interface
HTTP method registrationβ
In v2
one handler was already mandatory when the route has been registered, but this was checked at runtime and was not correctly reflected in the signature, this has now been changed in v3
to make it more explicit.
- Get(path string, handlers ...Handler) Router
+ Get(path string, handler Handler, middleware ...Handler) Router
- Head(path string, handlers ...Handler) Router
+ Head(path string, handler Handler, middleware ...Handler) Router
- Post(path string, handlers ...Handler) Router
+ Post(path string, handler Handler, middleware ...Handler) Router
- Put(path string, handlers ...Handler) Router
+ Put(path string, handler Handler, middleware ...Handler) Router
- Delete(path string, handlers ...Handler) Router
+ Delete(path string, handler Handler, middleware ...Handler) Router
- Connect(path string, handlers ...Handler) Router
+ Connect(path string, handler Handler, middleware ...Handler) Router
- Options(path string, handlers ...Handler) Router
+ Options(path string, handler Handler, middleware ...Handler) Router
- Trace(path string, handlers ...Handler) Router
+ Trace(path string, handler Handler, middleware ...Handler) Router
- Patch(path string, handlers ...Handler) Router
+ Patch(path string, handler Handler, middleware ...Handler) Router
- All(path string, handlers ...Handler) Router
+ All(path string, handler Handler, middleware ...Handler) Router
Route chainingβ
The route method is now like Express
which gives you the option of a different notation and allows you to concatenate the route declaration.
- Route(prefix string, fn func(router Router), name ...string) Router
+ Route(path string) Register
Example
app.Route("/api").Route("/user/:id?")
.Get(func(c fiber.Ctx) error {
// Get user
return c.JSON(fiber.Map{"message": "Get user", "id": c.Params("id")})
})
.Post(func(c fiber.Ctx) error {
// Create user
return c.JSON(fiber.Map{"message": "User created"})
})
.Put(func(c fiber.Ctx) error {
// Update user
return c.JSON(fiber.Map{"message": "User updated", "id": c.Params("id")})
})
.Delete(func(c fiber.Ctx) error {
// Delete user
return c.JSON(fiber.Map{"message": "User deleted", "id": c.Params("id")})
})
Here you can find more information.
Middleware registrationβ
We have aligned our method for middlewares closer to Express
and now also support the Use
of multiple prefixes.
Registering a subapp is now also possible via the Use
method instead of the old app.Mount
method.
Example
// register mulitple prefixes
app.Use(["/v1", "/v2"], func(c fiber.Ctx) error {
// Middleware for /v1 and /v2
return c.Next()
})
// define subapp
api := fiber.New()
api.Get("/user", func(c fiber.Ctx) error {
return c.SendString("User")
})
// register subapp
app.Use("/api", api)
To enable the routing changes above we had to slightly adjust the signature of the Add
method.
- Add(method, path string, handlers ...Handler) Router
+ Add(methods []string, path string, handler Handler, middleware ...Handler) Router
Test Configβ
The app.Test()
method now allows users to customize their test configurations:
Example
// Create a test app with a handler to test
app := fiber.New()
app.Get("/", func(c fiber.Ctx) {
return c.SendString("hello world")
})
// Define the HTTP request and custom TestConfig to test the handler
req := httptest.NewRequest(MethodGet, "/", nil)
testConfig := fiber.TestConfig{
Timeout: 0,
FailOnTimeout: false,
}
// Test the handler using the request and testConfig
resp, err := app.Test(req, testConfig)
To provide configurable testing capabilities, we had to change
the signature of the Test
method.
- Test(req *http.Request, timeout ...time.Duration) (*http.Response, error)
+ Test(req *http.Request, config ...fiber.TestConfig) (*http.Response, error)
The TestConfig
struct provides the following configuration options:
Timeout
: The duration to wait before timing out the test. Use 0 for no timeout.FailOnTimeout
: Controls the behavior when a timeout occurs:- When true, the test will return an
os.ErrDeadlineExceeded
if the test exceeds theTimeout
duration. - When false, the test will return the partial response received before timing out.
- When true, the test will return an
If a custom TestConfig
isn't provided, then the following will be used:
testConfig := fiber.TestConfig{
Timeout: time.Second,
FailOnTimeout: true,
}
Note: Using this default is NOT the same as providing an empty TestConfig
as an argument to app.Test()
.
An empty TestConfig
is the equivalent of:
testConfig := fiber.TestConfig{
Timeout: 0,
FailOnTimeout: false,
}
π§ Contextβ
New Featuresβ
- Cookie now allows Partitioned cookies for CHIPS support. CHIPS (Cookies Having Independent Partitioned State) is a feature that improves privacy by allowing cookies to be partitioned by top-level site, mitigating cross-site tracking.
New Methodsβ
- AutoFormat: Similar to Express.js, automatically formats the response based on the request's
Accept
header. - Host: Similar to Express.js, returns the host name of the request.
- Port: Similar to Express.js, returns the port number of the request.
- IsProxyTrusted: Checks the trustworthiness of the remote IP.
- Reset: Resets context fields for server handlers.
- Schema: Similar to Express.js, returns the schema (HTTP or HTTPS) of the request.
- SendStream: Similar to Express.js, sends a stream as the response.
- SendStreamWriter: Sends a stream using a writer function.
- SendString: Similar to Express.js, sends a string as the response.
- String: Similar to Express.js, converts a value to a string.
- ViewBind: Binds data to a view, replacing the old
Bind
method. - CBOR: Introducing CBOR binary encoding format for both request & response body. CBOR is a binary data serialization format which is both compact and efficient, making it ideal for use in web applications.
- Drop: Terminates the client connection silently without sending any HTTP headers or response body. This can be used for scenarios where you want to block certain requests without notifying the client, such as mitigating DDoS attacks or protecting sensitive endpoints from unauthorized access.
- End: Similar to Express.js, immediately flushes the current response and closes the underlying connection.
Removed Methodsβ
- AllParams: Use
c.Bind().URL()
instead. - ParamsInt: Use
Params
with generic types. - QueryBool: Use
Query
with generic types. - QueryFloat: Use
Query
with generic types. - QueryInt: Use
Query
with generic types. - BodyParser: Use
c.Bind().Body()
instead. - CookieParser: Use
c.Bind().Cookie()
instead. - ParamsParser: Use
c.Bind().URL()
instead. - RedirectToRoute: Use
c.Redirect().Route()
instead. - RedirectBack: Use
c.Redirect().Back()
instead. - ReqHeaderParser: Use
c.Bind().Header()
instead.
Changed Methodsβ
- Bind: Now used for binding instead of view binding. Use
c.ViewBind()
for view binding. - Format: Parameter changed from
body interface{}
tohandlers ...ResFmt
. - Redirect: Use
c.Redirect().To()
instead. - SendFile: Now supports different configurations using a config parameter.
- Context: Renamed to
RequestCtx
to correspond with the FastHTTP Request Context. - UserContext: Renamed to
Context
, which returns acontext.Context
object. - SetUserContext: Renamed to
SetContext
.
SendStreamWriterβ
In v3, we introduced support for buffered streaming with the addition of the SendStreamWriter
method:
func (c Ctx) SendStreamWriter(streamWriter func(w *bufio.Writer))
With this new method, you can implement:
- Server-Side Events (SSE)
- Large file downloads
- Live data streaming
app.Get("/sse", func(c fiber.Ctx) {
c.Set("Content-Type", "text/event-stream")
c.Set("Cache-Control", "no-cache")
c.Set("Connection", "keep-alive")
c.Set("Transfer-Encoding", "chunked")
return c.SendStreamWriter(func(w *bufio.Writer) {
for {
fmt.Fprintf(w, "event: my-event\n")
fmt.Fprintf(w, "data: Hello SSE\n\n")
if err := w.Flush(); err != nil {
log.Print("Client disconnected!")
return
}
}
})
})
You can find more details about this feature in /docs/api/ctx.md.
Dropβ
In v3, we introduced support to silently terminate requests through Drop
.
func (c Ctx) Drop()
With this method, you can:
- Block certain requests without notifying the client to mitigate DDoS attacks
- Protect sensitive endpoints from unauthorized access without leaking errors.
While this feature adds the ability to drop connections, it is still highly recommended to use additional measures (such as firewalls, proxies, etc.) to further protect your server endpoints by blocking malicious connections before the server establishes a connection.
app.Get("/", func(c fiber.Ctx) error {
if c.IP() == "192.168.1.1" {
return c.Drop()
}
return c.SendString("Hello World!")
})
You can find more details about this feature in /docs/api/ctx.md.
Endβ
In v3, we introduced a new method to match the Express.js API's res.end()
method.
func (c Ctx) End()
With this method, you can:
- Stop middleware from controlling the connection after a handler further up the method chain by immediately flushing the current response and closing the connection.
- Use
return c.End()
as an alternative toreturn nil
app.Use(func (c fiber.Ctx) error {
err := c.Next()
if err != nil {
log.Println("Got error: %v", err)
return c.SendString(err.Error()) // Will be unsuccessful since the response ended below
}
return nil
})
app.Get("/hello", func (c fiber.Ctx) error {
query := c.Query("name", "")
if query == "" {
c.SendString("You don't have a name?")
c.End() // Closes the underlying connection
return errors.New("No name provided")
}
return c.SendString("Hello, " + query + "!")
})
π Client packageβ
The Gofiber client has been completely rebuilt. It includes numerous new features such as Cookiejar, request/response hooks, and more. You can take a look to client docs to see what's new with the client.
π Bindingβ
Fiber v3 introduces a new binding mechanism that simplifies the process of binding request data to structs. The new binding system supports binding from various sources such as URL parameters, query parameters, headers, and request bodies. This unified approach makes it easier to handle different types of request data in a consistent manner.
New Featuresβ
- Unified binding from URL parameters, query parameters, headers, and request bodies.
- Support for custom binders and constraints.
- Improved error handling and validation.
Example
type User struct {
ID int `params:"id"`
Name string `json:"name"`
Email string `json:"email"`
}
app.Post("/user/:id", func(c fiber.Ctx) error {
var user User
if err := c.Bind().Body(&user); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(user)
})
In this example, the Bind
method is used to bind the request body to the User
struct. The Body
method of the Bind
class performs the actual binding.
π Redirectβ
Fiber v3 enhances the redirect functionality by introducing new methods and improving existing ones. The new redirect methods provide more flexibility and control over the redirection process.
New Methodsβ
Redirect().To()
: Redirects to a specific URL.Redirect().Route()
: Redirects to a named route.Redirect().Back()
: Redirects to the previous URL.
Example
app.Get("/old", func(c fiber.Ctx) error {
return c.Redirect().To("/new")
})
app.Get("/new", func(c fiber.Ctx) error {
return c.SendString("Welcome to the new route!")
})
π§° Generic functionsβ
Fiber v3 introduces new generic functions that provide additional utility and flexibility for developers. These functions are designed to simplify common tasks and improve code readability.
New Generic Functionsβ
- Convert: Converts a value with a specified converter function and default value.
- Locals: Retrieves or sets local values within a request context.
- Params: Retrieves route parameters and can handle various types of route parameters.
- Query: Retrieves the value of a query parameter from the request URI and can handle various types of query parameters.
- GetReqHeader: Returns the HTTP request header specified by the field and can handle various types of header values.
Exampleβ
Convert
package main
import (
"strconv"
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Get("/convert", func(c fiber.Ctx) error {
value, err := fiber.Convert[string](c.Query("value"), strconv.Atoi, 0)
if err != nil {
return c.Status(fiber.StatusBadRequest).SendString(err.Error())
}
return c.JSON(value)
})
app.Listen(":3000")
}
curl "http://localhost:3000/convert?value=123"
# Output: 123
curl "http://localhost:3000/convert?value=abc"
# Output: "failed to convert: strconv.Atoi: parsing \"abc\": invalid syntax"
Locals
package main
import (
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Use("/user/:id", func(c fiber.Ctx) error {
// ask database for user
// ...
// set local values from database
fiber.Locals[string](c, "user", "john")
fiber.Locals[int](c, "age", 25)
// ...
return c.Next()
})
app.Get("/user/*", func(c fiber.Ctx) error {
// get local values
name := fiber.Locals[string](c, "user")
age := fiber.Locals[int](c, "age")
// ...
return c.JSON(fiber.Map{"name": name, "age": age})
})
app.Listen(":3000")
}
curl "http://localhost:3000/user/5"
# Output: {"name":"john","age":25}
Params
package main
import (
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Get("/params/:id", func(c fiber.Ctx) error {
id := fiber.Params[int](c, "id", 0)
return c.JSON(id)
})
app.Listen(":3000")
}
curl "http://localhost:3000/params/123"
# Output: 123
curl "http://localhost:3000/params/abc"
# Output: 0
Query
package main
import (
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Get("/query", func(c fiber.Ctx) error {
age := fiber.Query[int](c, "age", 0)
return c.JSON(age)
})
app.Listen(":3000")
}
curl "http://localhost:3000/query?age=25"
# Output: 25
curl "http://localhost:3000/query?age=abc"
# Output: 0
GetReqHeader
package main
import (
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
app.Get("/header", func(c fiber.Ctx) error {
userAgent := fiber.GetReqHeader[string](c, "User-Agent", "Unknown")
return c.JSON(userAgent)
})
app.Listen(":3000")
}
curl -H "User-Agent: CustomAgent" "http://localhost:3000/header"
# Output: "CustomAgent"
curl "http://localhost:3000/header"
# Output: "Unknown"
π Logβ
fiber.AllLogger
interface now has a new method called Logger
. This method can be used to get the underlying logger instance from the Fiber logger middleware. This is useful when you want to configure the logger middleware with a custom logger and still want to access the underlying logger instance.
You can find more details about this feature in /docs/api/log.md.
𧬠Middlewaresβ
Adaptorβ
The adaptor middleware has been significantly optimized for performance and efficiency. Key improvements include reduced response times, lower memory usage, and fewer memory allocations. These changes make the middleware more reliable and capable of handling higher loads effectively. Enhancements include the introduction of a sync.Pool
for managing fasthttp.RequestCtx
instances and better HTTP request and response handling between net/http and fasthttp contexts.
Payload Size | Metric | V2 | V3 | Percent Change |
---|---|---|---|---|
100KB | Execution Time | 1056 ns/op | 588.6 ns/op | -44.25% |
Memory Usage | 2644 B/op | 254 B/op | -90.39% | |
Allocations | 16 allocs/op | 5 allocs/op | -68.75% | |
500KB | Execution Time | 1061 ns/op | 562.9 ns/op | -46.94% |
Memory Usage | 2644 B/op | 248 B/op | -90.62% | |
Allocations | 16 allocs/op | 5 allocs/op | -68.75% | |
1MB | Execution Time | 1080 ns/op | 629.7 ns/op | -41.68% |
Memory Usage | 2646 B/op | 267 B/op | -89.91% | |
Allocations | 16 allocs/op | 5 allocs/op | -68.75% | |
5MB | Execution Time | 1093 ns/op | 540.3 ns/op | -50.58% |
Memory Usage | 2654 B/op | 254 B/op | -90.43% | |
Allocations | 16 allocs/op | 5 allocs/op | -68.75% | |
10MB | Execution Time | 1044 ns/op | 533.1 ns/op | -48.94% |
Memory Usage | 2665 B/op | 258 B/op | -90.32% | |
Allocations | 16 allocs/op | 5 allocs/op | -68.75% | |
25MB | Execution Time | 1069 ns/op | 540.7 ns/op | -49.42% |
Memory Usage | 2706 B/op | 289 B/op | -89.32% | |
Allocations | 16 allocs/op | 5 allocs/op | -68.75% | |
50MB | Execution Time | 1137 ns/op | 554.6 ns/op | -51.21% |
Memory Usage | 2734 B/op | 298 B/op | -89.10% | |
Allocations | 16 allocs/op | 5 allocs/op | -68.75% |
Cacheβ
We are excited to introduce a new option in our caching middleware: Cache Invalidator. This feature provides greater control over cache management, allowing you to define a custom conditions for invalidating cache entries.
Additionally, the caching middleware has been optimized to avoid caching non-cacheable status codes, as defined by the HTTP standards. This improvement enhances cache accuracy and reduces unnecessary cache storage usage.
CORSβ
We've made some changes to the CORS middleware to improve its functionality and flexibility. Here's what's new:
New Struct Fieldsβ
Config.AllowPrivateNetwork
: This new field is a boolean that allows you to control whether private networks are allowed. This is related to the Private Network Access (PNA) specification from the Web Incubator Community Group (WICG). When set totrue
, the CORS middleware will allow CORS preflight requests from private networks and respond with theAccess-Control-Allow-Private-Network: true
header. This could be useful in development environments or specific use cases, but should be done with caution due to potential security risks.
Updated Struct Fieldsβ
We've updated several fields from a single string (containing comma-separated values) to slices, allowing for more explicit declaration of multiple values. Here are the updated fields:
Config.AllowOrigins
: Now accepts a slice of strings, each representing an allowed origin.Config.AllowMethods
: Now accepts a slice of strings, each representing an allowed method.Config.AllowHeaders
: Now accepts a slice of strings, each representing an allowed header.Config.ExposeHeaders
: Now accepts a slice of strings, each representing an exposed header.
Compressionβ
We've added support for zstd
compression on top of gzip
, deflate
, and brotli
.
EncryptCookieβ
Added support for specifying Key length when using encryptcookie.GenerateKey(length)
. This allows the user to generate keys compatible with AES-128
, AES-192
, and AES-256
(Default).
Sessionβ
The Session middleware has undergone key changes in v3 to improve functionality and flexibility. While v2 methods remain available for backward compatibility, we now recommend using the new middleware handler for session management.
Key Updatesβ
-
New Middleware Handler: The
New
function now returns a middleware handler instead of a*Store
. To access the session store, use theStore
method on the middleware, or opt forNewStore
orNewWithStore
for custom store integration. -
Manual Session Release: Session instances are no longer automatically released after being saved. To ensure proper lifecycle management, you must manually call
sess.Release()
. -
Idle Timeout: The
Expiration
field has been replaced withIdleTimeout
, which handles session inactivity. If the session is idle for the specified duration, it will expire. The idle timeout is updated when the session is saved. If you are using the middleware handler, the idle timeout will be updated automatically. -
Absolute Timeout: The
AbsoluteTimeout
field has been added. If you need to set an absolute session timeout, you can use this field to define the duration. The session will expire after the specified duration, regardless of activity.
For more details on these changes and migration instructions, check the Session Middleware Migration Guide.
Loggerβ
New helper function called LoggerToWriter
has been added to the logger middleware. This function allows you to use 3rd party loggers such as logrus
or zap
with the Fiber logger middleware without any extra afford. For example, you can use zap
with Fiber logger middleware like this:
Example
package main
import (
"github.com/gofiber/contrib/fiberzap/v2"
"github.com/gofiber/fiber/v3"
"github.com/gofiber/fiber/v3/log"
"github.com/gofiber/fiber/v3/middleware/logger"
)
func main() {
// Create a new Fiber instance
app := fiber.New()
// Create a new zap logger which is compatible with Fiber AllLogger interface
zap := fiberzap.NewLogger(fiberzap.LoggerConfig{
ExtraKeys: []string{"request_id"},
})
// Use the logger middleware with zerolog logger
app.Use(logger.New(logger.Config{
Output: logger.LoggerToWriter(zap, log.LevelDebug),
}))
// Define a route
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
// Start server on http://localhost:3000
app.Listen(":3000")
}
Filesystemβ
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 or migration guide to see what has been changed.
Monitorβ
Monitor middleware is migrated to the Contrib package with PR #1172.
Healthcheckβ
The Healthcheck middleware has been enhanced to support more than two routes, with default endpoints for liveliness, readiness, and startup checks. Here's a detailed breakdown of the changes and how to use the new features.
-
Support for More Than Two Routes:
- The updated middleware now supports multiple routes beyond the default liveliness and readiness endpoints. This allows for more granular health checks, such as startup probes.
-
Default Endpoints:
- Three default endpoints are now available:
- Liveness:
/livez
- Readiness:
/readyz
- Startup:
/startupz
- Liveness:
- These endpoints can be customized or replaced with user-defined routes.
- Three default endpoints are now available:
-
Simplified Configuration:
- The configuration for each health check endpoint has been simplified. Each endpoint can be configured separately, allowing for more flexibility and readability.
Refer to the healthcheck middleware migration guide or the general migration guide to review the changes.
π Migration guideβ
π Appβ
Staticβ
Since we've removed app.Static()
, you need to move methods to static middleware like the example below:
// Before
app.Static("/", "./public")
app.Static("/prefix", "./public")
app.Static("/prefix", "./public", Static{
Index: "index.htm",
})
app.Static("*", "./public/index.html")
// 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"))
You have to put *
to the end of the route if you don't define static route with app.Use
.
Trusted Proxiesβ
We've renamed EnableTrustedProxyCheck
to TrustProxy
and moved TrustedProxies
to TrustProxyConfig
.
// Before
app := fiber.New(fiber.Config{
// EnableTrustedProxyCheck enables the trusted proxy check.
EnableTrustedProxyCheck: true,
// TrustedProxies is a list of trusted proxy IP ranges/addresses.
TrustedProxies: []string{"0.8.0.0", "127.0.0.0/8", "::1/128"},
})
// After
app := fiber.New(fiber.Config{
// TrustProxy enables the trusted proxy check
TrustProxy: true,
// TrustProxyConfig allows for configuring trusted proxies.
TrustProxyConfig: fiber.TrustProxyConfig{
// Proxies is a list of trusted proxy IP ranges/addresses.
Proxies: []string{"0.8.0.0"},
// Trust all loop-back IP addresses (127.0.0.0/8, ::1/128)
Loopback: true,
}
})
πΊ Routerβ
The signatures for Add
and Route
have been changed.
To migrate Add
you must change the methods
in a slice.
// Before
app.Add(fiber.MethodPost, "/api", myHandler)
// After
app.Add([]string{fiber.MethodPost}, "/api", myHandler)
To migrate Route
you need to read this.
// Before
app.Route("/api", func(apiGrp Router) {
apiGrp.Route("/user/:id?", func(userGrp Router) {
userGrp.Get("/", func(c fiber.Ctx) error {
// Get user
return c.JSON(fiber.Map{"message": "Get user", "id": c.Params("id")})
})
userGrp.Post("/", func(c fiber.Ctx) error {
// Create user
return c.JSON(fiber.Map{"message": "User created"})
})
})
})
// After
app.Route("/api").Route("/user/:id?")
.Get(func(c fiber.Ctx) error {
// Get user
return c.JSON(fiber.Map{"message": "Get user", "id": c.Params("id")})
})
.Post(func(c fiber.Ctx) error {
// Create user
return c.JSON(fiber.Map{"message": "User created"})
});
πΊ RebuildTreeβ
We introduced a new method that enables rebuilding the route tree stack at runtime. This allows you to add routes dynamically while your application is running and update the route tree to make the new routes available for use.
For more details, refer to the app documentation:
Example Usageβ
app.Get("/define", func(c Ctx) error { // Define a new route dynamically
app.Get("/dynamically-defined", func(c Ctx) error { // Adding a dynamically defined route
return c.SendStatus(http.StatusOK)
})
app.RebuildTree() // Rebuild the route tree to register the new route
return c.SendStatus(http.StatusOK)
})
In this example, a new route is defined, and RebuildTree()
is called to ensure the new route is registered and available.
Note: Use this method with caution. It is not thread-safe and can be very performance-intensive. Therefore, it should be used sparingly and primarily in development mode. It should not be invoke concurrently.
π§ Contextβ
Fiber v3 introduces several new features and changes to the Ctx interface, enhancing its functionality and flexibility.
- ParamsInt: Use
Params
with generic types. - QueryBool: Use
Query
with generic types. - QueryFloat: Use
Query
with generic types. - QueryInt: Use
Query
with generic types. - Bind: Now used for binding instead of view binding. Use
c.ViewBind()
for view binding.
In Fiber v3, the Ctx
parameter in handlers is now an interface, which means the *
symbol is no longer used. Here is an example demonstrating this change:
Example
Before:
package main
import (
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
// Route Handler with *fiber.Ctx
app.Get("/", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":3000")
}
After:
package main
import (
"github.com/gofiber/fiber/v3"
)
func main() {
app := fiber.New()
// Route Handler without *fiber.Ctx
app.Get("/", func(c fiber.Ctx) error {
return c.SendString("Hello, World!")
})
app.Listen(":3000")
}
Explanation:
In this example, the Ctx
parameter in the handler is used as an interface (fiber.Ctx
) instead of a pointer (*fiber.Ctx
). This change allows for more flexibility and customization in Fiber v3.
π Parserβ
The Parser
section in Fiber v3 has undergone significant changes to improve functionality and flexibility.
Migration Instructionsβ
-
BodyParser: Use
c.Bind().Body()
instead ofc.BodyParser()
.Example
// Before
app.Post("/user", func(c *fiber.Ctx) error {
var user User
if err := c.BodyParser(&user); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(user)
})// After
app.Post("/user", func(c fiber.Ctx) error {
var user User
if err := c.Bind().Body(&user); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(user)
}) -
ParamsParser: Use
c.Bind().URL()
instead ofc.ParamsParser()
.Example
// Before
app.Get("/user/:id", func(c *fiber.Ctx) error {
var params Params
if err := c.ParamsParser(¶ms); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(params)
})// After
app.Get("/user/:id", func(c fiber.Ctx) error {
var params Params
if err := c.Bind().URL(¶ms); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(params)
}) -
QueryParser: Use
c.Bind().Query()
instead ofc.QueryParser()
.Example
// Before
app.Get("/search", func(c *fiber.Ctx) error {
var query Query
if err := c.QueryParser(&query); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(query)
})// After
app.Get("/search", func(c fiber.Ctx) error {
var query Query
if err := c.Bind().Query(&query); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(query)
}) -
CookieParser: Use
c.Bind().Cookie()
instead ofc.CookieParser()
.Example
// Before
app.Get("/cookie", func(c *fiber.Ctx) error {
var cookie Cookie
if err := c.CookieParser(&cookie); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(cookie)
})// After
app.Get("/cookie", func(c fiber.Ctx) error {
var cookie Cookie
if err := c.Bind().Cookie(&cookie); err != nil {
return c.Status(fiber.StatusBadRequest).JSON(fiber.Map{"error": err.Error()})
}
return c.JSON(cookie)
})
π Redirectβ
Fiber v3 enhances the redirect functionality by introducing new methods and improving existing ones. The new redirect methods provide more flexibility and control over the redirection process.
Migration Instructionsβ
-
RedirectToRoute: Use
c.Redirect().Route()
instead ofc.RedirectToRoute()
.Example
// Before
app.Get("/old", func(c *fiber.Ctx) error {
return c.RedirectToRoute("newRoute")
})// After
app.Get("/old", func(c fiber.Ctx) error {
return c.Redirect().Route("newRoute")
}) -
RedirectBack: Use
c.Redirect().Back()
instead ofc.RedirectBack()
.Example
// Before
app.Get("/back", func(c *fiber.Ctx) error {
return c.RedirectBack()
})// After
app.Get("/back", func(c fiber.Ctx) error {
return c.Redirect().Back()
}) -
Redirect: Use
c.Redirect().To()
instead ofc.Redirect()
.Example
// Before
app.Get("/old", func(c *fiber.Ctx) error {
return c.Redirect("/new")
})// After
app.Get("/old", func(c fiber.Ctx) error {
return c.Redirect().To("/new")
})
π Client packageβ
Fiber v3 introduces a completely rebuilt client package with numerous new features such as Cookiejar, request/response hooks, and more. Here is a guide to help you migrate from Fiber v2 to Fiber v3.
New Featuresβ
- Cookiejar: Manage cookies automatically.
- Request/Response Hooks: Customize request and response handling.
- Improved Error Handling: Better error management and reporting.
Migration Instructionsβ
Import Path:
Update the import path to the new client package.
Before
import "github.com/gofiber/fiber/v2/client"
After
import "github.com/gofiber/fiber/v3/client"
DRAFT section
𧬠Middlewaresβ
CORSβ
The CORS middleware has been updated to use slices instead of strings for the AllowOrigins
, AllowMethods
, AllowHeaders
, and ExposeHeaders
fields. Here's how you can update your code:
// Before
app.Use(cors.New(cors.Config{
AllowOrigins: "https://example.com,https://example2.com",
AllowMethods: strings.Join([]string{fiber.MethodGet, fiber.MethodPost}, ","),
AllowHeaders: "Content-Type",
ExposeHeaders: "Content-Length",
}))
// After
app.Use(cors.New(cors.Config{
AllowOrigins: []string{"https://example.com", "https://example2.com"},
AllowMethods: []string{fiber.MethodGet, fiber.MethodPost},
AllowHeaders: []string{"Content-Type"},
ExposeHeaders: []string{"Content-Length"},
}))
CSRFβ
- Field Renaming: The
Expiration
field in the CSRF middleware configuration has been renamed toIdleTimeout
to better describe its functionality. Additionally, the default value has been reduced from 1 hour to 30 minutes. Update your code as follows:
// Before
app.Use(csrf.New(csrf.Config{
Expiration: 10 * time.Minute,
}))
// After
app.Use(csrf.New(csrf.Config{
IdleTimeout: 10 * time.Minute,
}))
- Session Key Removal: The
SessionKey
field has been removed from the CSRF middleware configuration. The session key is now an unexported constant within the middleware to avoid potential key collisions in the session store.
Filesystemβ
You need to move filesystem middleware to static middleware due to it has been removed from the core.
// 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,
}))
// 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,
}))
Healthcheckβ
Previously, the Healthcheck middleware was configured with a combined setup for liveliness and readiness probes:
//before
app.Use(healthcheck.New(healthcheck.Config{
LivenessProbe: func(c fiber.Ctx) bool {
return true
},
LivenessEndpoint: "/live",
ReadinessProbe: func(c fiber.Ctx) bool {
return serviceA.Ready() && serviceB.Ready() && ...
},
ReadinessEndpoint: "/ready",
}))
With the new version, each health check endpoint is configured separately, allowing for more flexibility:
// after
// Default liveness endpoint configuration
app.Get(healthcheck.DefaultLivenessEndpoint, healthcheck.NewHealthChecker(healthcheck.Config{
Probe: func(c fiber.Ctx) bool {
return true
},
}))
// Default readiness endpoint configuration
app.Get(healthcheck.DefaultReadinessEndpoint, healthcheck.NewHealthChecker())
// New default startup endpoint configuration
// Default endpoint is /startupz
app.Get(healthcheck.DefaultStartupEndpoint, healthcheck.NewHealthChecker(healthcheck.Config{
Probe: func(c fiber.Ctx) bool {
return serviceA.Ready() && serviceB.Ready() && ...
},
}))
// Custom liveness endpoint configuration
app.Get("/live", healthcheck.NewHealthChecker())
Monitorβ
Since v3 the Monitor middleware has been moved to the Contrib package
// Before
import "github.com/gofiber/fiber/v2/middleware/monitor"
app.Use("/metrics", monitor.New())
You only need to change the import path to the contrib package.
// After
import "github.com/gofiber/contrib/monitor"
app.Use("/metrics", monitor.New())