From 666c35836df62545e5549f94ef3b81720fc0ae0a Mon Sep 17 00:00:00 2001 From: Leon Date: Fri, 16 Feb 2024 21:37:19 +0800 Subject: [PATCH] Add route printing functionality to main.go --- cmd/main.go | 6 ++++++ pkg/mygin/gin.go | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/cmd/main.go b/cmd/main.go index 25d4954..7f9ca84 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -4,9 +4,11 @@ import ( "context" "fmt" "go-gin/cmd/srv/controller" + "go-gin/pkg/mygin" "go-gin/pkg/utils" "go-gin/service/singleton" + "github.com/gin-gonic/gin" "github.com/ory/graceful" flag "github.com/spf13/pflag" ) @@ -62,6 +64,10 @@ func main() { fmt.Printf(" - %-7s: %s\n", "Network", utils.Colorize(utils.ColorGreen, fmt.Sprintf("http://%s:%d", ip, port))) } } + + fmt.Println() + fmt.Println("Server available routes:") + mygin.PrintRoute(srv.Handler.(*gin.Engine)) fmt.Println() } diff --git a/pkg/mygin/gin.go b/pkg/mygin/gin.go index 5ca42e8..ca7b566 100644 --- a/pkg/mygin/gin.go +++ b/pkg/mygin/gin.go @@ -2,6 +2,7 @@ package mygin import ( "fmt" + "go-gin/pkg/utils" "net/http" "strings" @@ -64,3 +65,10 @@ func BindForm(c *gin.Context, isForm bool, form interface{}) error { return c.ShouldBindJSON(form) } } + +func PrintRoute(r *gin.Engine) { + routes := r.Routes() + for _, route := range routes { + fmt.Printf(" - Route Path: %s, Method: %s\n", utils.Colorize(utils.ColorGreen, route.Path), utils.Colorize(utils.ColorGreen, route.Method)) + } +}