Skip to main content

Route Constraints: Validation Before Your Handler Runs

ยท 5 min read
Fiber Team
Maintainers

Almost every REST API has a handler that starts like this:

app.Get("/users/:id", func(c fiber.Ctx) error {
id, err := strconv.Atoi(c.Params("id"))
if err != nil {
return fiber.ErrBadRequest
}
// ... finally, the actual logic
})

Three lines of ceremony before the handler does anything useful, repeated in every handler that takes a numeric parameter. Fiber has a feature that moves this check into the router itself: route constraints. Declare the parameter as :id<int> and the route simply does not match for /users/abc. Your handler only ever sees values that passed the check.

Constraints arrived back in v2.37 and were inspired by .NET Core routing. They remain one of the least-known routing features, which is a shame, because they remove boilerplate exactly where it accumulates fastest.

The Fiber CLI

ยท 4 min read
Fiber Team
Maintainers

Most Go web frameworks give you a library and send you on your way. Fiber ships a CLI tool that handles the repetitive tasks around development: live reload when files change, project scaffolding from templates, serving static files with one command, and migrating between Fiber versions automatically.

You do not need the CLI to use Fiber. But once you try fiber dev instead of manually restarting your server after every change, you will not go back.

The Recipes Cookbook

ยท 4 min read
Fiber Team
Maintainers

Documentation tells you what an API does. Blog posts explain the why. But when you need to build something real - authentication with JWT, deployment to AWS, a WebSocket chat, a CRUD app with Postgres - what you really want is working code you can copy and adapt.

That is what the Fiber Recipes repository is. Over 70 complete, runnable example projects covering everything from "Hello World" to clean architecture with Docker, OAuth2, and Kubernetes. Every recipe is a self-contained Go project with its own go.mod, main.go, and README.