🎨 Decorators

In Velora, Decorators are lightweight wrappers for route handlers. They allow you to compose extra behavior around a handler in a reusable way, similar to function composition.

🛠️ Core Implementation

Velora provides a simple decorator system:

package app

import "github.com/gofiber/fiber/v2"

type Decorator func(fiber.Handler) fiber.Handler

func Use(decorators ...Decorator) func(fiber.Handler) fiber.Handler {
    return func(h fiber.Handler) fiber.Handler {
        for i := len(decorators) - 1; i >= 0; i-- {
            h = decorators[i](h)
        }
        return h
    }
}

A Decorator is a function that takes a fiber.Handlerand returns a new handler with extra logic applied.

🔍 Example: Logging Decorator

This decorator logs before and after the request is handled:

func LoggingDecorator() app.Decorator {
    return func(next fiber.Handler) fiber.Handler {
        return func(c *fiber.Ctx) error {
            fmt.Println("➡️ Before:", c.Path())
            err := next(c)
            fmt.Println("⬅️ After:", c.Path())
            return err
        }
    }
}

⚡ Using Decorators in Routes

You can wrap handlers when registering routes. For example, in a real Velora app:

a.HTTP().Get("/hello", app.Use(LoggingDecorator())(
    func(c *fiber.Ctx) error {
        return c.SendString("Hello, world!")
    },
))

🧩 Real Project Example

In a full Velora project, you might have global middlewares and module-specific routes:

// Register global middlewares
velora.RegisterMiddleware("auth", AuthMiddleware)
velora.RegisterMiddleware("logging", LoggingMiddleware)
velora.RegisterMiddleware("cors", CORSMiddleware)
a.UseGlobalMiddleware("logging", "cors", "auth")

// Module route with decorator
a.HTTP().Get("/secure", app.Use(LoggingDecorator(), AuthDecorator())(
    func(c *fiber.Ctx) error {
        return c.SendString("Secure route")
    },
))

🧩 Composing Multiple Decorators

You can chain decorators together. They are executed in reverse order, similar to middleware stacks.

🚀 Best Practices

  • Use decorators for per-route logic instead of global logic.
  • Examples: logging, auth guards, caching, error handling wrappers.
  • Decorators are applied only where you explicitly use them (unlike global middleware).
  • Keep decorators small, composable, and reusable.

Next: combine middlewares with decorators to build powerful request flows.