refactor: project directory structure

This commit is contained in:
0xJacky 2023-11-26 18:59:12 +08:00
parent c1193a5b8c
commit e5a5889931
No known key found for this signature in database
GPG key ID: B6E4A6E4A561BAF0
367 changed files with 710 additions and 756 deletions

58
api/api.go Normal file
View 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
}