gin icon indicating copy to clipboard operation
gin copied to clipboard

Why execute router.Use(xxx) first and then router.NoRoute(xxx)?

Open MmToon opened this issue 3 years ago • 1 comments

Description

Why execute router.Use(xxx) first and then router.NoRoute(xxx)? In addition, if it is necessary to run router.Use after occurrence of router.NoRoute? Because I did some request verification in router.Use(xxx).

How to reproduce

package main

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

func main() {
	router := gin.Default()
	//The coding sequence of router.NoRoute(xxx) and router.Use(xxx) does not affect the execution result.
	router.NoRoute(func (c *gin.Context) {
		c.String(404, "404")
	})
	router.Use(func (c *gin.Context) {
		fmt.Println("init")
	})
	router.GET("/", func(c *gin.Context) {
		c.String(200, "OK")
	})
	router.Run()
}

Expectations

$ curl http://127.0.0.1:8080/other
404(But I do not want router.Use(xxx) to run.)

Actual result

$ curl http://127.0.0.1:8080/other
404(console print "init".)

Environment

  • go version: go1.19.3 windows/amd64
  • gin version (or commit ref): v1.8.1
  • operating system: Windows 10 professional(64 bit)

MmToon avatar Nov 27 '22 05:11 MmToon

This is the way to solve the problem, in router.Use(xxx).

......
if fullPath = c.FullPath(); fullPath == "" {
	c.String(404, "404")
	c.Abort()
	return
}
......

MmToon avatar Nov 27 '22 06:11 MmToon