mirror of
https://github.com/0xJacky/nginx-ui.git
synced 2025-05-11 02:15:48 +02:00
refactor: project directory structure
This commit is contained in:
parent
c1193a5b8c
commit
e5a5889931
367 changed files with 710 additions and 756 deletions
58
api/api.go
Normal file
58
api/api.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
package api
|
||||
|
||||
import (
|
||||
"github.com/0xJacky/Nginx-UI/internal/logger"
|
||||
"github.com/gin-gonic/gin"
|
||||
val "github.com/go-playground/validator/v10"
|
||||
"net/http"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
func ErrHandler(c *gin.Context, err error) {
|
||||
logger.GetLogger().Errorln(err)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"message": err.Error(),
|
||||
})
|
||||
}
|
||||
|
||||
type ValidError struct {
|
||||
Key string
|
||||
Message string
|
||||
}
|
||||
|
||||
func BindAndValid(c *gin.Context, target interface{}) bool {
|
||||
errs := make(map[string]string)
|
||||
err := c.ShouldBindJSON(target)
|
||||
if err != nil {
|
||||
logger.Error("bind err", err)
|
||||
|
||||
verrs, ok := err.(val.ValidationErrors)
|
||||
|
||||
if !ok {
|
||||
logger.Error("valid err", verrs)
|
||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||
"message": "Requested with wrong parameters",
|
||||
"code": http.StatusNotAcceptable,
|
||||
"error": verrs,
|
||||
})
|
||||
return false
|
||||
}
|
||||
|
||||
for _, value := range verrs {
|
||||
t := reflect.ValueOf(target)
|
||||
realType := t.Type().Elem()
|
||||
field, _ := realType.FieldByName(value.StructField())
|
||||
errs[field.Tag.Get("json")] = value.Tag()
|
||||
}
|
||||
|
||||
c.JSON(http.StatusNotAcceptable, gin.H{
|
||||
"errors": errs,
|
||||
"message": "Requested with wrong parameters",
|
||||
"code": http.StatusNotAcceptable,
|
||||
})
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue