-
棒棒的,受益匪浅查看全部
-
Gin脚手架文件结构说明:
conf/ #配置文件目录-下一层#环境文件夹
/dev #开发环境
base.toml* #基本配置存储
mysql_map.toml* #mysql配置存储
redis_map.toml* #redis配置存储
controller/ #控制器类-按照文件名区分不同的请求,同一请求汇聚到同一文件
dao/ #数据访问的对象层-一般存储的是RM
dto/ #数据传输对象层-输入的参数转化为实际的对象
middleware/ #中间件层-日志的打印和请求的拦截,以及session和多语言的处理也可以放在这里
public/ #公共层-放置一些公共的函数
router/ #路由层-路由的配置请求的配置
tmpl/ #模版文件存储位置
查看全部 -
笔记查看全部
-
package main import ( "github.com/gin-gonic/gin" "gopkg.in/go-playground/validator.v9" "net/http" "time" ) type Booking struct { CheckIn time.Time `form:"check_in" validate:"required,bookableDate" time_format:"2006-01-02"` CheckOut time.Time `form:"check_out" validate:"required,gtfield=CheckIn" time_format:"2006-01-02"` } func main(){ r := gin.Default() validate := validator.New() validate.RegisterValidation("bookableDate", bookableDate) r.GET("/bookable", func(c *gin.Context) { var book Booking if err := c.ShouldBind(&book); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), }) c.Abort() return } if err := validate.Struct(book); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), }) c.Abort() return } c.JSON(http.StatusOK, gin.H{ "message": "OK", "booking": book, }) }) r.Run() } func bookableDate(fl validator.FieldLevel) bool { if date, ok := fl.Field().Interface().(time.Time); ok { today := time.Now() if date.Unix() > today.Unix() { return true } } return false }
查看全部 -
package main import ( "github.com/gin-gonic/gin" "github.com/go-playground/locales/en" "github.com/go-playground/locales/zh" "github.com/go-playground/universal-translator" "gopkg.in/go-playground/validator.v9" en2 "gopkg.in/go-playground/validator.v9/translations/en" zh2 "gopkg.in/go-playground/validator.v9/translations/zh" "net/http" "time" ) type Booking struct { CheckIn time.Time `form:"check_in" validate:"required,bookableDate" time_format:"2006-01-02"` CheckOut time.Time `form:"check_out" validate:"required,gtfield=CheckIn" time_format:"2006-01-02"` } func main(){ r := gin.Default() zhTranslate := zh.New() enTranslate := en.New() Uni := ut.New(zhTranslate, enTranslate) validate := validator.New() _ = validate.RegisterValidation("bookableDate", bookableDate) r.GET("/bookable", func(c *gin.Context) { locale := c.DefaultQuery("locale", "zh") trans, _ := Uni.GetTranslator(locale) switch locale { case "zh": _ = validate.RegisterTranslation("bookableDate", trans, registerBookableDateTranslation, bookableDateTranslation) _ = zh2.RegisterDefaultTranslations(validate, trans) break case "en": _ = en2.RegisterDefaultTranslations(validate, trans) break default: _ = validate.RegisterTranslation("bookableDate", trans, registerBookableDateTranslation, bookableDateTranslation) _ = zh2.RegisterDefaultTranslations(validate, trans) } var book Booking if err := c.ShouldBind(&book); err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), }) c.Abort() return } if err := validate.Struct(book); err != nil { errs := err.(validator.ValidationErrors) var sliceErrs []string for _, v := range errs { sliceErrs = append(sliceErrs, v.Translate(trans)) } c.JSON(http.StatusInternalServerError, gin.H{ "error": sliceErrs, }) c.Abort() return } c.JSON(http.StatusOK, gin.H{ "message": "OK", "booking": book, }) }) _ = r.Run() } func bookableDate(fl validator.FieldLevel) bool { if date, ok := fl.Field().Interface().(time.Time); ok { today := time.Now() if date.Unix() > today.Unix() { return true } } return false } func registerBookableDateTranslation(ut ut.Translator) error { return ut.Add("bookableDate", "{0} 日期必须大于当天日期!", true) } func bookableDateTranslation(ut ut.Translator, fe validator.FieldError) string { t, _ := ut.T("bookableDate", fe.Field()) return t }
查看全部 -
windows 也是这么输出mkdir -p $GOPATH/src/github.com/e421083458/gin_test_project吗?
查看全部 -
很棒!查看全部
-
完全的中间件
session
recovery
sessionauth
translation
查看全部 -
授权验证代码
查看全部 -
session的使用
查看全部 -
前端框架vue...
查看全部 -
值得拥有哦
查看全部 -
自动化证书
查看全部 -
脚手架演示
查看全部 -
ssl证书自动配置
查看全部
举报