Skip to content

📝 [v3 Proposal]: Enabling External Registration of Constraints for Router API #2783

@ReneWerner87

Description

@ReneWerner87

Feature Proposal Description

For Fiber v3, I propose an enhancement to the Router API that allows developers to externally register constraints.
This feature will provide a more flexible and dynamic routing mechanism, enabling the definition of custom path constraints directly in the route definition.

The idea stems from the discussion in the pull request #2782, particularly the comment #2782 (comment).
The core implementation for constraints is currently available in the v3-beta branch at https://github.com/gofiber/fiber/blob/v3-beta/path.go.
This feature is designed to enhance API flexibility without compromising the simplicity and performance Fiber is known for.

Alignment with Express API

The proposed feature aligns with the design philosophy of Express.js by enhancing API flexibility and developer control, similar to how Express allows middleware and routing flexibility. However, it introduces a more declarative approach to defining route constraints, which is not directly present in Express. This approach can streamline route management and improve code readability, maintaining a balance between Express's simplicity and a more expressive routing mechanism.

HTTP RFC Standards Compliance

The feature adheres to HTTP RFC standards, as it deals with URL path definitions and constraints, which are fundamental aspects of HTTP requests. By enabling custom constraints on the routes, it follows the standard practices of defining and handling URIs and parameters in web applications, ensuring full compatibility with existing web standards.

API Stability

The proposed feature is designed with stability in mind. The external registration of constraints follows a clear and straightforward API model, which minimizes the likelihood of future breaking changes. The design ensures that once a constraint is defined and registered, it can seamlessly integrate with the existing Fiber routing mechanism without necessitating future modifications, thereby promising long-term stability.

Feature Examples

Define a custom constraint function:

myCustomConstraint := func(param string) bool {
    // Logic for the constraint
    return true // or false based on the constraint logic
}

Here's an example of how this feature might be used in a Fiber application:

Example of registering a custom constraint:

app := fiber.New()
app.RegisterConstraint("myConstraint", myConstraintFunction)

or

// Initialize Fiber app with custom constraints in Config
app := fiber.New(fiber.Config{
    Constraints: map[string]fiber.Constraint{
        "myCustomConstraint": myCustomConstraint,
    },
})

Using the custom constraint in a route

app.Get("/user/:id{myConstraint}", handler)

In these examples, myConstraintFunction is a function defined by the developer that specifies the constraint logic. This feature allows for such custom constraints to be easily integrated into the routing system of a Fiber application.

Checklist:

  • I agree to follow Fiber's Code of Conduct.
  • I have searched for existing issues that describe my proposal before opening this one.
  • I understand that a proposal that does not meet these guidelines may be closed without explanation.

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions