38 lines
738 B
Go
38 lines
738 B
Go
|
package handlers
|
||
|
|
||
|
import "github.com/gofiber/fiber/v2"
|
||
|
|
||
|
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")
|
||
|
}
|