fix issue in missing handler name for standard router handlers

This commit is contained in:
John Guo
2021-12-18 18:25:04 +08:00
parent 57a1a7607a
commit 4978955404
5 changed files with 19 additions and 12 deletions

View File

@ -127,7 +127,6 @@ const (
routeCacheDuration = time.Hour
methodNameInit = "Init"
methodNameShut = "Shut"
methodNameExit = "Exit"
ctxKeyForRequest = "gHttpRequestObject"
contentTypeXml = "text/xml"
contentTypeHtml = "text/html"

View File

@ -272,10 +272,11 @@ func (s *Server) getListenAddress() string {
func (s *Server) dumpRouterMap() {
var (
ctx = context.TODO()
routes = s.GetRoutes()
headers = []string{"SERVER", "DOMAIN", "ADDRESS", "METHOD", "ROUTE", "HANDLER", "MIDDLEWARE"}
isJustDefaultServerAndDomain = true
)
for _, item := range s.GetRoutes() {
for _, item := range routes {
if item.Server != DefaultServerName || item.Domain != DefaultDomainName {
isJustDefaultServerAndDomain = false
break
@ -284,7 +285,7 @@ func (s *Server) dumpRouterMap() {
if isJustDefaultServerAndDomain {
headers = []string{"ADDRESS", "METHOD", "ROUTE", "HANDLER", "MIDDLEWARE"}
}
if s.config.DumpRouterMap && len(s.routesMap) > 0 {
if s.config.DumpRouterMap && len(routes) > 0 {
buffer := bytes.NewBuffer(nil)
table := tablewriter.NewWriter(buffer)
table.SetHeader(headers)
@ -292,7 +293,7 @@ func (s *Server) dumpRouterMap() {
table.SetBorder(false)
table.SetCenterSeparator("|")
for _, item := range s.GetRoutes() {
for _, item := range routes {
var (
data = make([]string, 0)
handlerName = gstr.TrimRightStr(item.Handler.Name, "-fm")
@ -336,10 +337,11 @@ func (s *Server) GetOpenApi() *goai.OpenApiV3 {
}
// GetRoutes retrieves and returns the router array.
// The key of the returned map is the domain of the server.
func (s *Server) GetRoutes() []RouterItem {
m := make(map[string]*garray.SortedArray)
address := s.config.Address
var (
m = make(map[string]*garray.SortedArray)
address = s.config.Address
)
if s.config.HTTPSAddr != "" {
if len(address) > 0 {
address += ","
@ -402,6 +404,7 @@ func (s *Server) GetRoutes() []RouterItem {
m[item.Domain].Add(item)
}
}
routerArray := make([]RouterItem, 0, 128)
for _, array := range m {
for _, v := range array.Slice() {

View File

@ -10,6 +10,7 @@ import (
"context"
"fmt"
"reflect"
"runtime"
"strings"
"github.com/gogf/gf/v2/container/glist"
@ -82,6 +83,9 @@ func (s *Server) setHandler(ctx context.Context, in setHandlerInput) {
pattern = in.Pattern
handler = in.HandlerItem
)
if handler.Name == "" {
handler.Name = runtime.FuncForPC(handler.Info.Value.Pointer()).Name()
}
handler.Id = handlerIdGenerator.Add(1)
if handler.Source == "" {
_, file, line := gdebug.CallerWithFilter([]string{utils.StackFilterKeyForGoFrame})

View File

@ -12,7 +12,6 @@ import (
"reflect"
"strings"
"github.com/gogf/gf/v2/debug/gdebug"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/text/gstr"
@ -57,7 +56,6 @@ func (s *Server) doBindHandler(ctx context.Context, in doBindHandlerInput) {
Prefix: in.Prefix,
Pattern: in.Pattern,
HandlerItem: &handlerItem{
Name: gdebug.FuncPath(in.FuncInfo.Func),
Type: HandlerTypeHandler,
Info: in.FuncInfo,
Middleware: in.Middleware,

View File

@ -47,9 +47,12 @@ func Test_Log(t *testing.T) {
t.Assert(client.GetContent(ctx, "/hello"), "hello")
t.Assert(client.GetContent(ctx, "/error"), "custom error")
logPath1 := gfile.Join(logDir, gtime.Now().Format("Y-m-d")+".log")
t.Assert(gstr.Contains(gfile.GetContents(logPath1), "http server started listening on"), true)
t.Assert(gstr.Contains(gfile.GetContents(logPath1), "HANDLER"), true)
var (
logPath1 = gfile.Join(logDir, gtime.Now().Format("Y-m-d")+".log")
content = gfile.GetContents(logPath1)
)
t.Assert(gstr.Contains(content, "http server started listening on"), true)
t.Assert(gstr.Contains(content, "HANDLER"), true)
logPath2 := gfile.Join(logDir, "access-"+gtime.Now().Format("Ymd")+".log")
// fmt.Println(gfile.GetContents(logPath2))