Skip to content

juvenn/gin-route

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Composable route groups for Gin

GitHub Actions Workflow Status GitHub Release Go Report Card

Composable route groups for Gin.

A route group defines a set of routes that share the same path prefix and middlewares. The route items defined in such group can be enumerated, and composed among other groups.

Usage

go get github.com/juvenn/gin-route
import (
  "github.com/gin-gonic/gin"
  route "github.com/juvenn/gin-route"
)

func main() {
  photos := route.NewGroup("/photos")
  photos.WithScope("", func(g *route.RouteGroup) {
    g.Use(logMiddleware)
    g.Handle("GET", "", queryPhotos)
    g.Handle("GET", "/:id", getPhoto)
    g.Handle("DELETE", "/:id", deletePhoto)
  })
  // Enumerate and print all routes
  for _, route := range photos.Routes() {
    fmt.Printf("%s %s %d\n", route.Method, route.Path, len(route.Handlers))
  }

  // Bind route to gin engine
  r := gin.Default()
  photos.Dock("/v1", r)
  photos.Dock("/v2", r)
  r.Run()
}