当前仓库属于关闭状态,部分功能使用受限,详情请查阅 仓库状态说明
17 Star 35 Fork 0

vz / ego
关闭

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
server.go 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
vz 提交于 2018-07-02 20:25 . update examples and more code
package main
import (
"net/http"
"reflect"
"time"
"github.com/go-ego/ego"
"github.com/go-ego/ego/mid/binding"
validator "gopkg.in/go-playground/validator.v8"
)
type Booking struct {
CheckIn time.Time `form:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"`
CheckOut time.Time `form:"check_out" binding:"required,gtfield=CheckIn" time_format:"2006-01-02"`
}
func bookableDate(
v *validator.Validate, topStruct reflect.Value, currentStructOrField reflect.Value,
field reflect.Value, fieldType reflect.Type, fieldKind reflect.Kind, param string,
) bool {
if date, ok := field.Interface().(time.Time); ok {
today := time.Now()
if today.Year() > date.Year() || today.YearDay() > date.YearDay() {
return false
}
}
return true
}
func main() {
route := ego.Default()
// binding.Validator.RegisterValidation("bookabledate", bookableDate)
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("bookabledate", bookableDate)
}
route.GET("/bookable", getBookable)
route.Run(":8085")
}
func getBookable(c *ego.Context) {
var b Booking
if err := c.ShouldBindWith(&b, binding.Query); err == nil {
c.JSON(http.StatusOK, ego.Map{"message": "Booking dates are valid!"})
} else {
c.JSON(http.StatusBadRequest, ego.Map{"error": err.Error()})
}
}
Go
1
https://gitee.com/veni0/ego.git
git@gitee.com:veni0/ego.git
veni0
ego
ego
263eb77d34e2

搜索帮助