Commit 7d7ae412 by zhengqiuyun86

初始化

parent 197cd255
......@@ -9,6 +9,8 @@ require (
github.com/olivere/elastic/v7 v7.0.32
gorm.io/driver/mysql v1.2.2
gorm.io/gorm v1.22.4
github.com/swaggo/files v0.0.0-20220728132757-551d4a08d97a
github.com/swaggo/gin-swagger v1.5.2
)
require (
......
package router
import (
"git.168cad.top/zhengqiuyun/rym-util/conf"
"git.168cad.top/zhengqiuyun/rym-util/router/validator"
"github.com/gin-gonic/gin"
_ "github.com/swaggo/files"
swaggerFiles "github.com/swaggo/files"
"github.com/swaggo/gin-swagger"
"net/http"
)
func InitGinEngine() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
c := gin.New()
//便于工具监测
c.GET("/favicon.ico", func(c *gin.Context) {
c.JSON(http.StatusOK, nil)
})
//自定义验证器
validator.RegisterValidate()
if conf.Env == "dev" || conf.Env == "test" {
//swagger
c.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
}
return c
}
package validator
import (
"github.com/gin-gonic/gin/binding"
"github.com/go-playground/validator/v10"
)
func RegisterValidate() {
// 注册验证器
validate, ok := binding.Validator.Engine().(*validator.Validate)
if ok {
validate.RegisterValidation("NotNull", notNullValidator)
}
}
// 参数不能为空(包括不传参数及参数值为空字符串)
var notNullValidator validator.Func = func(fl validator.FieldLevel) bool {
if value, ok := fl.Field().Interface().(string); ok {
// 字段不能为空,并且不等于 admin
return value != ""
}
return true
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment