mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-01-23 09:00:20 +08:00
feat:Add Routes
method for server (#2125)
Co-authored-by: czyt <czyt@w.cn>
This commit is contained in:
parent
24787a946b
commit
d71b3c841f
@ -79,6 +79,15 @@ func (s *Server) PrintRoutes() {
|
||||
s.ngin.print()
|
||||
}
|
||||
|
||||
// Routes returns the Http routers which are registered in the engine
|
||||
func (s *Server) Routes() []Route {
|
||||
routers := make([]Route, len(s.ngin.routes))
|
||||
for _, r := range s.ngin.routes {
|
||||
routers = append(routers, r.routes...)
|
||||
}
|
||||
return routers
|
||||
}
|
||||
|
||||
// Start starts the Server.
|
||||
// Graceful shutdown is enabled by default.
|
||||
// Use proc.SetTimeToForceQuit to customize the graceful shutdown period.
|
||||
|
@ -425,6 +425,56 @@ Port: 54321
|
||||
assert.Equal(t, expect, out)
|
||||
}
|
||||
|
||||
func TestServer_Routes(t *testing.T) {
|
||||
const (
|
||||
configYaml = `
|
||||
Name: foo
|
||||
Port: 54321
|
||||
`
|
||||
expect = `GET /foo GET /bar 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,
|
||||
},
|
||||
})
|
||||
|
||||
routes := svr.Routes()
|
||||
var buf strings.Builder
|
||||
for i := 0; i < len(routes); i++ {
|
||||
buf.WriteString(routes[i].Method)
|
||||
buf.WriteString(" ")
|
||||
buf.WriteString(routes[i].Path)
|
||||
buf.WriteString(" ")
|
||||
}
|
||||
|
||||
assert.Equal(t, expect, strings.Trim(buf.String(), " "))
|
||||
}
|
||||
|
||||
func TestHandleError(t *testing.T) {
|
||||
assert.NotPanics(t, func() {
|
||||
handleError(nil)
|
||||
|
Loading…
Reference in New Issue
Block a user