bson을 json으로 통일(일단은)

This commit is contained in:
2023-09-08 15:29:01 +09:00
parent 1d14fb659d
commit 3dde7ccaf5
5 changed files with 29 additions and 79 deletions

View File

@ -315,8 +315,8 @@ func (gc *groupChat) FetchChattingChannels(w http.ResponseWriter, r *http.Reques
var data struct { var data struct {
Prefix string `bson:"prefix"` Prefix string `bson:"prefix"`
} }
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &data); err != nil { if err := gocommon.ReadJsonDocumentFromBody(r.Body, &data); err != nil {
logger.Println("FetchChattingChannels failed. ReadBsonDocumentFromBody returns err :", err) logger.Println("FetchChattingChannels failed. ReadJsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -371,9 +371,9 @@ func (gc *groupChat) QueryPlayerChattingChannel(w http.ResponseWriter, r *http.R
Accid string `bson:"accid"` Accid string `bson:"accid"`
Typename string `bson:"typename"` Typename string `bson:"typename"`
} }
err := gocommon.ReadBsonDocumentFromBody(r.Body, &data) err := gocommon.ReadJsonDocumentFromBody(r.Body, &data)
if err != nil { if err != nil {
logger.Println("QueryPlayerChattingChannel failed. ReadBsonDocumentFromBody returns err :", err) logger.Println("QueryPlayerChattingChannel failed. ReadJsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -392,8 +392,8 @@ func (gc *groupChat) QueryPlayerChattingChannel(w http.ResponseWriter, r *http.R
func (gc *groupChat) SendMessageOnChannel(w http.ResponseWriter, r *http.Request) { func (gc *groupChat) SendMessageOnChannel(w http.ResponseWriter, r *http.Request) {
var msg wshandler.UpstreamMessage var msg wshandler.UpstreamMessage
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &msg); err != nil { if err := gocommon.ReadJsonDocumentFromBody(r.Body, &msg); err != nil {
logger.Println("SendMessageOnChannel failed. ReadBsonDocumentFromBody return err :", err) logger.Println("SendMessageOnChannel failed. ReadJsonDocumentFromBody return err :", err)
w.WriteHeader(http.StatusBadRequest) w.WriteHeader(http.StatusBadRequest)
return return
} }

View File

@ -281,8 +281,8 @@ func (gp *groupParty) JoinParty(w http.ResponseWriter, r *http.Request) {
Character bson.M `bson:"character"` Character bson.M `bson:"character"`
First bool `bson:"first"` First bool `bson:"first"`
} }
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &data); err != nil { if err := gocommon.ReadJsonDocumentFromBody(r.Body, &data); err != nil {
logger.Println("JoinParty failed. ReadBsonDocumentFromBody returns err :", err) logger.Println("JoinParty failed. ReadJsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -329,7 +329,8 @@ func (gp *groupParty) JoinParty(w http.ResponseWriter, r *http.Request) {
Body: gd.loadFull(), Body: gd.loadFull(),
Tag: []string{"GroupDocFull"}, Tag: []string{"GroupDocFull"},
}) })
writeBsonDoc(w, map[string]string{ enc := json.NewEncoder(w)
enc.Encode(map[string]string{
"gid": gid.Hex(), "gid": gid.Hex(),
"tid": gd.tid(mid), "tid": gd.tid(mid),
}) })
@ -374,8 +375,8 @@ func (gp *groupParty) InviteToParty(w http.ResponseWriter, r *http.Request) {
Invitee bson.M `bson:"invitee"` Invitee bson.M `bson:"invitee"`
} }
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &doc); err != nil { if err := gocommon.ReadJsonDocumentFromBody(r.Body, &doc); err != nil {
logger.Println("InviteToParty failed. ReadBsonDocumentFromBody returns err :", err) logger.Println("InviteToParty failed. ReadJsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -456,8 +457,8 @@ func (gp *groupParty) AcceptPartyInvitation(w http.ResponseWriter, r *http.Reque
Tid string `bson:"tid"` Tid string `bson:"tid"`
Character bson.M `bson:"character"` Character bson.M `bson:"character"`
} }
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &doc); err != nil { if err := gocommon.ReadJsonDocumentFromBody(r.Body, &doc); err != nil {
logger.Println("AcceptPartyInvitation failed. ReadBsonDocumentFromBody returns err :", err) logger.Println("AcceptPartyInvitation failed. ReadJsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -546,8 +547,8 @@ func (gp *groupParty) DenyPartyInvitation(w http.ResponseWriter, r *http.Request
Mid primitive.ObjectID `bson:"mid"` Mid primitive.ObjectID `bson:"mid"`
} }
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &data); err != nil { if err := gocommon.ReadJsonDocumentFromBody(r.Body, &data); err != nil {
logger.Println("DenyPartyInvitation failed. ReadBsonDocumentFromBody returns err :", err) logger.Println("DenyPartyInvitation failed. ReadJsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -568,8 +569,8 @@ func (gp *groupParty) QueryPartyMemberState(w http.ResponseWriter, r *http.Reque
Mid primitive.ObjectID `bson:"mid"` Mid primitive.ObjectID `bson:"mid"`
} }
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &data); err != nil { if err := gocommon.ReadJsonDocumentFromBody(r.Body, &data); err != nil {
logger.Println("DenyPartyInvitation failed. ReadBsonDocumentFromBody returns err :", err) logger.Println("DenyPartyInvitation failed. ReadJsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -597,8 +598,8 @@ func (gp *groupParty) LeaveParty(w http.ResponseWriter, r *http.Request) {
Mid primitive.ObjectID `bson:"mid"` Mid primitive.ObjectID `bson:"mid"`
Tid string `bson:"tid"` Tid string `bson:"tid"`
} }
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &data); err != nil { if err := gocommon.ReadJsonDocumentFromBody(r.Body, &data); err != nil {
logger.Println("LeaveParty failed. ReadBsonDocumentFromBody returns err :", err) logger.Println("LeaveParty failed. ReadJsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -711,8 +712,8 @@ func (gp *groupParty) UpdatePartyDocument(w http.ResponseWriter, r *http.Request
Doc bson.M `bson:"doc"` Doc bson.M `bson:"doc"`
} }
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &data); err != nil { if err := gocommon.ReadJsonDocumentFromBody(r.Body, &data); err != nil {
logger.Println("UpdatePartyDocument failed. ReadBsonDocumentFromBody returns err :", err) logger.Println("UpdatePartyDocument failed. ReadJsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -732,8 +733,8 @@ func (gp *groupParty) QueryPartyMembers(w http.ResponseWriter, r *http.Request)
Gid primitive.ObjectID `bson:"gid"` Gid primitive.ObjectID `bson:"gid"`
} }
if err := gocommon.ReadBsonDocumentFromBody(r.Body, &data); err != nil { if err := gocommon.ReadJsonDocumentFromBody(r.Body, &data); err != nil {
logger.Println("QueryPartyMembers failed. ReadBsonDocumentFromBody returns err :", err) logger.Println("QueryPartyMembers failed. ReadJsonDocumentFromBody returns err :", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return
} }
@ -751,7 +752,8 @@ func (gp *groupParty) QueryPartyMembers(w http.ResponseWriter, r *http.Request)
return return
} }
if err := writeBsonDoc(w, members); err != nil { enc := json.NewEncoder(w)
if err := enc.Encode(members); err != nil {
logger.Error("QueryPartyMembers failed. writeBsonDoc return err :", err) logger.Error("QueryPartyMembers failed. writeBsonDoc return err :", err)
w.WriteHeader(http.StatusInternalServerError) w.WriteHeader(http.StatusInternalServerError)
return return

View File

@ -17,25 +17,10 @@ import (
"repositories.action2quare.com/ayo/gocommon/wshandler" "repositories.action2quare.com/ayo/gocommon/wshandler"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/bson/bsonrw"
) )
var devflag = flagx.Bool("dev", false, "") var devflag = flagx.Bool("dev", false, "")
func writeBsonDoc[T any](w io.Writer, src T) error {
rw, err := bsonrw.NewBSONValueWriter(w)
if err != nil {
return err
}
enc, err := bson.NewEncoder(rw)
if err != nil {
return err
}
return enc.Encode(src)
}
type TavernConfig struct { type TavernConfig struct {
session.SessionConfig `json:",inline"` session.SessionConfig `json:",inline"`
Group map[string]configDocument `json:"tavern_group_types"` Group map[string]configDocument `json:"tavern_group_types"`
@ -156,45 +141,6 @@ func (tv *Tavern) ClientDisconnected(ctx wshandler.ApiCallContext) {
logger.Println("ClientDisconnected :", ctx.CallBy.Alias) logger.Println("ClientDisconnected :", ctx.CallBy.Alias)
} }
// func (tv *Tavern) OnClientMessageReceived(sender *wshandler.Sender, messageType wshandler.WebSocketMessageType, body io.Reader) {
// if messageType == wshandler.Connected {
// logger.Println("OnClientMessageReceived : connected ", sender.Accid.Hex())
// tv.redison.Del(tv.redison.Context(), sender.Accid.Hex())
// _, err := tv.redison.JSONSet(sender.Accid.Hex(), "$", bson.M{"_ts": time.Now().UTC().Unix()})
// if err != nil {
// logger.Println("OnClientMessageReceived HSet error :", err)
// }
// } else if messageType == wshandler.Disconnected {
// // TODO : 알려줘야하나???
// var rooms []string
// dec := json.NewDecoder(body)
// if err := dec.Decode(&rooms); err == nil {
// for _, gt := range tv.groups {
// gt.ClientMessageReceived(sender, messageType, rooms)
// }
// }
// tv.redison.Del(tv.redison.Context(), sender.Accid.Hex()).Result()
// logger.Println("OnClientMessageReceived : disconnected ", sender.Accid.Hex())
// } else if messageType == wshandler.BinaryMessage {
// var commandline []any
// dec := json.NewDecoder(body)
// if err := dec.Decode(&commandline); err == nil {
// cmd := commandline[0].(string)
// args := commandline[1:]
// switch cmd {
// case "EnterChannel":
// tv.wsh.EnterRoom(args[0].(string), sender.Accid)
// case "LeaveChannel":
// tv.wsh.LeaveRoom(args[0].(string), sender.Accid)
// }
// }
// }
// }
func (tv *Tavern) OnRoomCreated(name string) { func (tv *Tavern) OnRoomCreated(name string) {
cnt, err := tv.redison.IncrBy(tv.redison.Context(), "_ref_"+name, 1).Result() cnt, err := tv.redison.IncrBy(tv.redison.Context(), "_ref_"+name, 1).Result()
if err != nil && !errors.Is(err, redis.Nil) { if err != nil && !errors.Is(err, redis.Nil) {

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.20
require ( require (
github.com/go-redis/redis/v8 v8.11.5 github.com/go-redis/redis/v8 v8.11.5
go.mongodb.org/mongo-driver v1.11.7 go.mongodb.org/mongo-driver v1.11.7
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908025007-3603c0386b29 repositories.action2quare.com/ayo/gocommon v0.0.0-20230908062630-46ce5f09897a
) )
require ( require (

2
go.sum
View File

@ -110,3 +110,5 @@ repositories.action2quare.com/ayo/gocommon v0.0.0-20230908023557-6cbf32c3868b h1
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908023557-6cbf32c3868b/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY= repositories.action2quare.com/ayo/gocommon v0.0.0-20230908023557-6cbf32c3868b/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908025007-3603c0386b29 h1:Ts40m9MLMMx4uaQWko5QXkg/HX4uYQB9TGGEN6twhiU= repositories.action2quare.com/ayo/gocommon v0.0.0-20230908025007-3603c0386b29 h1:Ts40m9MLMMx4uaQWko5QXkg/HX4uYQB9TGGEN6twhiU=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908025007-3603c0386b29/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY= repositories.action2quare.com/ayo/gocommon v0.0.0-20230908025007-3603c0386b29/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908062630-46ce5f09897a h1:xKUI2xlP6LcUV5fy+4QEHoaZOhkSsMYgeIp6H5ADBCM=
repositories.action2quare.com/ayo/gocommon v0.0.0-20230908062630-46ce5f09897a/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=