feat: reflect ServeHTTP now can use parent

This commit is contained in:
langhuihui
2023-12-15 18:10:10 +08:00
parent b486af8484
commit 5e3008f00b

View File

@@ -147,15 +147,18 @@ func (opt *Plugin) registerHandler() {
// 注册http响应 // 注册http响应
for i, j := 0, t.NumMethod(); i < j; i++ { for i, j := 0, t.NumMethod(); i < j; i++ {
name := t.Method(i).Name name := t.Method(i).Name
patten := "/" if name == "ServeHTTP" {
if name != "ServeHTTP" { continue
patten = strings.ToLower(strings.ReplaceAll(name, "_", "/"))
} }
switch handler := v.Method(i).Interface().(type) { switch handler := v.Method(i).Interface().(type) {
case func(http.ResponseWriter, *http.Request): case func(http.ResponseWriter, *http.Request):
patten := strings.ToLower(strings.ReplaceAll(name, "_", "/"))
opt.handle(patten, http.HandlerFunc(handler)) opt.handle(patten, http.HandlerFunc(handler))
} }
} }
if rootHandler, ok := opt.Config.(http.Handler); ok {
opt.handle("/", rootHandler)
}
} }
func (opt *Plugin) settingPath() string { func (opt *Plugin) settingPath() string {