서버간 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

@ -3,24 +3,17 @@ package core
import (
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
"strings"
"time"
"github.com/go-redis/redis/v8"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/primitive"
"repositories.action2quare.com/ayo/gocommon"
"repositories.action2quare.com/ayo/gocommon/logger"
"repositories.action2quare.com/ayo/gocommon/wshandler"
)
func init() {
groupTypeContainer()["chat"] = reflect.TypeOf(&groupChat{})
}
type channelID = string
type channelConfig struct {
Capacity int64 `json:"capacity"`
@ -63,10 +56,6 @@ func (gc *groupChat) Initialize(tv *Tavern, cfg configDocument) error {
gc.rh = tv.redison
tv.apiFuncs.registApiFunction("FetchChattingChannels", gc.FetchChattingChannels)
tv.apiFuncs.registApiFunction("BroadcastMessageOnChannel", gc.BroadcastMessageOnChannel)
tv.apiFuncs.registApiFunction("QueryPlayerChattingChannel", gc.QueryPlayerChattingChannel)
tv.apiFuncs.registApiFunction("SendMessageOnChannel", gc.SendMessageOnChannel)
for name, cfg := range gc.chatConfig.Channels {
if cfg.Capacity == 0 {
cfg.Capacity = gc.chatConfig.DefaultCapacity
@ -223,7 +212,16 @@ func (gc *groupChat) ClientMessageReceived(sender *wshandler.Sender, mt wshandle
}
func (gc *groupChat) FetchChattingChannels(w http.ResponseWriter, r *http.Request) {
prefix, _ := gocommon.ReadStringFormValue(r.URL.Query(), "prefix")
var data struct {
Prefix string `bson:"prefix"`
}
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &data); err != nil {
logger.Println("FetchChattingChannels failed. ReadBsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
prefix := data.Prefix
if len(prefix) == 0 {
logger.Println("FetchChattingChannel failed. prefix is missing")
w.WriteHeader(http.StatusBadRequest)
@ -269,8 +267,18 @@ func (gc *groupChat) FetchChattingChannels(w http.ResponseWriter, r *http.Reques
}
func (gc *groupChat) QueryPlayerChattingChannel(w http.ResponseWriter, r *http.Request) {
accid, _ := gocommon.ReadStringFormValue(r.URL.Query(), "accid")
typename, _ := gocommon.ReadStringFormValue(r.URL.Query(), "typename")
var data struct {
Accid string `bson:"accid"`
Typename string `bson:"typename"`
}
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &data); err != nil {
logger.Println("QueryPlayerChattingChannel failed. ReadBsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
accid := data.Accid
typename := data.Typename
var fields []string
if len(typename) == 0 {
@ -298,60 +306,12 @@ func (gc *groupChat) QueryPlayerChattingChannel(w http.ResponseWriter, r *http.R
}
func (gc *groupChat) SendMessageOnChannel(w http.ResponseWriter, r *http.Request) {
channel, _ := gocommon.ReadStringFormValue(r.URL.Query(), "channel")
target, _ := gocommon.ReadStringFormValue(r.URL.Query(), "target")
tag, _ := gocommon.ReadStringFormValue(r.URL.Query(), "tag")
if len(channel) == 0 || len(target) == 0 || len(tag) == 0 {
logger.Println("SendMessageOnChannel failed. channel or target or tag is empty")
var msg wshandler.UpstreamMessage
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &msg); err != nil {
logger.Println("SendMessageOnChannel failed. ReadBsonDocumentFromBody return err :", err)
w.WriteHeader(http.StatusBadRequest)
return
}
var doc map[string]any
bt, err := io.ReadAll(r.Body)
if err != nil {
logger.Println("SendMessageOnChannel failed. io.ReadAll returns err :", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
if len(bt) > 0 {
if err := bson.Unmarshal(bt, &doc); err != nil {
logger.Println("SendMessageOnChannel failed. decode returns err :", err)
w.WriteHeader(http.StatusBadRequest)
return
}
}
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
Target: fmt.Sprintf("%s@%s", target, channel),
Body: doc,
Tag: []string{tag},
})
}
func (gc *groupChat) BroadcastMessageOnChannel(w http.ResponseWriter, r *http.Request) {
nickname, _ := gocommon.ReadStringFormValue(r.URL.Query(), "nickname")
channel, _ := gocommon.ReadStringFormValue(r.URL.Query(), "channel")
tag, _ := gocommon.ReadStringFormValue(r.URL.Query(), "tag")
text, _ := io.ReadAll(r.Body)
if len(tag) > 0 {
var doc map[string]any
if err := bson.Unmarshal(text, &doc); err == nil {
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
Target: "#" + channel,
Body: doc,
Tag: []string{tag},
})
} else {
logger.Println("BroadcastMessageOnChannel failed :", err)
}
} else {
gc.sendUpstreamMessage(&wshandler.UpstreamMessage{
Target: "#" + channel,
Body: map[string]any{"sender": nickname, "msg": string(text)},
Tag: []string{"TextMessage"},
})
}
gc.sendUpstreamMessage(&msg)
}