auth-api/pkgs/handlers/middlewares.go

28 lines
758 B
Go
Raw Normal View History

2020-12-29 13:46:58 +01:00
package handlers
import (
"github.com/gofiber/fiber/v2"
)
// Log all requests
2021-06-22 22:52:48 +02:00
func (h Handlers) LogReq(c *fiber.Ctx) error {
h.Log.Debug("http request", "method", c.Method(), "url", c.OriginalURL())
c.Next()
return nil
}
2020-12-29 13:46:58 +01:00
// RequireJSON is a middleware that makes sure the request content-type always is application/json (or nothing, defaulting to application/json)
2021-01-02 11:56:24 +01:00
func (h Handlers) RequireJSON(c *fiber.Ctx) error {
2020-12-29 13:46:58 +01:00
c.Accepts("application/json")
contentType := string(c.Request().Header.ContentType())
if contentType != "application/json" && contentType != "" {
2021-06-22 22:52:48 +02:00
h.Log.Debug("Invalid content-type in request", "content-type", contentType)
2020-12-29 13:46:58 +01:00
return c.Status(415).JSON([]ResJSONError{{Error: "Invalid content-type"}})
}
c.Next()
return nil
}