2020-12-29 13:46:58 +01:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
|
|
)
|
|
|
|
|
2021-01-03 18:21:42 +01:00
|
|
|
// 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())
|
2021-01-03 18:21:42 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|