介绍
参考网上的gin用法,做备份用来查询
示例
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"gopkg.in/go-playground/validator.v8"
"net/http"
"reflect"
"time"
)
func init() {
fmt.Println("start")
//gin日志输出 在实例化之前设置
//gin.DisableConsoleColor()
//f, _ := os.Create("gin.log")
//gin.DefaultWriter = io.MultiWriter(f)
}
type User struct {
Name string `json:"name" form:"name" binding:"required"`
Age uint8 `json:"age" form:"age" binding:"required,ageValidator"`
}
//校验
func AgeValidator(v *validator.Validate, topStruct reflect.Value,
currentStruct reflect.Value, field reflect.Value,
fieldtype reflect.Type, fieldKind reflect.Kind, param string) bool {
if age, ok := field.Interface().(uint8); ok {
if age > 150 {
return false
}
}
return true
}
func Logger() gin.HandlerFunc {
return func(context *gin.Context) {
t := time.Now()
context.Next()
total := time.Since(t)
fmt.Println("time:", total)
}
}
func main() {
router := gin.Default()
router.LoadHTMLGlob("static/*")
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
v.RegisterValidation("ageValidator", AgeValidator)
}
router.Use(func(context *gin.Context) {
fmt.Println("url=", context.Request.URL.Path)
context.Next()
})
router.Use(Logger())
router.GET("/test", func(context *gin.Context) {
context.String(http.StatusOK, "hello test")
})
router.GET("/", func(context *gin.Context) {
//静态页面
context.HTML(http.StatusOK, "index.html", nil)
})
router.GET("/param/:name", func(context *gin.Context) {
//地址获取值
name := context.Param("name")
context.String(http.StatusOK, "name=%s", name)
})
router.GET("/params/:name/*action", func(context *gin.Context) {
name := context.Param("name")
//任意可以替代action
action := context.Param("action")
context.String(http.StatusOK, "name=%s,action=%s", name, action)
})
router.GET("/getValue", func(context *gin.Context) {
//常见的url?name=123&age=1
name := context.DefaultQuery("name", "")
age := context.Query("age")
context.String(http.StatusOK, "name=%s,age=%s", name, age)
})
router.POST("/form_post", func(context *gin.Context) {
//一般的post
name := context.DefaultPostForm("name", "")
age := context.PostForm("age")
context.String(http.StatusOK, "name=%s,age=%s", name, age)
})
router.POST("/upload_file", func(context *gin.Context) {
//上传文件
file, err := context.FormFile("file")
if err != nil {
context.String(http.StatusBadRequest, "bad request")
}
fileName := file.Filename
fmt.Println("filename=", fileName)
if err := context.SaveUploadedFile(file, fileName); err != nil {
context.String(http.StatusBadRequest, "upload file fail")
}
context.String(http.StatusOK, "upload file success")
})
v1 := router.Group("/v1")
v1.GET("/test1", func(context *gin.Context) {
context.String(http.StatusOK, "test1")
})
v1.GET("/test2", func(context *gin.Context) {
context.String(http.StatusOK, "test2")
})
router.POST("/user_json", func(context *gin.Context) {
var user User
if context.BindJSON(&user) == nil {
context.String(http.StatusOK, "name=%s,age=%d", user.Name, user.Age)
}
})
router.POST("/user_form", func(context *gin.Context) {
var user User
if error := context.Bind(&user); error == nil {
context.String(http.StatusOK, "name=%s,age=%d", user.Name, user.Age)
} else {
context.String(http.StatusOK, error.Error())
}
})
router.GET("/user_get", func(context *gin.Context) {
var user User
if error := context.ShouldBindQuery(&user); error == nil {
context.String(http.StatusOK, "name=%s,age=%d", user.Name, user.Age)
} else {
context.String(http.StatusOK, error.Error())
}
})
router.GET("/redirect/baidu", func(context *gin.Context) {
//301跳转
context.Redirect(http.StatusMovedPermanently, "http://www.baidu.com")
})
router.GET("/go", func(context *gin.Context) {
//go 值拷贝
c := context.Copy()
go func() {
time.Sleep(5 * time.Second)
fmt.Println(c.Request.URL.Path)
}()
})
router.Run(":8100")
}