HTTP重定向

HTTP 重定向很容易。 内部、外部重定向均支持

r.GET("/test", func(c *gin.Context) {
		c.Redirect(http.StatusMovedPermanently, "http://www.sogo.com/")

	})

路由重定向

路由重定向,使用HandleContext

r.GET("/test", func(c *gin.Context) {
    // 指定重定向的URL
    c.Request.URL.Path = "/test2"
    r.HandleContext(c)
})
r.GET("/test2", func(c *gin.Context) {
    c.JSON(http.StatusOK, gin.H{"hello": "world"})
})

此外,还有一个可以匹配所有请求方法的Any方法如下:

r.Any("/user", func(c *gin.Context) {
		 switch c.Request.Method {
		 case  "GET":
		 	c.JSON(http.StatusOK,gin.H{"method":"GET"	})
		 case http.MethodPost:
		 	c.JSON(
		 		http.StatusOK,gin.H{
		 			"method":"post",
				})

		 //....
		 }
		 c.JSON(http.StatusOK,gin.H{
		 	"method": "any",
		 })
	 })

我们可以看一下源码:

/ Any registers a route that matches all the HTTP methods.
// GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE.
func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes {
	group.handle(http.MethodGet, relativePath, handlers)
	group.handle(http.MethodPost, relativePath, handlers)
	group.handle(http.MethodPut, relativePath, handlers)
	group.handle(http.MethodPatch, relativePath, handlers)
	group.handle(http.MethodHead, relativePath, handlers)
	group.handle(http.MethodOptions, relativePath, handlers)
	group.handle(http.MethodDelete, relativePath, handlers)
	group.handle(http.MethodConnect, relativePath, handlers)
	group.handle(http.MethodTrace, relativePath, handlers)
	return group.returnObj()
}

源码:


// Common HTTP methods.
//
// Unless otherwise noted, these are defined in RFC 7231 section 4.3.
const (
	MethodGet     = "GET"
	MethodHead    = "HEAD"
	MethodPost    = "POST"
	MethodPut     = "PUT"
	MethodPatch   = "PATCH" // RFC 5789
	MethodDelete  = "DELETE"
	MethodConnect = "CONNECT"
	MethodOptions = "OPTIONS"
	MethodTrace   = "TRACE"
)

为没有配置处理函数的路由添加处理程序,默认情况下它返回404代码,

/为没有的页面请求设置默认norouter
r.NoRoute(func(c *gin.Context) {
   c.JSON(http.StatusNotFound,gin.H{
      "是不是没想到":"007",
   })
})

router.go的源码:

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main()  {
	r:=gin.Default()
	//获取某个信息
	//访问/index请求,会访问这个页面,或者处理逻辑,也就是当前路由
	r.GET("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK,gin.H{
			"method":"get",
		})
	})
	//更新某个请求
	r.PUT("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK,gin.H{
			"method":"put",
		})
	})
	r.DELETE("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK,gin.H{
			"method":"delete",
		})
	})
	r.GET("/login", func(c *gin.Context) {})
	//创建某个信息 ,注册会员信息等
	r.POST("/login", func(c *gin.Context) {
		c.JSON(http.StatusOK,gin.H{
			"method":"post",
		})
	})
	r.Any("/user", func(c *gin.Context) {
		switch c.Request.Method {
		case  "GET":
			c.JSON(http.StatusOK,gin.H{"method":"GET"	})
		case http.MethodPost:
			c.JSON(
				http.StatusOK,gin.H{
					"method":"post",
				})

			//....
		}
		c.JSON(http.StatusOK,gin.H{
			"method": "any",
		})
	})

	//为没有的页面请求设置默认norouter
	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusNotFound,gin.H{
			"是不是没想到":"007",
		})
	})

	//写一个视频的首页和详情页
	//r.GET("/video/index", func(c *gin.Context) {
	//	c.JSON(http.StatusOK,gin.H{
	//		"msg":"/video/index",
	//	})
	//})
	//	r.GET("/video/shop", func(c *gin.Context) {
	//		c.JSON(http.StatusOK,gin.H{
	//			"msg":"/video/index",
	//		})
	//	})
	//	r.GET("/video/goods", func(c *gin.Context) {
	//		c.JSON(http.StatusOK,gin.H{
	//			"msg":"/video/index",
	//		})
	//	})
	//路由组的组
	//公用的前缀提取出来,创建一个组,便于管理
	VideoGroup:=r.Group("/video")
	{
		VideoGroup.GET("/index", func(c *gin.Context) {
			c.JSON(http.StatusOK,gin.H{
				"msg":"/video/index",
			})
		})
		VideoGroup.GET("/goods", func(c *gin.Context) {
			c.JSON(http.StatusOK,gin.H{
				"msg":"/video/goods",
			})
		})
		VideoGroup.GET("/shop", func(c *gin.Context) {
			c.JSON(http.StatusOK,gin.H{
				"msg":"/video/shop",
			})
		})
	}

	//写一个商品的首页和详情页
	r.GET("/shop/index", func(c *gin.Context) {
		c.JSON(http.StatusOK,gin.H{
			"md=sg":"/shop/index",
		})
	})
	r.Run()

}