mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
feat: print routes (#1964)
* feat: print rest routes * feat: print rest routes
This commit is contained in:
parent
87800419f5
commit
b6b96d9dad
@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/justinas/alice"
|
||||
@ -184,6 +185,22 @@ func (ng *engine) notFoundHandler(next http.Handler) http.Handler {
|
||||
})
|
||||
}
|
||||
|
||||
func (ng *engine) print() {
|
||||
var routes []string
|
||||
|
||||
for _, fr := range ng.routes {
|
||||
for _, route := range fr.routes {
|
||||
routes = append(routes, fmt.Sprintf("%s %s", route.Method, route.Path))
|
||||
}
|
||||
}
|
||||
|
||||
sort.Strings(routes)
|
||||
|
||||
for _, route := range routes {
|
||||
fmt.Println(route)
|
||||
}
|
||||
}
|
||||
|
||||
func (ng *engine) setTlsConfig(cfg *tls.Config) {
|
||||
ng.tlsConfig = cfg
|
||||
}
|
||||
|
@ -73,6 +73,11 @@ func (s *Server) AddRoute(r Route, opts ...RouteOption) {
|
||||
s.AddRoutes([]Route{r}, opts...)
|
||||
}
|
||||
|
||||
// PrintRoutes prints the added routes to stdout.
|
||||
func (s *Server) PrintRoutes() {
|
||||
s.ngin.print()
|
||||
}
|
||||
|
||||
// Start starts the Server.
|
||||
// Graceful shutdown is enabled by default.
|
||||
// Use proc.SetTimeToForceQuit to customize the graceful shutdown period.
|
||||
|
@ -7,6 +7,8 @@ import (
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@ -341,3 +343,67 @@ Port: 54321
|
||||
}, "local")
|
||||
opt(svr)
|
||||
}
|
||||
|
||||
func TestServer_PrintRoutes(t *testing.T) {
|
||||
const (
|
||||
configYaml = `
|
||||
Name: foo
|
||||
Port: 54321
|
||||
`
|
||||
expect = `GET /bar
|
||||
GET /foo
|
||||
GET /foo/:bar
|
||||
GET /foo/:bar/baz
|
||||
`
|
||||
)
|
||||
|
||||
var cnf RestConf
|
||||
assert.Nil(t, conf.LoadFromYamlBytes([]byte(configYaml), &cnf))
|
||||
|
||||
svr, err := NewServer(cnf)
|
||||
assert.Nil(t, err)
|
||||
|
||||
svr.AddRoutes([]Route{
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/foo",
|
||||
Handler: http.NotFound,
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/bar",
|
||||
Handler: http.NotFound,
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/foo/:bar",
|
||||
Handler: http.NotFound,
|
||||
},
|
||||
{
|
||||
Method: http.MethodGet,
|
||||
Path: "/foo/:bar/baz",
|
||||
Handler: http.NotFound,
|
||||
},
|
||||
})
|
||||
|
||||
old := os.Stdout
|
||||
r, w, err := os.Pipe()
|
||||
assert.Nil(t, err)
|
||||
os.Stdout = w
|
||||
defer func() {
|
||||
os.Stdout = old
|
||||
}()
|
||||
|
||||
svr.PrintRoutes()
|
||||
ch := make(chan string)
|
||||
|
||||
go func() {
|
||||
var buf strings.Builder
|
||||
io.Copy(&buf, r)
|
||||
ch <- buf.String()
|
||||
}()
|
||||
|
||||
w.Close()
|
||||
out := <-ch
|
||||
assert.Equal(t, expect, out)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user