MakeHttpApiReceiverByValue 제거

This commit is contained in:
2023-09-05 12:28:25 +09:00
parent def9494dd3
commit 8877bf88c0

View File

@ -613,49 +613,6 @@ type HttpApiReceiver struct {
methods map[string]apiFuncType
}
func MakeHttpApiReceiverByValue(receiver reflect.Value) HttpApiReceiver {
methods := make(map[string]apiFuncType)
tp := receiver.Type()
name := tp.Elem().Name()
for i := 0; i < tp.NumMethod(); i++ {
method := tp.Method(i)
if method.Type.NumIn() != 3 {
continue
}
if method.Type.In(0) != tp {
continue
}
var w http.ResponseWriter
if method.Type.In(1) != reflect.TypeOf(w) {
continue
}
var r http.Request
if method.Type.In(2) != reflect.TypeOf(&r) {
continue
}
if method.Name == "ServeHTTP" {
continue
}
funcptr := method.Func.Pointer()
p1 := unsafe.Pointer(&funcptr)
p2 := unsafe.Pointer(&p1)
testfunc := (*func(*T, http.ResponseWriter, *http.Request))(p2)
methods[name+"."+method.Name] = func(w http.ResponseWriter, r *http.Request) {
(*testfunc)(receiver.Interface(), w, r)
}
}
return HttpApiReceiver{
methods: methods,
}
}
func MakeHttpApiReceiver[T any](receiver *T) HttpApiReceiver {
methods := make(map[string]apiFuncType)
@ -714,8 +671,12 @@ func (hc *HttpApiHandlerContainer) RegistReceiver(receiver HttpApiReceiver) {
}
}
func (hc *HttpApiHandlerContainer) HandlerFunc(serveMux *http.ServeMux, prefix string) {
for k, v := range hc.methods {
serveMux.HandleFunc(MakeHttpHandlerPattern(prefix, k), v)
func (hc *HttpApiHandlerContainer) Call(w http.ResponseWriter, r *http.Request) {
lastSlash := strings.LastIndex(r.URL.Path, "/")
fname := r.URL.Path[lastSlash:]
if found := hc.methods[fname]; found != nil {
found(w, r)
} else {
logger.Println("api is not found :", fname)
}
}