hugo-cms/handlers/mimeselectors.go

38 lines
729 B
Go
Raw Permalink Normal View History

2025-01-31 22:42:32 +00:00
package handlers
2025-02-03 10:35:58 +00:00
import "github.com/gofiber/fiber/v3"
2025-01-31 22:42:32 +00:00
type mimeTypeHandler interface {
2025-02-03 10:35:58 +00:00
CanHandle(c fiber.Ctx) bool
Handle(c fiber.Ctx) error
2025-01-31 22:42:32 +00:00
}
2025-02-03 10:35:58 +00:00
type HTMX func(c fiber.Ctx) error
2025-01-31 22:42:32 +00:00
2025-02-03 10:35:58 +00:00
func (h HTMX) CanHandle(c fiber.Ctx) bool {
2025-01-31 22:42:32 +00:00
return c.Get("Hx-request") == "true"
}
2025-02-03 10:35:58 +00:00
func (h HTMX) Handle(c fiber.Ctx) error {
2025-01-31 22:42:32 +00:00
return h(c)
}
2025-02-03 10:35:58 +00:00
type Otherwise func(c fiber.Ctx) error
2025-01-31 22:42:32 +00:00
2025-02-03 10:35:58 +00:00
func (h Otherwise) CanHandle(c fiber.Ctx) bool {
2025-01-31 22:42:32 +00:00
return true
}
2025-02-03 10:35:58 +00:00
func (h Otherwise) Handle(c fiber.Ctx) error {
2025-01-31 22:42:32 +00:00
return h(c)
}
2025-02-03 10:35:58 +00:00
func Select(c fiber.Ctx, mimeTypes ...mimeTypeHandler) error {
2025-01-31 22:42:32 +00:00
for _, mt := range mimeTypes {
if mt.CanHandle(c) {
return mt.Handle(c)
}
}
return c.Status(fiber.StatusInternalServerError).SendString("cant handle response")
}