서버간 api 호출 간소화

This commit is contained in:
2023-09-05 17:14:07 +09:00
parent 6410056c87
commit 4a51f7d433
6 changed files with 189 additions and 287 deletions

View File

@ -1,56 +1,9 @@
package core
import (
"net/http"
"reflect"
"repositories.action2quare.com/ayo/gocommon/logger"
"repositories.action2quare.com/ayo/gocommon/wshandler"
)
var groupTypes map[string]reflect.Type
func groupTypeContainer() map[string]reflect.Type {
if groupTypes == nil {
groupTypes = make(map[string]reflect.Type)
}
return groupTypes
}
type apiFuncType func(http.ResponseWriter, *http.Request)
type apiFuncsContainer struct {
normfuncs map[string]apiFuncType
funcs map[string][]apiFuncType
}
func (afc *apiFuncsContainer) registApiFunction(name string, f apiFuncType) {
afc.funcs[name] = append(afc.funcs[name], f)
}
func (afc *apiFuncsContainer) normalize() {
for k, v := range afc.funcs {
if len(v) == 1 {
afc.normfuncs[k] = v[0]
} else {
afc.normfuncs[k] = func(w http.ResponseWriter, r *http.Request) {
for _, f := range v {
f(w, r)
}
}
}
}
afc.funcs = nil
}
func (afc *apiFuncsContainer) call(fn string, w http.ResponseWriter, r *http.Request) {
f := afc.normfuncs[fn]
if f != nil {
f(w, r)
} else {
logger.Println("api func is missing :", fn)
}
}
type configDocument map[string]any
type group interface {
Initialize(*Tavern, configDocument) error