-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
56 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
"strings" | ||
|
||
"github.com/gin-gonic/gin" | ||
) | ||
|
||
// ListRoutes prints all registered routes in the Gin application, grouped by the first segment of the route | ||
func ListRoutes(router *gin.Engine) { | ||
routeMap := make(map[string][]string) | ||
|
||
for _, route := range router.Routes() { | ||
segments := strings.Split(route.Path, "/") | ||
if len(segments) > 1 { | ||
firstSegment := segments[1] | ||
key := "/" + firstSegment | ||
routeMap[key] = append(routeMap[key], fmt.Sprintf("%s -> %s", route.Path, route.Handler)) | ||
} | ||
} | ||
|
||
firstSegments := make([]string, 0, len(routeMap)) | ||
for k := range routeMap { | ||
firstSegments = append(firstSegments, k) | ||
} | ||
sort.Strings(firstSegments) | ||
|
||
for _, fs := range firstSegments { | ||
fmt.Printf("%s:\n", fs) | ||
for _, route := range routeMap[fs] { | ||
fmt.Println(" " + route) | ||
} | ||
fmt.Println() | ||
} | ||
} |