Giter VIP home page Giter VIP logo

Comments (4)

JimChenWYU avatar JimChenWYU commented on September 27, 2024

Base on the current structure of gin, I dont think gin can solve your problem.

Just group by prefix

image

All routes will be registered to engine.Handlers

image
image

So only provides the global 404 handler

image

from gin.

jerome-laforge avatar jerome-laforge commented on September 27, 2024

Thanks for this confirmation @JimChenWYU

from gin.

jerome-laforge avatar jerome-laforge commented on September 27, 2024

I won't use gin.NoRoute(), instead of I will use this workaround:

func noRoute(r *gin.Engine, basePath string, hfs ...gin.HandlerFunc) {
	r.Any(basePath+"/:path", hfs...)
}
...
noRoute(r, "/a", func(ctx *gin.Context) {
	// manage no route for /a/*
})
noRoute(r, "/b", func(ctx *gin.Context) {
	// manage no route for /b/*
})

Thx again @JimChenWYU for your support

from gin.

jerome-laforge avatar jerome-laforge commented on September 27, 2024

Another workaround that is more robust but also more weird, it is use 2 gin.Engine for each group (the global mergeEngine & the specific engineOnlyForA or engineOnlyForB)

mergeEngine := gin.New()

// manage all routes + noroutes for /a/*
{
	engineOnlyForA := gin.New()
	engineOnlyForA.GET("/a/bc", func(c *gin.Context) {
		// do stuff for /a/bc
	})
	engineOnlyForA.GET("/a/de", func(c *gin.Context) {
		// do stuff for /a/de
	})
	engineOnlyForA.NoRoute(func(c *gin.Context) {
		// no route for /a/*
	})

	mergeEngine.Any("/a/*path", gin.WrapH(engineOnlyForA))
}

// manage all routes + noroutes for /b/* 
{
	engineOnlyForB := gin.New()
	engineOnlyForB.GET("/b/cd", func(c *gin.Context) {
		// do stuff for /b/cd
	})
	engineOnlyForB.GET("/b/ef", func(c *gin.Context) {
		// do stuff for /b/ef
	})
	engineOnlyForB.NoRoute(func(c *gin.Context) {
		// no route for /b/*
	})

	mergeEngine.Any("/b/*path", gin.WrapH(engineOnlyForB))
}

from gin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.