I
I
Ilya2015-10-03 14:09:23
go
Ilya, 2015-10-03 14:09:23

GIN: is it possible to nest a router in a router?

In all examples, the routers are in the same place, I did not find any attachments anywhere.

r := gin.New()
main := r.Group("/main", midl())
main.Get("/get", mainPkg.get)
another := r.Group("/another", midl())
//и т.д.

I would like to describe the routes in the package, and in the main file it’s just to enter something like
r := gin.New()
main := r.Group("/main", here_imported_route.Route)

#here_imported_route.go
package here_imported_route

Router := gin.New()
Router.Use(midl())
Router.Get("/test", hello)

we take the router created in another package, with the prescribed paths, but the / main prefix goes to neither. upon opening /main/test, hello will be returned to us
In general, a Django-like router.
Somehow confusingly wrote, but I hope you can understand.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Илья, 2015-10-05
@FireGM

В главном роутинге что-то вроде

package main

import (
  "path_to_pkg/pkg"

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

var r *gin.Engine

func init() {
  r = gin.New()
  pkg.Concon(r.Group("/pkg"))

}

func main() {
  r.Run(":8080")
}

В импортируемом пакете создадим функцию для конкатенации
#pkg.go
package pkg

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

func Concon(g *gin.RouterGroup) {
  g.GET("/ping", func(c *gin.Context) {
    c.String(200, "pong")
  })
}

при открытии 127.0.0.1:8080/pkg/ping получаем в ответ pong.

O
Oleg Shevelev, 2015-10-04
@mantyr

Просто соблюдайте входные и выходные параметры. Возвращать функции с нужным перечнем входных и выходных параметров тоже можно. Остальное дело техники.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question