🧭 Controllers
In Velora, Controllers define HTTP routes and handle requests. They connect to services to execute business logic and return responses.
🛠️ Creating a Controller
A controller is usually a struct that depends on a service. It must implement the RegisterRoutes(app *fiber.App) method:
package user
import "github.com/gofiber/fiber/v2"
type Controller struct {
service *Service
}
func NewController(s *Service) *Controller {
return &Controller{service: s}
}
func (c *Controller) RegisterRoutes(app *fiber.App) {
app.Get("/users", c.findAll)
}
func (c *Controller) findAll(ctx *fiber.Ctx) error {
users := c.service.FindAll()
return ctx.JSON(users)
}🔌 Dependency Injection
The controller is provided in the module along with the service:
func (m *UserModule) Register(container *dig.Container, app *fiber.App) error {
container.Provide(NewService)
container.Provide(NewController)
// Invoke RegisterRoutes with dependencies
return container.Invoke(func(c *Controller) {
c.RegisterRoutes(app)
})
}🚀 Best Practices
- Keep controllers focused only on request/response handling.
- Delegate all logic to services instead of writing it in controllers.
- Use DTOs for input validation and consistent response formats.