Gin框架路由

Gin框架路由

一、普通路由

r.GET("/index", func(c *gin.Context) {...})
r.GET("/login", func(c *gin.Context) {...})
r.POST("/login", func(c *gin.Context) {...})
package main

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

func main() {
	r := gin.Default()
	r.HEAD("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "head",
		})

	})
	r.GET("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "get",
		})

	})

	r.POST("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "post",
		})

	})

	r.PUT("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "put",
		})
	})
	r.PATCH("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "patch",
		})
	})
	r.DELETE("/index", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"method": "delete",
		})
	})

	r.Run(":9999")
}

二、Any

匹配所有请求方法的Any方法如下:

r.Any("/test", func(c *gin.Context) {...})
package main

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

func main() {
	r := gin.Default()
	
	// Any: 接收所有请求方式
	r.Any("/any", func(c *gin.Context) {
		switch c.Request.Method {
		case http.MethodGet:
			c.JSON(http.StatusOK, gin.H{
				"method": http.MethodGet,
			})
		case http.MethodPost:
			c.JSON(http.StatusOK, gin.H{
				"method": http.MethodPost,
			})
		case http.MethodHead:
			c.JSON(http.StatusOK, gin.H{
				"method": http.MethodHead,
			})
		case http.MethodDelete:
			c.JSON(http.StatusOK, gin.H{
				"method": http.MethodDelete,
			})

		}
	})

	r.Run(":9999")
}

三、NoRoute

为没有配置处理函数的路由添加处理程序,默认情况下它返回404代码,下面的代码为没有匹配到路由的请求都返回views/404.html页面。

r.NoRoute(func(c *gin.Context) {
		c.HTML(http.StatusNotFound, "views/404.html", nil)
	})
package main

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

func main() {
	r := gin.Default()
	
	// NoRoute:处理不存在的路由
	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "访问资源不存在",
		})

	})
	r.Run(":9999")
}

四、路由组

我们可以将拥有共同URL前缀的路由划分为一个路由组。习惯性一对{}包裹同组的路由,这只是为了看着清晰,你用不用{}包裹功能上没什么区别。

package main

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

func main() {
	r := gin.Default()
	
	// 路由组
	bookRouteGroup := r.Group("/book")
	{
		bookRouteGroup.GET("/details", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{
				"msg": "book",
			})
		})

		bookRouteGroup.POST("/add", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{
				"msg": "add book",
			})
		})
		bookRouteGroup.DELETE("/move", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{
				"msg": "delete book",
			})
		})
	}

	r.Run(":9999")
}

五、路由组嵌套

路由组也是支持嵌套的,例如:

package main

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

func main() {
	r := gin.Default()
	
	// 路由组嵌套
	shopRouteGroup := r.Group("/shop")
	{
		shopRouteGroup.GET("/details", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{
				"msg": "shop",
			})
		})

		shopRouteGroup.POST("/add", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{
				"msg": "add shop",
			})
		})

		// 路由嵌套
		shopOrderRouteGroup := shopRouteGroup.Group("shopOrder")
		shopOrderRouteGroup.GET("/details", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{
				"msg": "shop order",
			})
		})

		shopOrderRouteGroup.POST("/add", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{
				"msg": "add shop order",
			})
		})
		shopOrderRouteGroup.DELETE("/move", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{
				"msg": "delete shop order",
			})
		})
	}

	// NoRoute:处理不存在的路由
	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "访问资源不存在",
		})

	})
	r.Run(":9999")
}

通常我们将路由分组用在划分业务逻辑或划分API版本时。

六、路由原理

Gin框架中的路由使用的是httprouter这个库。

其基本原理就是构造一个路由地址的前缀树。

posted @ 2021-11-30 22:22  RandySun  阅读(210)  评论(0编辑  收藏  举报