How to redirect Request in Go Fiber?

I was working on adding new domain from readclip.ahmadrosid.com to readclip.site today. The migration is finished but I need one more thing when user visit the old url I want to redirect them into the new url.

Redirecting url from server is just setting up the headers. Here's what look like in good old PHP.

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: https://new-url.com");
exit();
?>

The same thing happen for golang app. Since it follow the expressjs architecture we just need to add one more middleware put it above all your route definition. Better after you create fiber app instance.

app := fiber.New()
app.Use("/", func(ctx *fiber.Ctx) error {
    if ctx.BaseURL() == "https://readclip.ahmadrosid.com" {
        return ctx.Redirect(
            fmt.Sprintf("https://readclip.site%s", ctx.Path()),
            http.StatusMovedPermanently,
        )
    }
    return ctx.Next()
})

In local development I still need to use the localhost host so we skip when the base url is not the old url.