go gin (嵌套)路由组 NoRoute统一处理404 请求转发 Redirect重定向

6 篇文章 0 订阅

r.Group后的大括号可以不要

package main

import (
	"fmt"
	"html/template"
	"log"
	"mime/multipart"
	"net/http"
	"path"
	"time"

	"github.com/gin-gonic/gin"
)

func main() {
	mainGin()
}

func mainGin() {
	r := gin.Default()

	g1 := r.Group("/g1")
	{
		// get /g1/1
		g1.GET("/1", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{"msg": "g11"})
		})
		// post /g1/2
		g1.POST("/2", func(c *gin.Context) {
			c.JSON(http.StatusOK, gin.H{"msg": "g12"})
		})
		g2 := g1.Group("/g2")
		{
			// get /g1/g2/1
			g2.GET("/3", func(c *gin.Context) {
				c.JSON(http.StatusOK, gin.H{"msg": "g23"})
			})
		}
	}

	//处理url请求
	r.NoRoute(func(c *gin.Context) {
		c.HTML(http.StatusNotFound, "404.html", nil)
	})
	//请求转发
	r.GET("/b5", func(c *gin.Context) {
		c.Request.URL.Path = "/b2"
		r.HandleContext(c)
	})
	//重定向
	r.GET("/b4", func(c *gin.Context) {
		c.Redirect(http.StatusFound, "https://www.google.com")
	})
	//重定向
	r.GET("/b3", func(c *gin.Context) {
		//c.Redirect(http.StatusTemporaryRedirect, "/b2")
		c.Redirect(http.StatusMovedPermanently, "/b2")
	})
重定向遇到的method的bug:
    访问项目时,使用中间件查看session里有没有user字段,
    如果没有就重定向到login,login的get/post和logout没有使用中间件。
    代码是c.Redirect(http.StatusPermanentRedirect , "/login")。
    发现get方法访问就get到此页面(正常),post访问就post到此页面(异常),
    因为post用来提交登录信息。需要全都设置为get方法。
    一开始修改c.Request.Method="GET",发现method是改了,依旧是POST请求。
    解决方法:
        重定向代码改成c.Redirect(http.StatusMovedPermanently , "/login")
        参考https://gin-gonic.com/zh-cn/docs/examples/redirects/
        和http://psychedelicnekopunch.com/archives/1848
    推荐使用301(StatusMovedPermanently)302(StatusFound)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Go语言中,可以使用Gin框架来进行路由的模糊匹配。Gin框架提供了`Group`和`Any`方法来实现路由的模糊匹配。以下是一个简单的示例: ```go package main import ( "github.com/gin-gonic/gin" ) func main() { router := gin.Default() router.GET("/users", getUsers) router.GET("/users/:id", getUserByID) // 使用 Group 进行模糊匹配 v1 := router.Group("/v1") { v1.GET("/products", getProducts) v1.GET("/products/:id", getProductByID) } router.Run(":8080") } func getUsers(c *gin.Context) { c.JSON(200, gin.H{ "message": "Get all users", }) } func getUserByID(c *gin.Context) { id := c.Param("id") c.JSON(200, gin.H{ "message": "Get user by ID", "id": id, }) } func getProducts(c *gin.Context) { c.JSON(200, gin.H{ "message": "Get all products", }) } func getProductByID(c *gin.Context) { id := c.Param("id") c.JSON(200, gin.H{ "message": "Get product by ID", "id": id, }) } ``` 在上面的例子中,我们使用了`router.GET`方法来定义了一些基本的路由,如`/users`和`/users/:id`。然后,我们使用`Group`方法创建了一个路由`v1`,并在该路由内定义了一些模糊匹配的路由,如`/v1/products`和`/v1/products/:id`。 通过使用路由,我们可以实现对指定路由路径的前缀或者模式进行匹配,从而实现模糊匹配的效果。 需要注意的是,Gin框架还提供了`Any`方法,可以用于匹配所有HTTP方法(GET、POST、PUT、DELETE等)。你可以根据实际需求选择合适的方法来实现模糊匹配。同时,你还可以使用通配符`*`来匹配多级路径,如`/users/*action`可以匹配`/users/get`, `/users/add`等路径。 以上示例只是一个简单的演示,你可以根据实际需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值