서버간 api 호출 간소화
This commit is contained in:
@ -4,11 +4,9 @@ import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@ -39,29 +37,6 @@ func writeBsonDoc[T any](w io.Writer, src T) error {
|
||||
return enc.Encode(src)
|
||||
}
|
||||
|
||||
func readBsonDoc(r io.Reader, src any) error {
|
||||
body, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(body) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
decoder, err := bson.NewDecoder(bsonrw.NewBSONDocumentReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = decoder.Decode(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type TavernConfig struct {
|
||||
session.SessionConfig `json:",inline"`
|
||||
gocommon.StorageAddr `json:"storage"`
|
||||
@ -75,11 +50,11 @@ type TavernConfig struct {
|
||||
var config TavernConfig
|
||||
|
||||
type Tavern struct {
|
||||
wsh *wshandler.WebsocketHandler
|
||||
mongoClient gocommon.MongoClient
|
||||
redison *gocommon.RedisonHandler
|
||||
groups map[string]group
|
||||
apiFuncs *apiFuncsContainer
|
||||
wsh *wshandler.WebsocketHandler
|
||||
mongoClient gocommon.MongoClient
|
||||
redison *gocommon.RedisonHandler
|
||||
groups map[string]group
|
||||
apiReceivers gocommon.HttpApiHandlerContainer
|
||||
}
|
||||
|
||||
func getMacAddr() (string, error) {
|
||||
@ -116,10 +91,6 @@ func New(context context.Context, wsh *wshandler.WebsocketHandler, inconfig *Tav
|
||||
config.macAddr = macaddr
|
||||
tv := &Tavern{
|
||||
wsh: wsh,
|
||||
apiFuncs: &apiFuncsContainer{
|
||||
normfuncs: make(map[string]apiFuncType),
|
||||
funcs: make(map[string][]apiFuncType),
|
||||
},
|
||||
}
|
||||
|
||||
if err = tv.prepare(context); err != nil {
|
||||
@ -143,25 +114,24 @@ func (tv *Tavern) prepare(ctx context.Context) error {
|
||||
tv.redison = gocommon.NewRedisonHandler(redisClient.Context(), redisClient)
|
||||
|
||||
groups := make(map[string]group)
|
||||
for typename, cfg := range config.Group {
|
||||
gtype, ok := groupTypeContainer()[typename]
|
||||
if !ok {
|
||||
return fmt.Errorf("%s group type is not valid", typename)
|
||||
}
|
||||
|
||||
if !gtype.Implements(reflect.TypeOf((*group)(nil)).Elem()) {
|
||||
return fmt.Errorf("%s is not implement proper interface", typename)
|
||||
}
|
||||
ptrvalue := reflect.New(gtype.Elem())
|
||||
instance := ptrvalue.Interface().(group)
|
||||
if err := instance.Initialize(tv, cfg); err != nil {
|
||||
if cfg, ok := config.Group["chat"]; ok {
|
||||
chat := new(groupChat)
|
||||
if err := chat.Initialize(tv, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
groups[typename] = instance
|
||||
tv.apiReceivers.RegistReceiver(gocommon.MakeHttpApiReceiver(chat, "chat"))
|
||||
groups["chat"] = chat
|
||||
}
|
||||
|
||||
if cfg, ok := config.Group["party"]; ok {
|
||||
party := new(groupParty)
|
||||
if err := party.Initialize(tv, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
tv.apiReceivers.RegistReceiver(gocommon.MakeHttpApiReceiver(party, "party"))
|
||||
groups["party"] = party
|
||||
}
|
||||
tv.groups = groups
|
||||
tv.apiFuncs.normalize()
|
||||
|
||||
return nil
|
||||
}
|
||||
@ -259,11 +229,12 @@ func (tv *Tavern) api(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
operation := r.URL.Query().Get("operation")
|
||||
if len(operation) == 0 {
|
||||
funcname := r.URL.Query().Get("call")
|
||||
if len(funcname) == 0 {
|
||||
logger.Println("query param 'call' is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
tv.apiFuncs.call(operation, w, r)
|
||||
tv.apiReceivers.Call(funcname, w, r)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user