2023-12-21 16:04:04 +00:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.myservermanager.com/varakh/upda/api"
|
2024-06-11 21:47:30 +00:00
|
|
|
"git.myservermanager.com/varakh/upda/commons"
|
2023-12-21 16:04:04 +00:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"net/http"
|
2024-06-10 18:03:25 +00:00
|
|
|
"strings"
|
2023-12-21 16:04:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func middlewareAppName() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2024-06-11 21:47:30 +00:00
|
|
|
c.Header(api.HeaderAppName, name)
|
2024-06-10 18:03:25 +00:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func middlewareGlobalNotFound() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusNotFound, api.NewErrorResponseWithStatusAndMessage(string(notFound), "page not found"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func middlewareGlobalMethodNotAllowed() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
c.AbortWithStatusJSON(http.StatusMethodNotAllowed, api.NewErrorResponseWithStatusAndMessage(string(methodNotAllowed), "method not allowed"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func middlewareEnforceJsonContentType() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2024-06-11 21:47:30 +00:00
|
|
|
if c.Request.Method != http.MethodOptions && !strings.HasPrefix(c.GetHeader(api.HeaderContentType), api.HeaderContentTypeApplicationJson) {
|
2024-06-10 18:03:25 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusBadRequest, api.NewErrorResponseWithStatusAndMessage(string(illegalArgument), "content-type must be application/json"))
|
|
|
|
return
|
|
|
|
}
|
2023-12-21 16:04:04 +00:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func middlewareAppVersion() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
2024-06-11 21:47:30 +00:00
|
|
|
c.Header(api.HeaderAppVersion, commons.Version)
|
2023-12-21 16:04:04 +00:00
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// middlewareErrorHandler handles global error handling, does not overwrite any given status (see -1)
|
|
|
|
func middlewareErrorHandler() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
// call next first, so this is the last in chain
|
|
|
|
c.Next()
|
|
|
|
|
|
|
|
if len(c.Errors) > 0 {
|
|
|
|
// status -1 doesn't overwrite existing status code
|
2024-06-11 21:47:30 +00:00
|
|
|
c.Header(api.HeaderContentType, api.HeaderContentTypeApplicationJson)
|
2023-12-21 16:04:04 +00:00
|
|
|
c.JSON(-1, api.NewErrorResponseWithStatusAndMessage(errCodeToStr(c.Errors.Last()), c.Errors.Last().Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func middlewareAppErrorRecoveryHandler() gin.HandlerFunc {
|
|
|
|
return func(c *gin.Context) {
|
|
|
|
defer func() {
|
|
|
|
if err := recover(); err != nil {
|
2024-06-10 18:03:25 +00:00
|
|
|
c.AbortWithStatusJSON(http.StatusInternalServerError, api.NewErrorResponseWithStatusAndMessage(string(general), fmt.Sprintf("%s", err)))
|
2023-12-21 16:04:04 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
c.Next()
|
|
|
|
}
|
|
|
|
}
|