rename GetRouterMap to GetRouterArray for ghttp.Server
@@ -326,12 +326,12 @@ func (s *Server) Start() error {
})
}
- s.DumpRouterMap()
+ s.dumpRouterMap()
return nil
}
// DumpRouterMap dumps the router map to the log.
-func (s *Server) DumpRouterMap() {
+func (s *Server) dumpRouterMap() {
if s.config.DumpRouterMap && len(s.routesMap) > 0 {
buffer := bytes.NewBuffer(nil)
table := tablewriter.NewWriter(buffer)
@@ -350,9 +350,8 @@ func (s *Server) DumpRouterMap() {
tablewriter.ALIGN_LEFT,
})
- for _, array := range s.GetRouterMap() {
+ for _, item := range s.GetRouterArray() {
data := make([]string, 8)
- for _, item := range array {
data[0] = item.Server
data[1] = item.Address
data[2] = item.Domain
@@ -363,15 +362,14 @@ func (s *Server) DumpRouterMap() {
data[7] = item.Middleware
table.Append(data)
}
- }
table.Render()
s.config.Logger.Header(false).Printf("\n%s", buffer.String())
}
}
-// GetRouterMap retrieves and returns the router map.
+// GetRouterArray retrieves and returns the router array.
// The key of the returned map is the domain of the server.
-func (s *Server) GetRouterMap() map[string][]RouterItem {
+func (s *Server) GetRouterArray() []RouterItem {
m := make(map[string]*garray.SortedArray)
address := s.config.Address
if s.config.HTTPSAddr != "" {
@@ -412,7 +410,7 @@ func (s *Server) GetRouterMap() map[string][]RouterItem {
// The value of the map is a custom sorted array.
if _, ok := m[item.Domain]; !ok {
// Sort in ASC order.
- m[item.Domain] = garray.NewSortedArraySize(100, func(v1, v2 interface{}) int {
+ m[item.Domain] = garray.NewSortedArray(func(v1, v2 interface{}) int {
item1 := v1.(RouterItem)
item2 := v2.(RouterItem)
r := 0
@@ -435,16 +433,13 @@ func (s *Server) GetRouterMap() map[string][]RouterItem {
m[item.Domain].Add(item)
}
}
- routerMap := make(map[string][]RouterItem, len(m))
- for domain, array := range m {
- if routerMap[domain] == nil {
- routerMap[domain] = make([]RouterItem, array.Len())
- }
- for k, v := range array.Slice() {
- routerMap[domain][k] = v.(RouterItem)
+ routerArray := make([]RouterItem, 0, 128)
+ for _, array := range m {
+ for _, v := range array.Slice() {
+ routerArray = append(routerArray, v.(RouterItem))
}
}
- return routerMap
+ return routerArray
}
// Run starts server listening in blocking way.
