forked from s-mang/test2doc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
incorporates some PR changes from s-mang#3. adds customisable handler…
…info function for gorilla/mux, using Walk to find the original http handler
- Loading branch information
Showing
6 changed files
with
104 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package test | ||
|
||
import ( | ||
"github.com/cj123/test2doc/doc/parse" | ||
"github.com/gorilla/mux" | ||
"log" | ||
"net/http" | ||
"reflect" | ||
"runtime" | ||
"strings" | ||
) | ||
|
||
var infoFunc = DefaultHandlerInfo | ||
|
||
type HandlerInfo struct { | ||
FuncName string | ||
} | ||
|
||
type handlerInfoFunc func(r *http.Request) *HandlerInfo | ||
|
||
func RegisterHandlerInfoFunc(fn handlerInfoFunc) { | ||
infoFunc = fn | ||
} | ||
|
||
func DefaultHandlerInfo(r *http.Request) *HandlerInfo { | ||
i := 1 | ||
max := 15 | ||
|
||
var pc uintptr | ||
var fnName string | ||
var ok, fnInPkg, sawPkg bool | ||
|
||
// iterate until we find the top level func in this pkg (the handler) | ||
for i < max { | ||
pc, _, _, ok = runtime.Caller(i) | ||
if !ok { | ||
log.Println("test2doc: DefaultHandlerInfo: !ok") | ||
return nil | ||
} | ||
|
||
fn := runtime.FuncForPC(pc) | ||
fnName = fn.Name() | ||
|
||
fnInPkg = parse.IsFuncInPkg(fnName) | ||
if sawPkg && !fnInPkg { | ||
pc, _, _, ok = runtime.Caller(i - 1) | ||
fn := runtime.FuncForPC(pc) | ||
fnName = fn.Name() | ||
break | ||
} | ||
|
||
sawPkg = fnInPkg | ||
i++ | ||
} | ||
|
||
return &HandlerInfo{ | ||
FuncName: fnName, | ||
} | ||
} | ||
|
||
// GorillaMuxHandlerInfo takes a mux.Router and finds handler info from it. | ||
func GorillaMuxHandlerInfo(router *mux.Router) func(r *http.Request) *HandlerInfo { | ||
return func(r *http.Request) *HandlerInfo { | ||
var handlerInfo *HandlerInfo | ||
|
||
router.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error { | ||
var match mux.RouteMatch | ||
|
||
if route.Match(r, &match) { | ||
fn := runtime.FuncForPC(reflect.ValueOf(match.Handler).Pointer()) | ||
handlerInfo = &HandlerInfo{FuncName: strings.Replace(fn.Name(), "-fm", " ", 1)} | ||
return nil | ||
} | ||
|
||
return nil | ||
}) | ||
|
||
return handlerInfo | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
package test2doc | ||
|
||
|