Gin
安装
go get -u -v github.com/gin-gonic/gin
package main
import (
"fmt"
"log"
"net/http"
"github.com/gin-gonic/gin"
)
type student struct {
Name string
Age int8
}
var r = gin.Default()
func main() {
runMain()
r.Run(":8090")
}
func runMain() {
MyGet()
MyPost()
GroupRoute()
uploadFile()
templateFun()
}
func templateFun() {
r.LoadHTMLGlob("templates/*")
stu1 := &student{Name: "Chuancy", Age: 20}
stu2 := &student{Name: "Tty", Age: 22}
r.GET("/arr", func(c *gin.Context) {
c.HTML(http.StatusOK, "tom.tmpl", gin.H{
"title": "Gin",
"stuArr": [2]*student{stu1, stu2},
})
})
}
func uploadFile() {
fmt.Printf("tool.GetEnvDefault(\"name\", \"hhh\"): %v\n", tool.GetEnvDefault("name", "hhh"))
//curl --location --request POST 'http://localhost:8090/upload' --form 'file=@"/D:/文档/calico/calico-recomand.yaml.yaml"'
r.POST("/upload", func(ctx *gin.Context) {
fh, err := ctx.FormFile("file")
if err == nil {
dest := "D:\\download\\" + fh.Filename
ctx.SaveUploadedFile(fh, dest)
} else {
ctx.String(400, "%s upload failed!", fh.Filename)
}
ctx.String(200, "%s uploaded!!", fh.Filename)
})
r.POST("/uploads", func(ctx *gin.Context) {
form, _ := ctx.MultipartForm()
files := form.File["upload[]"]
for _, file := range files {
log.Print(file.Filename)
dest := "D:\\download\\" + file.Filename
ctx.SaveUploadedFile(file, dest)
}
ctx.String(200, "%d files uploaded!!", len(files))
})
}
func MyGet() {
// 无参get
// curl http://localhost:8090/
r.GET("/", func(ctx *gin.Context) {
ctx.String(http.StatusOK, "无参GET")
})
r.GET("/index", func(ctx *gin.Context) {
ctx.String(http.StatusOK, "index")
})
// 解析路径参数
// curl http://localhost:8090/foo/chauncy
r.GET("/foo/:name", func(ctx *gin.Context) {
name := ctx.Param("name")
ctx.String(200, "hello,%s", name)
})
// 获取Query参数
// curl http://localhost:8090/users?name=chuancy&role=teacher
r.GET("/users", func(ctx *gin.Context) {
name := ctx.Query("name")
role := ctx.DefaultQuery("role", "IT")
ctx.String(200, "%s is a %s", name, role)
})
}
func MyPost() {
// curl http://localhost:8090/form -X POST -d 'username=chauncy&password=1234'
r.POST("/form", func(ctx *gin.Context) {
username := ctx.PostForm("username")
password := ctx.DefaultPostForm("password", "00000") // 默认值
ctx.JSON(200, gin.H{
"username": username,
"password": password,
})
})
// get post参数混合
// curl --location --request POST 'localhost:8090/posts?id=0&page=12' --form 'username="chuancy"'
r.POST("/posts", func(ctx *gin.Context) {
id := ctx.Query("id")
page := ctx.DefaultQuery("page", "0")
username := ctx.PostForm("username")
password := ctx.DefaultPostForm("password", "0000000")
ctx.JSON(200, gin.H{
"id": id,
"page": page,
"username": username,
"passowrod": password,
})
})
// map参数
// curl --location -g --request POST 'http://localhost:8090/post?ids[chauncy]=001&ids[jin]=002' --form 'names[foo]="tla"' --form 'names[bar]="Wiei"'
r.POST("/post", func(ctx *gin.Context) {
ids := ctx.QueryMap("ids")
names := ctx.PostFormMap("names")
ctx.JSON(200, gin.H{
"ids": ids,
"names": names,
})
})
// 重定向
r.GET("/redirect", func(ctx *gin.Context) {
ctx.Redirect(http.StatusMovedPermanently, "/index")
})
r.GET("/goindex", func(ctx *gin.Context) {
ctx.Request.URL.Path = "/"
r.HandleContext(ctx)
})
}
// 分组路由
func GroupRoute() {
defaultHandler := func(ctx *gin.Context) {
ctx.JSON(200, gin.H{
"path": ctx.FullPath(),
})
}
v1 := r.Group("/v1")
v1.GET("/foo", defaultHandler)
v1.GET("/bar", func(ctx *gin.Context) {
ctx.String(200, "/v1/bar")
})
v2 := r.Group("/v2")
{
v2.GET("/posts", defaultHandler)
}
}