hugo-cms/handlers/mimeselectors.go
2025-02-03 21:35:58 +11:00

38 lines
729 B
Go

package handlers
import "github.com/gofiber/fiber/v3"
type mimeTypeHandler interface {
CanHandle(c fiber.Ctx) bool
Handle(c fiber.Ctx) error
}
type HTMX func(c fiber.Ctx) error
func (h HTMX) CanHandle(c fiber.Ctx) bool {
return c.Get("Hx-request") == "true"
}
func (h HTMX) Handle(c fiber.Ctx) error {
return h(c)
}
type Otherwise func(c fiber.Ctx) error
func (h Otherwise) CanHandle(c fiber.Ctx) bool {
return true
}
func (h Otherwise) Handle(c fiber.Ctx) error {
return h(c)
}
func Select(c fiber.Ctx, mimeTypes ...mimeTypeHandler) error {
for _, mt := range mimeTypes {
if mt.CanHandle(c) {
return mt.Handle(c)
}
}
return c.Status(fiber.StatusInternalServerError).SendString("cant handle response")
}