https://www.liwenzhou.com/posts/Go/Gin_framework/
package main
import (
"encoding/json"
"github.com/gin-gonic/gin"
"html/template"
"log"
"net/http"
"time"
)
type UserInfo struct {
Name string `json:"name"`
Age int `json:"age"`
Gender string `json:"gender"`
}
func StatCost() gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
c.Set("start", start.String())
c.Next()
cost := time.Since(start)
log.Println(cost)
}
}
func router01() http.Handler {
router := gin.New()
router.Use(gin.Recovery())
router.GET("/router01", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "welcome server 01",
})
})
return router
}
func router02() http.Handler {
router := gin.New()
router.Use(gin.Recovery())
router.GET("/router02", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "welcome server 02",
})
})
return router
}
func main() {
router := gin.Default()
router.Use(StatCost())
router.SetFuncMap(template.FuncMap{
"safe": func(str string) template.HTML {
return template.HTML(str)
},
})
router.LoadHTMLFiles("./index.tmpl")
router.NoRoute(func(c *gin.Context) {
c.JSON(http.StatusNotFound, gin.H{
"message": "页面未找到",
})
})
_ = map[string]interface{}{
"name": "张建平",
"age": 18,
}
router.GET("/user_1", func(c *gin.Context) {
userInfo := UserInfo{
Name: "张建平",
Age: 25,
Gender: "male",
}
c.JSON(http.StatusOK, userInfo)
})
router.GET("/book", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "get",
})
})
router.POST("/book", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "post",
})
})
router.PUT("/book", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "put",
})
})
router.DELETE("/book", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "delete",
})
})
router.GET("/index", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", "<a href='https://www.baidu.com/'>点击跳转</a>")
})
router.GET("/query", func(c *gin.Context) {
b, _ := c.GetRawData()
var m map[string]interface{}
_ = json.Unmarshal(b, &m)
username := c.DefaultQuery("username", "张建平")
address := c.Query("address")
c.JSON(http.StatusOK, gin.H{
"message": "ok",
"username": username,
"address": address,
"json": m,
})
})
router.GET("/user/search/:username/:address", func(c *gin.Context) {
username := c.Param("username")
address := c.Param("address")
c.JSON(http.StatusOK, gin.H{
"message": "ok",
"username": username,
"address": address,
})
})
router.GET("/redirect_1", func(c *gin.Context) {
c.Redirect(http.StatusMovedPermanently, "https://52-qq.cnblogs.com/")
})
router.GET("/redirect_2", func(c *gin.Context) {
c.Request.URL.Path = "/redirect_1"
router.HandleContext(c)
})
router.Any("/any", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "any_ok",
})
})
router.GET("/middle", StatCost(), func(c *gin.Context) {
start := c.MustGet("start").(string)
log.Println(start)
c.JSON(http.StatusOK, gin.H{
"message": "middle",
})
})
userGroup := router.Group("/user")
{
userGroup.GET("/index", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "用户主页",
})
})
userGroup.GET("/home", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "用户家目录",
})
})
userAdminGroup := userGroup.Group("/admin", StatCost())
{
userAdminGroup.GET("/index", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "用户管理首页",
})
})
}
}
_ = router.Run(":8080")
}