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

Handling dynamic paths / routes #2102

Open
kravemir opened this issue Oct 23, 2019 · 3 comments · May be fixed by #3270
Open

Handling dynamic paths / routes #2102

kravemir opened this issue Oct 23, 2019 · 3 comments · May be fixed by #3270

Comments

@kravemir
Copy link

I am planning to create support for dynamic pages, where user/admin can create page using rich text, and assign it a path (actually, it would be a tree-like structure with "infinite" depth).

So, I will need routing, which handles calls in following order:

  1. static-path routes (API calls, fixed function pages,... and this is already supported by gin),
  2. dynamic-path "routes", which are handled by dynamic pages functionality.

Is it possible to do so with gin?

@kravemir
Copy link
Author

Probably, gin ain't tool/library suitable for my needs. Feel free to close this issue, if you like.

I'm going to use more versatile and convenient httptreemux, which provides features needed for above use-case: catch-all variable at end of the rule, and "overlapping/sub-set" rules evaluated according to specific priority.

@odiferousmint
Copy link

Yep, I have been having basic issues with the router used which I commented about in another issue. I cannot achieve this without conflicts (I could in another router, forgot which one):

/index.html
/js/socket.io.js
/socket.io/

using:

/*
router.Static("/", "./static")
or
router.StaticFS("/", gin.Dir("./static", false))
*/
router.GET("/socket.io/", gin.WrapH(server))
router.POST("/socket.io/", gin.WrapH(server))

where the directory structure is:

$ find static/
static/
static/js
static/js/socket.io.js
static/index.html

@aamirmousavi
Copy link

{
	r := gin.Default()

	routes:=ReadRoutesFromDB()

	r=UpdateRoutes(routes,r)

	r.GET("/updateroute",func(c *gin.Context){	//you don't need to use a route for updating them in runtime you can use UpdateRoutes function when you add a new route in your admin panel
		newRoutes:=[]string{
			"/newroute","/newroute2"
		}
		r=UpdateRoutes(newRoutes,r)
	})

	r.Run(":8080")
}

func ReadRoutesFromDB(){
	//you can read routes from database or..
	allRoutes:=[]string{
		"/","/aboutus","/contactus","/post/:postid","/post/news/:newsid"
	}
	return allRoutes
}

func UpdateRoutes(allRoutes []string, r *gin.Engine) *gin.Engine{
	for route:=range allRoutes{
		r.GET(route,func(c *gin.Context){
			c.String(200,c.Request.RequestURI)
		})
	}
}

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

Successfully merging a pull request may close this issue.

3 participants