Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using r.Use causes middleware to be hit on every request #49

Open
SharkFourSix opened this issue Mar 26, 2024 · 1 comment
Open

Using r.Use causes middleware to be hit on every request #49

SharkFourSix opened this issue Mar 26, 2024 · 1 comment

Comments

@SharkFourSix
Copy link

SharkFourSix commented Mar 26, 2024

When using the middleware as

app.Use(static.Serve())

Every request first hits the static middleware which causes errors to show up on in the logs indicating that the specified files do not exist.

Is there a better way to use the static middleware with its own middleware as well so it only gets hit when the request matches the url passed to the .Serve function?

@JayC-SF
Copy link

JayC-SF commented Oct 4, 2024

When using the middleware as

app.Use(static.Serve())

Every request first hits the static middleware which causes errors to show up on in the logs indicating that the specified files do not exist.

Is there a better way to use the static middleware with its own middleware as well so it only gets hit when the request matches the url passed to the .Serve function?

This is my alternative to this issue 👍

func main() {
	router := gin.Default()

	router.GET("/hello", func(c *gin.Context) {
		fmt.Println("HELLO REQ")
		c.JSON(http.StatusOK, gin.H{"hello": "there"})
	})
	router.NoRoute(static.Serve("/", static.LocalFile("../frontend/dist", true)))
	router.Run(":8080")
}

I tested to see if the middleware gets hit on every request and it doesn't for this one. I tested it by wrapping the middleware with a handler function and put a logger. The logger only prints when no route was found :)

Let me know what you guys think :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants