Files
tavern/core/group.go
2023-08-12 15:26:32 +09:00

59 lines
1.3 KiB
Go

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(*subTavern, configDocument) error
ClientMessageReceived(*wshandler.Sender, wshandler.WebSocketMessageType, any)
}