Compare commits
11 Commits
e18a5eafe0
...
toredis
| Author | SHA1 | Date | |
|---|---|---|---|
| 8e1b232d57 | |||
| 01da5bb3a4 | |||
| ba61a11659 | |||
| 67cca13326 | |||
| 272c696c59 | |||
| aa568ec3fa | |||
| b9c4d8b21b | |||
| 99834c1461 | |||
| 592112219e | |||
| 62485b6d54 | |||
| d36dd13bb7 |
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,3 @@
|
||||
.vscode/
|
||||
__debug_bin.exe
|
||||
*.log
|
||||
config.json
|
||||
|
||||
1
config.json
Normal file
1
config.json
Normal file
@ -0,0 +1 @@
|
||||
{}
|
||||
@ -5,42 +5,68 @@
|
||||
"redis": {
|
||||
"cache": "redis://192.168.8.94:6380/0",
|
||||
"session": "redis://192.168.8.94:6380/1",
|
||||
"tx": "redis://192.168.8.94:6380/2",
|
||||
"tavern": "redis://192.168.8.94:6380/3"
|
||||
"ranking": "redis://192.168.8.94:6380/2",
|
||||
"wshandler": "redis://192.168.8.94:6380/3",
|
||||
"tavern": "redis://192.168.8.94:6380/4"
|
||||
}
|
||||
},
|
||||
"dev": {
|
||||
"mongo": "mongodb://192.168.8.94:27017/?replicaSet=repl01&retrywrites=false",
|
||||
"redis": {
|
||||
"cache": "redis://192.168.8.94:6380/4",
|
||||
"session": "redis://192.168.8.94:6380/5",
|
||||
"ranking": "redis://192.168.8.94:6380/6",
|
||||
"wshandler": "redis://192.168.8.94:6380/7",
|
||||
"tavern": "redis://192.168.8.94:6380/8"
|
||||
}
|
||||
}
|
||||
},
|
||||
"maingate_session_storage": "redis://192.168.8.94:6380/1",
|
||||
"maingate_session_ttl" : 3600,
|
||||
"maingate_mongodb_url": "mongodb://192.168.8.94:27017/?replicaSet=repl01&retrywrites=false",
|
||||
"maingate_service_url": "http://localhost/maingate",
|
||||
"maingate_api_token": "63d08aa34f0162622c11284b",
|
||||
|
||||
"social_redis_url": "redis://192.168.8.94:6380/4",
|
||||
"social_storage_url" : "mongodb://192.168.8.94:27017/social?replicaSet=repl01&retrywrites=false",
|
||||
|
||||
"tavern_service_url": "http://localhost/tavern",
|
||||
"tavern_group_types": {
|
||||
"party": {
|
||||
"max_member": 3,
|
||||
"invite_ttl": 30
|
||||
"subjugate": {
|
||||
"text_search_field": [
|
||||
"name"
|
||||
],
|
||||
"unique_index": [
|
||||
"name,_id",
|
||||
"_id,members",
|
||||
"name,hidden"
|
||||
],
|
||||
"search_index": [
|
||||
"rules"
|
||||
],
|
||||
"member_index": [
|
||||
"_gid,candidate,luts",
|
||||
"_gid,luts",
|
||||
"_gid,expiring"
|
||||
],
|
||||
"invite_ttl": 30,
|
||||
"candidate_ttl": 3600,
|
||||
"invitee_exlusive": true,
|
||||
"invitee_is_member": true,
|
||||
"max_member": 4
|
||||
},
|
||||
"chat" : {
|
||||
"default_capacity" : 1000,
|
||||
"channels" : {
|
||||
"bazzar-1" : {
|
||||
"name" : "FText(bazzar-1)"
|
||||
},
|
||||
"bazzar-2" : {
|
||||
"name" : "FText(bazzar-2)"
|
||||
},
|
||||
"bazzar-3" : {
|
||||
"name" : "FText(bazzar-3)"
|
||||
},
|
||||
"bazzar-4" : {
|
||||
"name" : "FText(bazzar-4)"
|
||||
},
|
||||
"bazzar-5" : {
|
||||
"name" : "FText(bazzar-5)"
|
||||
}
|
||||
"lobby": {
|
||||
"max_member": 3,
|
||||
"invitee_exlusive": true,
|
||||
"invitee_is_member": true,
|
||||
"transient": true,
|
||||
"invite_ttl": 30
|
||||
}
|
||||
},
|
||||
|
||||
"services": {
|
||||
"kingdom": {
|
||||
"개발중": {
|
||||
"url": "http://localhost/warehouse/dev",
|
||||
"development": true
|
||||
},
|
||||
"개인서버": {
|
||||
"url": "http://localhost/warehouse/private",
|
||||
"development": false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
526
core/apiimpl.go
Normal file
526
core/apiimpl.go
Normal file
@ -0,0 +1,526 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
common "repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
// CreateGroup : 그룹 생성
|
||||
// - 그룹 : 멤버와 권한을 관리할 수 있다. 그룹 타입에 따라 디비에 저장되거나 메모리에만 존재한다.
|
||||
// - 생성 요청이 오면 파티를 만든다. 파티을 만들 수 있는지 여부는 서비스에서 결정할 것이고, 이 요청을 호출했다는 것은 서비스가 결정한 그룹 생성 조건을 다 통과했다는 의미이다.
|
||||
// - parameter :
|
||||
// - type : 그룹 종류. 그룹 종류에 따라 인덱스와 쿼리 가능 field가 다르다.
|
||||
func (sub *subTavern) CreateGroup(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
grouptype := sub.groups[typename]
|
||||
if grouptype == nil {
|
||||
logger.Println("CreateGroup failed. group type is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
doc := bson.M{}
|
||||
if err := readBsonDoc(r.Body, &doc); err != nil {
|
||||
logger.Error("CreateGroup failed. readBsonDoc returns err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
inserted, err := grouptype.Create(r.Form, doc)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write(inserted[:])
|
||||
}
|
||||
|
||||
// JoinGroup : 그룹에 참가
|
||||
// - type : 그룹 타입
|
||||
// - 그룹 타입에 맞는 키(주로 _id)
|
||||
// - member_id : 참가 멤버의 아이디
|
||||
// - body : 멤버의 속성 bson document
|
||||
func (sub *subTavern) JoinGroup(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("JoinGroup failed. group type is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
doc := bson.M{}
|
||||
if err := readBsonDoc(r.Body, &doc); err != nil {
|
||||
logger.Error("JoinGroup failed. readBsonDoc returns err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
gidobj, ok := common.ReadObjectIDFormValue(r.Form, "gid")
|
||||
if !ok {
|
||||
logger.Println("JoinGroup failed. gid is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
midobj, midok := common.ReadObjectIDFormValue(r.Form, "mid")
|
||||
tidobj, tidok := common.ReadObjectIDFormValue(r.Form, "tid")
|
||||
if !midok && !tidok {
|
||||
// 둘다 없네?
|
||||
logger.Println("JoinGroup failed. tid or mid should be exist")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
if candidate, ok := common.ReadBoolFormValue(r.Form, "candidate"); ok && candidate {
|
||||
err = group.Candidate(gidobj, midobj, doc)
|
||||
} else {
|
||||
err = group.Join(gidobj, midobj, doc)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
writeBsonDoc(w, map[string]string{
|
||||
"gid": gidobj.Hex(),
|
||||
"tid": tidobj.Hex(),
|
||||
})
|
||||
} else if err == errGroupNotExist {
|
||||
w.Write([]byte("{}"))
|
||||
} else if err != nil {
|
||||
logger.Error("JoinGroup failed :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
// Invite : 초대
|
||||
// - type : 초대 타입 (required)
|
||||
// - from : 초대하는 자 (required)
|
||||
// - to : 초대받는 자 (required)
|
||||
// - timeout : 초대 유지시간(optional. 없으면 config 기본 값)
|
||||
// - (body) : 검색시 노출되는 document
|
||||
func (sub *subTavern) Invite(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("Invite failed. group type is missing :", r)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
gid, ok := common.ReadObjectIDFormValue(r.Form, "gid")
|
||||
if !ok {
|
||||
logger.Println("Invite failed. gid is missing :", r)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
mid, ok := common.ReadObjectIDFormValue(r.Form, "mid")
|
||||
if !ok {
|
||||
logger.Println("Invite failed. mid is missing :", r)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var reqdoc struct {
|
||||
Inviter bson.M `bson:"inviter"`
|
||||
Invitee bson.M `bson:"invitee"`
|
||||
}
|
||||
if err := readBsonDoc(r.Body, &reqdoc); err != nil {
|
||||
logger.Println("Invite failed. readBsonDoc returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := group.Invite(gid, mid, reqdoc.Inviter, reqdoc.Invitee)
|
||||
if err != nil {
|
||||
logger.Println("Invite failed. group.Invite returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Write([]byte(result))
|
||||
}
|
||||
|
||||
func (sub *subTavern) CancelInvitation(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("CancelInvitation failed. group type is missing :", r)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
tid, ok := common.ReadObjectIDFormValue(r.Form, "tid")
|
||||
if !ok {
|
||||
logger.Println("CancelInvitation failed. form value 'tid' is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
gid, ok := common.ReadObjectIDFormValue(r.Form, "gid")
|
||||
if !ok {
|
||||
logger.Println("CancelInvitation failed. form value 'gid' is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := group.CancelInvitation(gid, tid); err != nil {
|
||||
logger.Println("CancelInvitation failed. group.CancelInvitation returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) AcceptInvitation(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("CancelInvitation failed. group type is missing :", r)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
gid, _ := common.ReadObjectIDFormValue(r.Form, "gid")
|
||||
mid, _ := common.ReadObjectIDFormValue(r.Form, "mid")
|
||||
|
||||
var member bson.M
|
||||
if err := readBsonDoc(r.Body, &member); err != nil {
|
||||
logger.Error("AcceptInvitation failed. readBsonDoc returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err := group.AcceptInvitation(gid, mid, member)
|
||||
if err != nil {
|
||||
logger.Println("AcceptInvitation failed. group.AcceptInvitation returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) DenyInvitation(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("DenyInvitation failed. group type is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
gid, _ := common.ReadObjectIDFormValue(r.Form, "gid")
|
||||
mid, _ := common.ReadObjectIDFormValue(r.Form, "mid")
|
||||
tid, ok := common.ReadObjectIDFormValue(r.Form, "tid")
|
||||
if !ok {
|
||||
logger.Println("DenyInvitation failed. tid is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
err := group.DenyInvitation(gid, mid, tid)
|
||||
if err != nil {
|
||||
logger.Error("DenyInvitation failed. group.DenyInvitation returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) QueryInvitations(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("QueryInvitations failed. group type is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
mid, ok := common.ReadObjectIDFormValue(r.Form, "mid")
|
||||
if !ok {
|
||||
logger.Println("QueryInvitations failed. mid is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var after primitive.Timestamp
|
||||
if v, ok := common.ReadStringFormValue(r.Form, "after"); ok && v != "0.0" {
|
||||
after = common.DotStringToTimestamp(v)
|
||||
}
|
||||
|
||||
result, err := group.QueryInvitations(mid, after)
|
||||
|
||||
if err != nil {
|
||||
logger.Println("QueryInvitations failed. group.QueryInvitations returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if err := writeBsonArr(w, result); err != nil {
|
||||
logger.Println("QueryInvitations failed. Encode returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) SearchGroup(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("SearchGroup failed. group type is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
projection, _ := common.ReadStringFormValue(r.Form, "projection")
|
||||
|
||||
var filter bson.M
|
||||
if err := readBsonDoc(r.Body, &filter); err != nil {
|
||||
logger.Error("SearchGroup failed. readBsonDoc returns err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := group.FindAll(filter, projection, primitive.Timestamp{})
|
||||
if err != nil {
|
||||
logger.Error("SearchGroup failed. FindAll err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if result == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if err := writeBsonArr(w, result); err != nil {
|
||||
logger.Error("bson marshal failed :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) QueryOnlineState(w http.ResponseWriter, r *http.Request) {
|
||||
mid, ok := common.ReadObjectIDFormValue(r.Form, "mid")
|
||||
if !ok {
|
||||
logger.Println("IsOnline failed. mid is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
state := sub.wsh.GetState(sub.region, mid)
|
||||
w.Write([]byte(state))
|
||||
}
|
||||
|
||||
func (sub *subTavern) IsOnline(w http.ResponseWriter, r *http.Request) {
|
||||
mid, ok := common.ReadObjectIDFormValue(r.Form, "mid")
|
||||
if !ok {
|
||||
logger.Println("IsOnline failed. mid is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if state := sub.wsh.GetState(sub.region, mid); len(state) > 0 {
|
||||
w.Write([]byte("true"))
|
||||
} else {
|
||||
w.Write([]byte("false"))
|
||||
}
|
||||
}
|
||||
|
||||
// QueryGroup : 그룹조회
|
||||
// - type : 그룹 타입
|
||||
// - 그룹 타입에 맞는 키(주로 _id)
|
||||
// - projection : select할 필드. ,로 구분
|
||||
func (sub *subTavern) QueryGroup(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("QueryGroup failed. group type is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
gid, ok := common.ReadObjectIDFormValue(r.Form, "_id")
|
||||
if !ok {
|
||||
logger.Println("QueryGroup failed. _id is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
projection, _ := common.ReadStringFormValue(r.Form, "projection")
|
||||
|
||||
after, _ := common.ReadStringFormValue(r.Form, "after")
|
||||
if after != "0.0" {
|
||||
projection += ",+luts"
|
||||
}
|
||||
|
||||
result, err := group.FindOne(gid, projection)
|
||||
if err != nil {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if result == nil {
|
||||
return
|
||||
}
|
||||
if len(after) > 0 {
|
||||
if luts, ok := result["luts"].(primitive.Timestamp); ok {
|
||||
afterts := common.DotStringToTimestamp(after)
|
||||
if primitive.CompareTimestamp(luts, afterts) < 0 {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if err := writeBsonDoc(w, result); err != nil {
|
||||
logger.Error("bson marshal failed :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// LeaveGroup : 그룹에서 나감 or 내보냄
|
||||
// - type : 그룹 타입
|
||||
// - 그룹 타입에 맞는 키(주로 _id)
|
||||
// - member_id : 나갈 멤버의 아이디
|
||||
func (sub *subTavern) LeaveGroup(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("LeaveGroup failed. group type is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
gid, ok := common.ReadObjectIDFormValue(r.Form, "gid")
|
||||
if !ok {
|
||||
logger.Println("LeaveGroup failed. gid is missing :", r.Form)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
mid, midok := common.ReadObjectIDFormValue(r.Form, "mid")
|
||||
|
||||
if !midok {
|
||||
logger.Println("LeaveGroup failed. mid is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := group.Leave(gid, mid); err != nil {
|
||||
// 둘 중 하나는 있어야지
|
||||
logger.Println("LeaveGroup failed. group.Leave returns err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) UpdateMemberDocument(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("DismissGroup failed. type is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
midobj, ok := common.ReadObjectIDFormValue(r.Form, "mid")
|
||||
if !ok {
|
||||
logger.Println("UpdateMemberDocument failed. member_id is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
gidobj, ok := common.ReadObjectIDFormValue(r.Form, "gid")
|
||||
if !ok {
|
||||
logger.Println("UpdateMemberDocument failed. _id is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var updatedoc bson.M
|
||||
if err := readBsonDoc(r.Body, &updatedoc); err != nil {
|
||||
logger.Error("UpdateMemberDocument failed. body decoding error :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := group.UpdateMemberDocument(gidobj, midobj, updatedoc); err != nil {
|
||||
logger.Println("UpdateMemberDocument failed :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) DismissGroup(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("DismissGroup failed. type is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
gid, ok := common.ReadObjectIDFormValue(r.Form, "gid")
|
||||
if !ok {
|
||||
logger.Println("DismissGroup failed. gid is missing :")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := group.Dismiss(gid); err != nil {
|
||||
logger.Error("DismissGroup failed. group.Dismiss returns err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) UpdateGroupDocument(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("UpdateGroupDocument failed. type is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
gid, ok := common.ReadObjectIDFormValue(r.Form, "gid")
|
||||
if !ok {
|
||||
logger.Println("UpdateGroupDocument failed. gid is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var frag bson.M
|
||||
if err := readBsonDoc(r.Body, &frag); err != nil {
|
||||
logger.Error("UpdateGroupDocument failed. readBsonDoc err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := group.UpdateGroupDocument(gid, frag); err != nil {
|
||||
logger.Error("UpdateGroupDocument failed. group.UpdateGroupDocument returns err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) QueryGroupMembers(w http.ResponseWriter, r *http.Request) {
|
||||
typename, _ := common.ReadStringFormValue(r.Form, "type")
|
||||
group := sub.groups[typename]
|
||||
if group == nil {
|
||||
logger.Println("QueryGroupMembers failed. type is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
gid, ok := common.ReadObjectIDFormValue(r.Form, "gid")
|
||||
if !ok {
|
||||
logger.Println("QueryGroupMembers failed. gid is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
members, err := group.QueryGroupMembers(gid)
|
||||
if err != nil {
|
||||
logger.Error("QueryGroupMembers failed. group.QueryGroupMembers returns err :", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if err := writeBsonDoc(w, members); err != nil {
|
||||
logger.Error("QueryGroupMembers failed. writeBsonDoc return err :", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
1
core/config.json
Normal file
1
core/config.json
Normal file
@ -0,0 +1 @@
|
||||
{}
|
||||
491
core/friend.go
491
core/friend.go
@ -1,491 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
||||
)
|
||||
|
||||
const (
|
||||
friends_collection_name = gocommon.CollectionName("friends")
|
||||
monitoring_center_count = 100
|
||||
state_online = "online"
|
||||
state_offline = "offline"
|
||||
)
|
||||
|
||||
var friend_state_tag = []string{"social.FriendState"}
|
||||
|
||||
type friendDoc struct {
|
||||
Id primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
|
||||
From primitive.ObjectID `bson:"from" json:"-"`
|
||||
To primitive.ObjectID `bson:"to" json:"-"`
|
||||
ToAlias string `bson:"talias" json:"to"`
|
||||
Timestamp int64 `bson:"ts" json:"ts"`
|
||||
Deleted bool `bson:"deleted,omitempty" json:"deleted,omitempty"`
|
||||
}
|
||||
|
||||
type registerListener struct {
|
||||
src primitive.ObjectID
|
||||
alias string
|
||||
l *listener
|
||||
}
|
||||
|
||||
type monitoringCenter struct {
|
||||
regChan chan registerListener
|
||||
publishState func(string, string, string)
|
||||
}
|
||||
|
||||
type connWithFriends struct {
|
||||
c *websocket.Conn
|
||||
friends []*friendDoc
|
||||
initialized bool
|
||||
}
|
||||
|
||||
type connections struct {
|
||||
connLock sync.Mutex
|
||||
conns map[primitive.ObjectID]*connWithFriends
|
||||
}
|
||||
|
||||
func (cs *connections) new(accid primitive.ObjectID, conn *websocket.Conn) {
|
||||
cs.connLock.Lock()
|
||||
defer cs.connLock.Unlock()
|
||||
cs.conns[accid] = &connWithFriends{
|
||||
c: conn,
|
||||
initialized: false,
|
||||
}
|
||||
}
|
||||
|
||||
func (cs *connections) delete(accid primitive.ObjectID) {
|
||||
cs.connLock.Lock()
|
||||
defer cs.connLock.Unlock()
|
||||
|
||||
delete(cs.conns, accid)
|
||||
}
|
||||
|
||||
func (cs *connections) conn(accid primitive.ObjectID) *websocket.Conn {
|
||||
cs.connLock.Lock()
|
||||
defer cs.connLock.Unlock()
|
||||
if cf, ok := cs.conns[accid]; ok {
|
||||
return cf.c
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cs *connections) addFriend(accid primitive.ObjectID, fdoc *friendDoc) bool {
|
||||
cs.connLock.Lock()
|
||||
defer cs.connLock.Unlock()
|
||||
if cf, ok := cs.conns[accid]; ok {
|
||||
if cf.initialized {
|
||||
cf.friends = append(cf.friends, fdoc)
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (cs *connections) initFriends(accid primitive.ObjectID, fdocs []*friendDoc) {
|
||||
cs.connLock.Lock()
|
||||
defer cs.connLock.Unlock()
|
||||
if cf, ok := cs.conns[accid]; ok {
|
||||
cf.friends = fdocs
|
||||
cf.initialized = true
|
||||
}
|
||||
}
|
||||
|
||||
func (cs *connections) clearFriends(accid primitive.ObjectID) (out []*friendDoc) {
|
||||
cs.connLock.Lock()
|
||||
defer cs.connLock.Unlock()
|
||||
if cf, ok := cs.conns[accid]; ok {
|
||||
out = cf.friends
|
||||
cf.friends = nil
|
||||
cf.initialized = false
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type friends struct {
|
||||
mongoClient gocommon.MongoClient
|
||||
redison *gocommon.RedisonHandler
|
||||
wsh *wshandler.WebsocketHandler
|
||||
moncen []monitoringCenter
|
||||
conns connections
|
||||
}
|
||||
|
||||
type listener struct {
|
||||
c *websocket.Conn
|
||||
me primitive.ObjectID
|
||||
}
|
||||
|
||||
type listenerMap struct {
|
||||
listeners map[primitive.ObjectID]*listener
|
||||
connected bool
|
||||
online []byte
|
||||
offline []byte
|
||||
}
|
||||
|
||||
func init() {
|
||||
gob.Register([]friendDoc{})
|
||||
}
|
||||
|
||||
// per channel
|
||||
// src(alias) - listener(objectid) : socket
|
||||
// - listener(objectid) : socket
|
||||
// - listener(objectid) : socket
|
||||
|
||||
func makeSrcMap(src string, connected bool) *listenerMap {
|
||||
online, _ := json.Marshal(wshandler.DownstreamMessage{
|
||||
Body: bson.M{
|
||||
"from": src,
|
||||
"state": state_online,
|
||||
},
|
||||
Tag: friend_state_tag,
|
||||
})
|
||||
|
||||
offline, _ := json.Marshal(wshandler.DownstreamMessage{
|
||||
Body: bson.M{
|
||||
"from": src,
|
||||
"state": state_offline,
|
||||
},
|
||||
Tag: friend_state_tag,
|
||||
})
|
||||
|
||||
return &listenerMap{
|
||||
listeners: make(map[primitive.ObjectID]*listener),
|
||||
connected: connected,
|
||||
online: online,
|
||||
offline: offline,
|
||||
}
|
||||
}
|
||||
|
||||
func makeFriends(ctx context.Context, so *Social) (*friends, error) {
|
||||
if err := so.mongoClient.MakeUniqueIndices(friends_collection_name, map[string]bson.D{
|
||||
"fromto": {{Key: "from", Value: 1}, {Key: "to", Value: 1}},
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var moncen []monitoringCenter
|
||||
for i := 0; i < monitoring_center_count; i++ {
|
||||
subChannel := fmt.Sprintf("_soc_fr_monitor_ch_%d_%d", i, so.redison.Options().DB)
|
||||
regChan := make(chan registerListener)
|
||||
moncen = append(moncen, monitoringCenter{
|
||||
regChan: regChan,
|
||||
publishState: func(src, alias, state string) {
|
||||
so.redison.Publish(ctx, subChannel, src+alias+":"+state).Result()
|
||||
},
|
||||
})
|
||||
|
||||
go func(subChannel string, regChan chan registerListener) {
|
||||
pubsub := so.redison.Subscribe(ctx, subChannel)
|
||||
listeners := make(map[primitive.ObjectID]*listenerMap)
|
||||
for {
|
||||
select {
|
||||
case reg := <-regChan:
|
||||
// 내가 관심있는 애들 등록
|
||||
srcmap, online := listeners[reg.src]
|
||||
if !online {
|
||||
srcmap = makeSrcMap(reg.alias, false)
|
||||
listeners[reg.src] = srcmap
|
||||
}
|
||||
|
||||
if reg.l.c == nil {
|
||||
// 등록 해제. 모니터링 종료
|
||||
// listener목록에서 나(reg.l.me)를 제거
|
||||
delete(srcmap.listeners, reg.l.me)
|
||||
online = false
|
||||
logger.Println("regChan unregistered :", reg.src.Hex(), reg.l.me.Hex())
|
||||
} else if oldl, ok := srcmap.listeners[reg.l.me]; ok {
|
||||
// 내가 이미 리스너로 등록되어 있다.
|
||||
// 상대방이 나를 차단했을 경우에는 기존 리스너가 nil임
|
||||
online = oldl != nil
|
||||
logger.Println("regChan registered :", reg.src.Hex(), reg.l.me.Hex(), "old", online)
|
||||
} else {
|
||||
logger.Println("regChan registered :", reg.src.Hex(), reg.l.me.Hex())
|
||||
srcmap.listeners[reg.l.me] = reg.l
|
||||
}
|
||||
|
||||
if online && srcmap != nil {
|
||||
logger.Println("regChan send online :", reg.l.me.Hex(), string(srcmap.online))
|
||||
reg.l.c.WriteMessage(websocket.TextMessage, srcmap.online)
|
||||
}
|
||||
|
||||
if len(srcmap.listeners) == 0 && !srcmap.connected {
|
||||
delete(listeners, reg.src)
|
||||
}
|
||||
|
||||
case msg := <-pubsub.Channel():
|
||||
target, _ := primitive.ObjectIDFromHex(msg.Payload[:24])
|
||||
aliasstate := strings.SplitN(msg.Payload[24:], ":", 2)
|
||||
var sent []byte
|
||||
if srcmap, ok := listeners[target]; ok {
|
||||
if aliasstate[1] == state_online {
|
||||
sent = srcmap.online
|
||||
srcmap.connected = true
|
||||
} else if aliasstate[1] == state_offline {
|
||||
sent = srcmap.offline
|
||||
srcmap.connected = false
|
||||
if len(srcmap.listeners) == 0 {
|
||||
delete(listeners, target)
|
||||
}
|
||||
}
|
||||
|
||||
if len(sent) > 0 {
|
||||
for _, l := range srcmap.listeners {
|
||||
logger.Println("state fire :", l.me, string(sent))
|
||||
l.c.WriteMessage(websocket.TextMessage, sent)
|
||||
}
|
||||
}
|
||||
} else if aliasstate[1] == state_online {
|
||||
listeners[target] = makeSrcMap(aliasstate[0], true)
|
||||
}
|
||||
}
|
||||
}
|
||||
}(subChannel, regChan)
|
||||
}
|
||||
|
||||
return &friends{
|
||||
mongoClient: so.mongoClient,
|
||||
redison: so.redison,
|
||||
wsh: so.wsh,
|
||||
moncen: moncen,
|
||||
conns: connections{
|
||||
conns: make(map[primitive.ObjectID]*connWithFriends),
|
||||
},
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (fs *friends) ClientConnected(conn *websocket.Conn, callby *wshandler.Sender) {
|
||||
fs.conns.new(callby.Accid, conn)
|
||||
|
||||
// 내 로그인 상태를 알림
|
||||
meidx := callby.Accid[11] % monitoring_center_count
|
||||
fs.moncen[meidx].publishState(callby.Accid.Hex(), callby.Alias, state_online)
|
||||
}
|
||||
|
||||
func (fs *friends) ClientDisconnected(conn *websocket.Conn, callby *wshandler.Sender) {
|
||||
// 로그 오프 상태를 알림
|
||||
meidx := callby.Accid[11] % monitoring_center_count
|
||||
fs.moncen[meidx].publishState(callby.Accid.Hex(), callby.Alias, state_offline)
|
||||
|
||||
fs.stopMonitoringFriends(callby.Accid)
|
||||
|
||||
fs.conns.delete(callby.Accid)
|
||||
}
|
||||
|
||||
func (fs *friends) writeMessage(acc primitive.ObjectID, src any) {
|
||||
c := fs.conns.conn(acc)
|
||||
if c == nil {
|
||||
return
|
||||
}
|
||||
|
||||
if bt, err := json.Marshal(src); err == nil {
|
||||
c.WriteMessage(websocket.TextMessage, bt)
|
||||
}
|
||||
}
|
||||
|
||||
var errAddFriendFailed = errors.New("addFriend failed")
|
||||
|
||||
func (fs *friends) addFriend(f *friendDoc) error {
|
||||
_, newid, err := fs.mongoClient.Update(friends_collection_name, bson.M{
|
||||
"_id": primitive.NewObjectID(),
|
||||
}, bson.M{
|
||||
"$setOnInsert": f,
|
||||
}, options.Update().SetUpsert(true))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if newid == nil {
|
||||
return errAddFriendFailed
|
||||
}
|
||||
|
||||
f.Id = newid.(primitive.ObjectID)
|
||||
if fs.conns.addFriend(f.From, f) {
|
||||
// 모니터링 중
|
||||
conn := fs.conns.conn(f.From)
|
||||
if conn != nil {
|
||||
toidx := f.To[11] % monitoring_center_count
|
||||
fs.moncen[toidx].regChan <- registerListener{
|
||||
src: f.To,
|
||||
alias: f.ToAlias,
|
||||
l: &listener{
|
||||
c: conn,
|
||||
me: f.From,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (fs *friends) Block(ctx wshandler.ApiCallContext) {
|
||||
// BlockByMe 에 추가하고 상대의 BlockByYou를 설정한다.
|
||||
|
||||
// var bi struct {
|
||||
// From primitive.ObjectID
|
||||
// To primitive.ObjectID
|
||||
// }
|
||||
// if err := gocommon.MakeDecoder(r).Decode(&bi); err != nil {
|
||||
// logger.Println("friends.Block failed :", err)
|
||||
// w.WriteHeader(http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
// logger.Println("friends.Block :", bi)
|
||||
}
|
||||
|
||||
func (fs *friends) DeleteFriend(ctx wshandler.ApiCallContext) {
|
||||
fid, _ := primitive.ObjectIDFromHex(ctx.Arguments[0].(string))
|
||||
|
||||
var fdoc friendDoc
|
||||
if err := fs.mongoClient.FindOneAs(friends_collection_name, bson.M{
|
||||
"_id": fid,
|
||||
}, &fdoc, options.FindOne().SetProjection(bson.M{
|
||||
"from": 1,
|
||||
"to": 1,
|
||||
})); err != nil {
|
||||
logger.Println("DeleteFriend is failed :", err)
|
||||
return
|
||||
}
|
||||
|
||||
if fdoc.Id.IsZero() {
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now().UTC().Unix()
|
||||
fdoc.Deleted = true
|
||||
fdoc.Timestamp = now
|
||||
|
||||
// 나한테 삭제
|
||||
fs.mongoClient.Update(friends_collection_name, bson.M{
|
||||
"_id": fid,
|
||||
}, bson.M{
|
||||
"$set": bson.M{
|
||||
"deleted": true,
|
||||
"ts": fdoc.Timestamp,
|
||||
},
|
||||
}, options.Update().SetUpsert(false))
|
||||
fs.writeMessage(ctx.CallBy.Accid, &wshandler.DownstreamMessage{
|
||||
Body: []friendDoc{fdoc},
|
||||
Tag: friends_tag,
|
||||
})
|
||||
|
||||
// 상대방에게 삭제
|
||||
var yourdoc friendDoc
|
||||
if err := fs.mongoClient.FindOneAndUpdateAs(friends_collection_name, bson.M{
|
||||
"from": fdoc.To,
|
||||
"to": fdoc.From,
|
||||
}, bson.M{
|
||||
"$set": bson.M{
|
||||
"deleted": true,
|
||||
"ts": now,
|
||||
},
|
||||
}, &yourdoc, options.FindOneAndUpdate().SetReturnDocument(options.After).SetUpsert(false)); err == nil {
|
||||
fs.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: fdoc.To.Hex(),
|
||||
Body: []friendDoc{yourdoc},
|
||||
Tag: friends_tag,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (fs *friends) StartMonitoringFriends(ctx wshandler.ApiCallContext) {
|
||||
// 내 친구 목록에 나를 등록
|
||||
var friends []*friendDoc
|
||||
if err := fs.mongoClient.FindAllAs(friends_collection_name, bson.M{
|
||||
"from": ctx.CallBy.Accid,
|
||||
}, &friends, options.Find().SetProjection(bson.M{"to": 1, "talias": 1})); err != nil {
|
||||
logger.Println("StartMonitoringFriends is failed :", err)
|
||||
return
|
||||
}
|
||||
|
||||
me := &listener{
|
||||
c: fs.conns.conn(ctx.CallBy.Accid),
|
||||
me: ctx.CallBy.Accid,
|
||||
}
|
||||
|
||||
for _, f := range friends {
|
||||
toidx := f.To[11] % monitoring_center_count
|
||||
fs.moncen[toidx].regChan <- registerListener{
|
||||
src: f.To,
|
||||
alias: f.ToAlias,
|
||||
l: me,
|
||||
}
|
||||
}
|
||||
|
||||
fs.conns.initFriends(ctx.CallBy.Accid, friends)
|
||||
}
|
||||
|
||||
func (fs *friends) stopMonitoringFriends(accid primitive.ObjectID) {
|
||||
friends := fs.conns.clearFriends(accid)
|
||||
|
||||
if len(friends) > 0 {
|
||||
// 나를 상대방 모니터링에서 뺀다
|
||||
nilListener := &listener{c: nil, me: accid}
|
||||
for _, f := range friends {
|
||||
toidx := f.To[11] % monitoring_center_count
|
||||
fs.moncen[toidx].regChan <- registerListener{
|
||||
src: f.To,
|
||||
alias: f.ToAlias,
|
||||
l: nilListener,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (fs *friends) StopMonitoringFriends(ctx wshandler.ApiCallContext) {
|
||||
fs.stopMonitoringFriends(ctx.CallBy.Accid)
|
||||
}
|
||||
|
||||
func (fs *friends) QueryFriends(ctx wshandler.ApiCallContext) {
|
||||
queryfrom := int64(ctx.Arguments[0].(float64))
|
||||
|
||||
var myfriends []friendDoc
|
||||
err := fs.mongoClient.FindAllAs(friends_collection_name, bson.M{
|
||||
"from": ctx.CallBy.Accid,
|
||||
"ts": bson.M{"$gt": queryfrom},
|
||||
}, &myfriends)
|
||||
if err != nil {
|
||||
logger.Println("QueryReceivedInvitations failed. FindAllAs err :", err)
|
||||
}
|
||||
|
||||
if len(myfriends) > 0 {
|
||||
fs.writeMessage(ctx.CallBy.Accid, &wshandler.DownstreamMessage{
|
||||
Alias: ctx.CallBy.Alias,
|
||||
Body: myfriends,
|
||||
Tag: friends_tag,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (fs *friends) Trim(ctx wshandler.ApiCallContext) {
|
||||
stringsTobjs := func(in []any) (out []primitive.ObjectID) {
|
||||
for _, i := range in {
|
||||
p, _ := primitive.ObjectIDFromHex(i.(string))
|
||||
out = append(out, p)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ids := stringsTobjs(ctx.Arguments[2].([]any))
|
||||
if len(ids) > 0 {
|
||||
if len(ids) == 1 {
|
||||
fs.mongoClient.Delete(friends_collection_name, bson.M{"_id": ids[0]})
|
||||
} else {
|
||||
fs.mongoClient.DeleteMany(friends_collection_name, bson.D{{Key: "_id", Value: bson.M{"$in": ids}}})
|
||||
}
|
||||
}
|
||||
}
|
||||
42
core/group.go
Normal file
42
core/group.go
Normal file
@ -0,0 +1,42 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type groupConfig struct {
|
||||
UniqueIndex []string `json:"unique_index"`
|
||||
SearchIndex []string `json:"search_index"`
|
||||
MemberIndex []string `json:"member_index"`
|
||||
TextSearchFields []string `json:"text_search_field"`
|
||||
InviteExpire int32 `json:"invite_ttl"` // 그룹이 개인에게 보낸 초대장 만료 기한
|
||||
CandidateExpire int32 `json:"candidate_ttl"` // 개인이 그룹에게 보낸 신청서 만료 기한
|
||||
InviteeExlusive bool `json:"invitee_exlusive"`
|
||||
InviteeIsMember bool `json:"invitee_is_member"`
|
||||
MaxMember int `json:"max_member"`
|
||||
Transient bool `json:"transient"`
|
||||
|
||||
Name string
|
||||
}
|
||||
|
||||
type group interface {
|
||||
Create(form url.Values, doc bson.M) (primitive.ObjectID, error)
|
||||
Candidate(groupID primitive.ObjectID, memberID primitive.ObjectID, doc bson.M) error
|
||||
Join(groupID primitive.ObjectID, memberID primitive.ObjectID, doc bson.M) error
|
||||
Invite(groupID primitive.ObjectID, memberID primitive.ObjectID, inviterDoc bson.M, inviteeDoc bson.M) (string, error)
|
||||
CancelInvitation(groupID primitive.ObjectID, ticketID primitive.ObjectID) error
|
||||
AcceptInvitation(groupID primitive.ObjectID, mid primitive.ObjectID, member bson.M) error
|
||||
DenyInvitation(groupID primitive.ObjectID, mid primitive.ObjectID, ticketID primitive.ObjectID) error
|
||||
QueryInvitations(memberID primitive.ObjectID, after primitive.Timestamp) ([]bson.M, error)
|
||||
Exist(groupID primitive.ObjectID, filter bson.M) (bool, error)
|
||||
FindAll(filter bson.M, projection string, after primitive.Timestamp) ([]bson.M, error)
|
||||
FindOne(groupID primitive.ObjectID, projection string) (bson.M, error)
|
||||
Leave(groupID primitive.ObjectID, memberID primitive.ObjectID) error
|
||||
UpdateMemberDocument(groupID primitive.ObjectID, memberID primitive.ObjectID, doc bson.M) error
|
||||
Dismiss(groupID primitive.ObjectID) error
|
||||
UpdateGroupDocument(groupID primitive.ObjectID, doc bson.M) error
|
||||
QueryGroupMembers(groupID primitive.ObjectID) (bson.M, error)
|
||||
}
|
||||
553
core/group_memory.go
Normal file
553
core/group_memory.go
Normal file
@ -0,0 +1,553 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
type accountID = primitive.ObjectID
|
||||
type ticketID = primitive.ObjectID
|
||||
type groupID = primitive.ObjectID
|
||||
|
||||
func init() {
|
||||
gob.Register(memberDoc{})
|
||||
gob.Register(groupDoc{})
|
||||
gob.Register(Invitation{})
|
||||
gob.Register(InvitationFail{})
|
||||
}
|
||||
|
||||
func makeTid(gid groupID, in accountID) string {
|
||||
var out primitive.ObjectID
|
||||
for i := range in {
|
||||
out[12-i-1] = gid[i] ^ in[12-i-1]
|
||||
}
|
||||
return out.Hex()
|
||||
}
|
||||
|
||||
type Invitation struct {
|
||||
GroupID groupID `json:"_gid"`
|
||||
TicketID string `json:"_tid"`
|
||||
Inviter bson.M `json:"_inviter"` // memberDoc.Body
|
||||
ExpireAtUTC int64 `json:"_expire_at_utc"`
|
||||
}
|
||||
|
||||
// 플레이어한테 공유하는 멤버 정보
|
||||
type memberDoc struct {
|
||||
Body bson.M `json:"_body"`
|
||||
Invite bool `json:"_invite"`
|
||||
InviteExpire int64 `json:"_invite_exp"`
|
||||
}
|
||||
|
||||
type InvitationFail bson.M
|
||||
|
||||
type groupDoc struct {
|
||||
Members map[string]any `json:"_members"`
|
||||
InCharge string `json:"_incharge"`
|
||||
Gid string `json:"_gid"`
|
||||
|
||||
rh *gocommon.RedisonHandler
|
||||
id groupID
|
||||
}
|
||||
|
||||
func (gd *groupDoc) loadMemberFull(tid string) (bson.M, error) {
|
||||
full, err := gd.rh.JSONGet(gd.strid(), "$._members."+tid)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
bt := []byte(full.(string))
|
||||
bt = bt[1 : len(bt)-1]
|
||||
|
||||
var doc bson.M
|
||||
if err = json.Unmarshal(bt, &doc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
func (gd *groupDoc) loadFull() (doc bson.M) {
|
||||
// 새 멤버에 그룹 전체를 알림
|
||||
full, err := gd.rh.JSONGet(gd.strid(), "$")
|
||||
if err == nil {
|
||||
bt := []byte(full.(string))
|
||||
bt = bt[1 : len(bt)-1]
|
||||
err = json.Unmarshal(bt, &doc)
|
||||
if err != nil {
|
||||
logger.Println("loadFull err :", err)
|
||||
}
|
||||
} else {
|
||||
logger.Println("loadFull err :", err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (gd *groupDoc) strid() string {
|
||||
if len(gd.Gid) == 0 {
|
||||
gd.Gid = gd.id.Hex()
|
||||
}
|
||||
return gd.Gid
|
||||
}
|
||||
|
||||
func (gd *groupDoc) tid(in accountID) string {
|
||||
return makeTid(gd.id, in)
|
||||
}
|
||||
|
||||
func (gd *groupDoc) mid(tid string) accountID {
|
||||
tidobj, _ := primitive.ObjectIDFromHex(tid)
|
||||
var out primitive.ObjectID
|
||||
for i := range tidobj {
|
||||
out[12-i-1] = gd.id[i] ^ tidobj[12-i-1]
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (gd *groupDoc) addInvite(inviteeDoc bson.M, ttl time.Duration, max int) (*memberDoc, error) {
|
||||
targetmid := inviteeDoc["_mid"].(accountID)
|
||||
targetbody := inviteeDoc["body"].(bson.M)
|
||||
|
||||
// 초대 가능한 빈 자리가 있나
|
||||
tids, err := gd.rh.JSONObjKeys(gd.strid(), "$._members")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
createNewDoc := func() *memberDoc {
|
||||
return &memberDoc{
|
||||
Body: targetbody,
|
||||
Invite: true,
|
||||
InviteExpire: now.Add(ttl).Unix(),
|
||||
}
|
||||
}
|
||||
|
||||
newtid := gd.tid(targetmid)
|
||||
if len(tids) < max {
|
||||
// 빈자리를 찾았다.
|
||||
newdoc := createNewDoc()
|
||||
_, err := gd.rh.JSONSet(gd.strid(), "$._members."+newtid, newdoc)
|
||||
return newdoc, err
|
||||
}
|
||||
|
||||
expires, err := gd.rh.JSONGetInt64(gd.strid(), "$._members.._invite_exp")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var delpaths []string
|
||||
for i, expire := range expires {
|
||||
if expire < now.Unix() {
|
||||
// 만료된 초대가 있네? 지우자
|
||||
delpaths = append(delpaths, "$._members."+tids[i])
|
||||
}
|
||||
}
|
||||
|
||||
if len(delpaths) == 0 {
|
||||
// 빈자리가 없다
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
if err := gd.rh.JSONMDel(gd.strid(), delpaths); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
newdoc := createNewDoc()
|
||||
_, err = gd.rh.JSONSet(gd.strid(), "$._members."+newtid, newdoc)
|
||||
return newdoc, err
|
||||
}
|
||||
|
||||
func (gd *groupDoc) addMember(mid accountID, doc bson.M) (bson.M, error) {
|
||||
tid := gd.tid(mid)
|
||||
prefix := "$._members." + tid
|
||||
|
||||
if _, err := gd.rh.JSONMerge(gd.strid(), prefix+"._body", doc, gocommon.RedisonSetOptionXX); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if err := gd.rh.JSONMDel(gd.strid(), []string{prefix + "._invite", prefix + "._invite_exp"}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return gd.loadMemberFull(tid)
|
||||
}
|
||||
|
||||
func (gd *groupDoc) removeMember(mid accountID) error {
|
||||
_, err := gd.rh.JSONDel(gd.strid(), "$._members."+gd.tid(mid))
|
||||
return err
|
||||
}
|
||||
|
||||
func (gd *groupDoc) getMembers() (map[string]any, error) {
|
||||
res, err := gd.rh.JSONGet(gd.strid(), "$._members")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var temp []map[string]any
|
||||
err = json.Unmarshal([]byte(res.(string)), &temp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out := make(map[string]any)
|
||||
for k, v := range temp[0] {
|
||||
body := v.(map[string]any)["_body"]
|
||||
out[gd.mid(k).Hex()] = body
|
||||
}
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
type groupInMemory struct {
|
||||
*groupConfig
|
||||
sendUpstreamMessage func(*wshandler.UpstreamMessage)
|
||||
sendEnterRoomMessage func(groupID, accountID)
|
||||
sendLeaveRoomMessage func(groupID, accountID)
|
||||
rh *gocommon.RedisonHandler
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) createGroup(newid groupID, charge accountID, chargeDoc bson.M) (*groupDoc, error) {
|
||||
tid := makeTid(newid, charge)
|
||||
|
||||
gd := &groupDoc{
|
||||
Members: map[string]any{
|
||||
tid: &memberDoc{
|
||||
Body: chargeDoc,
|
||||
Invite: false,
|
||||
InviteExpire: 0,
|
||||
},
|
||||
},
|
||||
InCharge: tid,
|
||||
|
||||
rh: gm.rh,
|
||||
id: newid,
|
||||
}
|
||||
|
||||
_, err := gm.rh.JSONSet(gd.strid(), "$", gd, gocommon.RedisonSetOptionNX)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return gd, nil
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) find(id groupID) (*groupDoc, error) {
|
||||
if id.IsZero() {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
_, err := gm.rh.JSONObjLen(id.Hex(), "$")
|
||||
if err == redis.Nil {
|
||||
return nil, nil
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &groupDoc{
|
||||
rh: gm.rh,
|
||||
id: id,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) Create(form url.Values, base bson.M) (groupID, error) {
|
||||
return primitive.NilObjectID, nil
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) Candidate(gid groupID, mid accountID, doc bson.M) error {
|
||||
logger.Error("not implemented func : Canidate")
|
||||
return nil
|
||||
}
|
||||
|
||||
var errGroupNotExist = errors.New("group does not exist")
|
||||
|
||||
func (gm *groupInMemory) Join(gid groupID, mid accountID, doc bson.M) error {
|
||||
gd, err := gm.find(gid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if gd == nil {
|
||||
// 그룹이 없다. 실패
|
||||
return errGroupNotExist
|
||||
}
|
||||
|
||||
// 내 정보 업데이트할 때에도 사용됨
|
||||
if memdoc, err := gd.addMember(mid, doc); err == nil {
|
||||
// 기존 유저에게 새 유저 알림
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + gid.Hex(),
|
||||
Body: map[string]any{
|
||||
gd.tid(mid): memdoc,
|
||||
},
|
||||
Tag: []string{"MemberDocFull"},
|
||||
})
|
||||
|
||||
gm.sendEnterRoomMessage(gid, mid)
|
||||
|
||||
// 새 멤버에 그룹 전체를 알림
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "@" + mid.Hex(),
|
||||
Body: gd.loadFull(),
|
||||
Tag: []string{"GroupDocFull"},
|
||||
})
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
var errInviteeDocMidMissing = errors.New("inviteeDoc must have '_mid' field")
|
||||
var errAlreadyInvited = errors.New("this target is already invited by someone or me")
|
||||
|
||||
func (gm *groupInMemory) Invite(gid groupID, mid accountID, inviterDoc bson.M, inviteeDoc bson.M) (string, error) {
|
||||
targetid, ok := inviteeDoc["_mid"].(accountID)
|
||||
if !ok {
|
||||
return "", errInviteeDocMidMissing
|
||||
}
|
||||
|
||||
// targetid에 초대한 mid가 들어있다.
|
||||
already, err := gm.rh.Get(context.Background(), targetid.Hex()).Result()
|
||||
if err != nil && err != redis.Nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if len(already) > 0 {
|
||||
if already != mid.Hex() {
|
||||
// 이미 초대 중이다.
|
||||
// inviter한테 알려줘야 한다.
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "@" + mid.Hex(),
|
||||
Body: inviteeDoc,
|
||||
Tag: []string{"InvitationFail"},
|
||||
})
|
||||
}
|
||||
return "", errAlreadyInvited
|
||||
}
|
||||
|
||||
gd, err := gm.find(gid)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if gd == nil {
|
||||
gd, err = gm.createGroup(gid, mid, inviterDoc)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
// 내가 wshandler room에 입장
|
||||
gm.sendEnterRoomMessage(gid, mid)
|
||||
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "@" + mid.Hex(),
|
||||
Body: gd,
|
||||
Tag: []string{"GroupDocFull"},
|
||||
})
|
||||
}
|
||||
|
||||
newdoc, err := gd.addInvite(inviteeDoc, time.Duration(gm.InviteExpire+1)*time.Second, gm.MaxMember)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// 초대 중 표시
|
||||
_, err = gm.rh.SetNX(context.Background(), targetid.Hex(), mid.Hex(), time.Duration(gm.InviteExpire)*time.Second).Result()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
// invitee에게 알림
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "@" + targetid.Hex(),
|
||||
Body: Invitation{
|
||||
GroupID: gid,
|
||||
TicketID: gd.tid(targetid),
|
||||
Inviter: inviterDoc,
|
||||
ExpireAtUTC: newdoc.InviteExpire,
|
||||
},
|
||||
Tag: []string{"Invitation"},
|
||||
})
|
||||
|
||||
return gd.strid(), nil
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) CancelInvitation(gid groupID, tid ticketID) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
var errInvitationExpired = errors.New("invitation is already expired")
|
||||
|
||||
func (gm *groupInMemory) AcceptInvitation(gid groupID, mid accountID, member bson.M) error {
|
||||
cnt, err := gm.rh.Del(context.Background(), mid.Hex()).Result()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if cnt == 0 {
|
||||
// 만료됨
|
||||
return errInvitationExpired
|
||||
}
|
||||
|
||||
gd := &groupDoc{
|
||||
id: gid,
|
||||
rh: gm.rh,
|
||||
}
|
||||
|
||||
memberDoc, err := gd.addMember(mid, member)
|
||||
if err == nil {
|
||||
// 기존 멤버에게 새 멤버를 알림
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + gid.Hex(),
|
||||
Body: map[string]any{
|
||||
gd.tid(mid): memberDoc,
|
||||
},
|
||||
Tag: []string{"MemberDocFull"},
|
||||
})
|
||||
|
||||
gm.sendEnterRoomMessage(gid, mid)
|
||||
|
||||
// 새 멤버에 그룹 전체를 알림
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "@" + mid.Hex(),
|
||||
Body: gd.loadFull(),
|
||||
Tag: []string{"GroupDocFull"},
|
||||
})
|
||||
}
|
||||
|
||||
// 실패
|
||||
return err
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) QueryGroupMembers(gid groupID) (bson.M, error) {
|
||||
gd := groupDoc{
|
||||
id: gid,
|
||||
rh: gm.rh,
|
||||
}
|
||||
return gd.getMembers()
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) DenyInvitation(gid groupID, mid accountID, tid ticketID) error {
|
||||
gm.rh.Del(context.Background(), mid.Hex()).Result()
|
||||
gd := groupDoc{
|
||||
id: gid,
|
||||
rh: gm.rh,
|
||||
}
|
||||
return gd.removeMember(mid)
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) QueryInvitations(mid accountID, after primitive.Timestamp) ([]bson.M, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (gm *groupInMemory) Exist(gid groupID, filter bson.M) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
func (gm *groupInMemory) FindAll(filter bson.M, projection string, after primitive.Timestamp) ([]bson.M, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (gm *groupInMemory) FindOne(gid groupID, projection string) (bson.M, error) {
|
||||
return nil, nil
|
||||
}
|
||||
func (gm *groupInMemory) Leave(gid groupID, mid accountID) error {
|
||||
gd := groupDoc{
|
||||
id: gid,
|
||||
rh: gm.rh,
|
||||
}
|
||||
if err := gd.removeMember(mid); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 나한테는 빈 GroupDocFull을 보낸다. 그러면 지워짐
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "@" + mid.Hex(),
|
||||
Body: bson.M{"gid": gid},
|
||||
Tag: []string{"GroupDocFull", gid.Hex()},
|
||||
})
|
||||
gm.sendLeaveRoomMessage(gid, mid)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) UpdateMemberDocument(gid groupID, mid accountID, doc bson.M) error {
|
||||
gd := &groupDoc{
|
||||
id: gid,
|
||||
rh: gm.rh,
|
||||
}
|
||||
|
||||
prefixPath := fmt.Sprintf("$._members.%s.", gd.tid(mid))
|
||||
err := gm.rh.JSONMSetRel(gd.strid(), prefixPath, doc)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + gid.Hex(),
|
||||
Body: map[string]any{
|
||||
gd.tid(mid): doc,
|
||||
},
|
||||
Tag: []string{"MemberDocFragment"},
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) Dismiss(gid groupID) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (gm *groupInMemory) UpdateGroupDocument(gid groupID, frag bson.M) error {
|
||||
gd := groupDoc{
|
||||
id: gid,
|
||||
rh: gm.rh,
|
||||
}
|
||||
|
||||
if err := gm.rh.JSONMSetRel(gd.strid(), "$.", frag); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// 업데이트 알림
|
||||
gm.sendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: "#" + gid.Hex(),
|
||||
Body: frag,
|
||||
Tag: []string{"GroupDocFragment"},
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cfg *groupConfig) prepareInMemory(ctx context.Context, typename string, sub *subTavern) (group, error) {
|
||||
// group document
|
||||
// member document
|
||||
region := sub.region
|
||||
wsh := sub.wsh
|
||||
storage := config.RegionStorage[sub.region]
|
||||
redisClient, err := gocommon.NewRedisClient(storage.Redis["tavern"])
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
// 여기서는 subscribe channel
|
||||
// 각 함수에서는 publish
|
||||
gm := &groupInMemory{
|
||||
groupConfig: cfg,
|
||||
rh: gocommon.NewRedisonHandler(ctx, redisClient),
|
||||
sendUpstreamMessage: func(msg *wshandler.UpstreamMessage) {
|
||||
wsh.SendUpstreamMessage(region, msg)
|
||||
},
|
||||
sendEnterRoomMessage: func(gid groupID, accid accountID) {
|
||||
wsh.EnterRoom(region, gid.Hex(), accid)
|
||||
},
|
||||
sendLeaveRoomMessage: func(gid groupID, accid accountID) {
|
||||
wsh.LeaveRoom(region, gid.Hex(), accid)
|
||||
},
|
||||
}
|
||||
|
||||
return gm, nil
|
||||
}
|
||||
@ -1,383 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/gob"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
||||
)
|
||||
|
||||
const (
|
||||
invitation_collection_name = gocommon.CollectionName("invitation")
|
||||
)
|
||||
|
||||
var invitation_sent_tag = []string{"social.InvitationsSent"}
|
||||
var invitation_received_tag = []string{"social.InvitationsReceived"}
|
||||
var friends_tag = []string{"social.Friends"}
|
||||
|
||||
type invitation struct {
|
||||
mongoClient gocommon.MongoClient
|
||||
redison *gocommon.RedisonHandler
|
||||
wsh *wshandler.WebsocketHandler
|
||||
f *friends
|
||||
}
|
||||
|
||||
type invitationDoc struct {
|
||||
Id primitive.ObjectID `bson:"_id,omitempty" json:"_id"`
|
||||
From primitive.ObjectID `bson:"from,omitempty" json:"-"`
|
||||
To primitive.ObjectID `bson:"to,omitempty" json:"-"`
|
||||
FromAlias string `bson:"falias,omitempty" json:"from"`
|
||||
ToAlias string `bson:"talias,omitempty" json:"to"`
|
||||
Timestamp int64 `bson:"ts" json:"ts"`
|
||||
Deleted bool `bson:"deleted,omitempty" json:"deleted,omitempty"`
|
||||
}
|
||||
|
||||
func init() {
|
||||
gob.Register([]invitationDoc{})
|
||||
}
|
||||
|
||||
func makeInvitation(ctx context.Context, s *Social, f *friends) (*invitation, error) {
|
||||
if err := s.mongoClient.MakeUniqueIndices(invitation_collection_name, map[string]bson.D{
|
||||
"fromto": {{Key: "from", Value: 1}, {Key: "to", Value: 1}},
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 내가 받은거
|
||||
if err := s.mongoClient.MakeIndices(invitation_collection_name, map[string]bson.D{
|
||||
"received": {{Key: "to", Value: 1}, {Key: "ts", Value: -1}},
|
||||
}); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &invitation{
|
||||
mongoClient: s.mongoClient,
|
||||
redison: s.redison,
|
||||
wsh: s.wsh,
|
||||
f: f,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (iv *invitation) QueryReceivedInvitations(ctx wshandler.ApiCallContext) {
|
||||
// 내가 받은 초대 목록
|
||||
queryfrom := int64(ctx.Arguments[0].(float64))
|
||||
|
||||
var receives []*invitationDoc
|
||||
|
||||
err := iv.mongoClient.FindAllAs(invitation_collection_name, bson.M{
|
||||
"to": ctx.CallBy.Accid,
|
||||
"ts": bson.M{"$gt": queryfrom},
|
||||
}, &receives)
|
||||
if err != nil {
|
||||
logger.Println("QueryReceivedInvitations failed. FindAllAs err :", err)
|
||||
}
|
||||
|
||||
if len(receives) > 0 {
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ctx.CallBy.Accid.Hex(),
|
||||
Body: receives,
|
||||
Tag: invitation_received_tag,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (iv *invitation) QuerySentInvitations(ctx wshandler.ApiCallContext) {
|
||||
// 내가 보낸 초대 목록
|
||||
queryfrom := int64(ctx.Arguments[0].(float64))
|
||||
|
||||
var receives []*invitationDoc
|
||||
|
||||
err := iv.mongoClient.FindAllAs(invitation_collection_name, bson.M{
|
||||
"from": ctx.CallBy.Accid,
|
||||
"ts": bson.M{"$gt": queryfrom},
|
||||
"falias": bson.M{"$exists": true},
|
||||
}, &receives)
|
||||
if err != nil {
|
||||
logger.Println("QueryReceivedInvitations failed. FindAllAs err :", err)
|
||||
}
|
||||
|
||||
if len(receives) > 0 {
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ctx.CallBy.Accid.Hex(),
|
||||
Body: receives,
|
||||
Tag: invitation_sent_tag,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (iv *invitation) CancelInvitation(ctx wshandler.ApiCallContext) {
|
||||
// ctx.CallBy.Accid
|
||||
id, _ := primitive.ObjectIDFromHex(ctx.Arguments[0].(string))
|
||||
|
||||
var ivdoc invitationDoc
|
||||
if err := iv.mongoClient.FindOneAs(invitation_collection_name, bson.M{
|
||||
"_id": id,
|
||||
}, &ivdoc); err != nil {
|
||||
logger.Println("CancelInvitation failed:", err)
|
||||
return
|
||||
}
|
||||
|
||||
if ivdoc.From != ctx.CallBy.Accid {
|
||||
return
|
||||
}
|
||||
|
||||
ivdoc.Deleted = true
|
||||
if _, _, err := iv.mongoClient.Update(invitation_collection_name, bson.M{
|
||||
"_id": id,
|
||||
}, bson.M{
|
||||
"$set": bson.M{
|
||||
"falias": "",
|
||||
"deleted": true,
|
||||
"ts": time.Now().UTC().Unix(),
|
||||
},
|
||||
}); err != nil {
|
||||
logger.Println("CancelInvitation failed:", err)
|
||||
return
|
||||
}
|
||||
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.To.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_received_tag,
|
||||
})
|
||||
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.From.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_sent_tag,
|
||||
})
|
||||
}
|
||||
|
||||
func (iv *invitation) AcceptInvitation(ctx wshandler.ApiCallContext) {
|
||||
invId, _ := primitive.ObjectIDFromHex(ctx.Arguments[0].(string))
|
||||
|
||||
var ivdoc invitationDoc
|
||||
if err := iv.mongoClient.FindOneAs(invitation_collection_name, bson.M{
|
||||
"_id": invId,
|
||||
}, &ivdoc); err != nil {
|
||||
logger.Println("AcceptInvitation failed:", err)
|
||||
return
|
||||
}
|
||||
|
||||
if ivdoc.Id != invId {
|
||||
// 초대가 없다
|
||||
return
|
||||
}
|
||||
|
||||
if ivdoc.To != ctx.CallBy.Accid {
|
||||
// 내가 받은 초대가 아니네?
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now().UTC().Unix()
|
||||
f1 := friendDoc{
|
||||
From: ivdoc.To, // 수락한 나
|
||||
To: ivdoc.From, // 상대방
|
||||
ToAlias: ivdoc.FromAlias,
|
||||
Timestamp: now,
|
||||
}
|
||||
f2 := friendDoc{
|
||||
From: ivdoc.From, // 상대방
|
||||
To: ivdoc.To, // 나
|
||||
ToAlias: ivdoc.ToAlias,
|
||||
Timestamp: now,
|
||||
}
|
||||
|
||||
// 나한테 상대방을 친구로 만들고
|
||||
if err := iv.f.addFriend(&f1); err == nil {
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: f1.From.Hex(),
|
||||
Body: []friendDoc{f1},
|
||||
Tag: friends_tag,
|
||||
})
|
||||
} else {
|
||||
logger.Println("AcceptInvitation failed. addFriend(f1) err :", err)
|
||||
return
|
||||
}
|
||||
|
||||
// 상대방한테 나를 친구로 만듬
|
||||
if err := iv.f.addFriend(&f2); err == nil {
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: f2.From.Hex(),
|
||||
Body: []friendDoc{f2},
|
||||
Tag: friends_tag,
|
||||
})
|
||||
} else {
|
||||
logger.Println("AcceptInvitation failed. addFriend(f2) err :", err)
|
||||
return
|
||||
}
|
||||
|
||||
iv.mongoClient.Update(invitation_collection_name, bson.M{
|
||||
"_id": invId,
|
||||
}, bson.M{
|
||||
"$set": bson.M{
|
||||
"deleted": true,
|
||||
"ts": now,
|
||||
},
|
||||
}, options.Update().SetUpsert(false))
|
||||
}
|
||||
|
||||
func (iv *invitation) DenyInvitation(ctx wshandler.ApiCallContext) {
|
||||
invId, _ := primitive.ObjectIDFromHex(ctx.Arguments[0].(string))
|
||||
|
||||
var ivdoc invitationDoc
|
||||
if err := iv.mongoClient.FindOneAs(invitation_collection_name, bson.M{
|
||||
"_id": invId,
|
||||
}, &ivdoc); err != nil {
|
||||
logger.Println("AcceptInvitation failed:", err)
|
||||
return
|
||||
}
|
||||
|
||||
if ivdoc.Id != invId {
|
||||
// 초대가 없다
|
||||
return
|
||||
}
|
||||
|
||||
if ivdoc.To != ctx.CallBy.Accid {
|
||||
// 내가 받은 초대가 아니네?
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now().UTC().Unix()
|
||||
ivdoc.Timestamp = now
|
||||
ivdoc.Deleted = true
|
||||
if _, _, err := iv.mongoClient.Update(invitation_collection_name, bson.M{
|
||||
"_id": invId,
|
||||
}, bson.M{
|
||||
"$set": bson.M{
|
||||
"deleted": true,
|
||||
"ts": now,
|
||||
},
|
||||
}, options.Update().SetUpsert(false)); err != nil {
|
||||
logger.Println("DenyInvitation failed. addFriend(f2) err :", err)
|
||||
return
|
||||
}
|
||||
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.To.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_received_tag,
|
||||
})
|
||||
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.From.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_sent_tag,
|
||||
})
|
||||
}
|
||||
|
||||
func (iv *invitation) Trim(ctx wshandler.ApiCallContext) {
|
||||
stringsTobjs := func(in []any) (out []primitive.ObjectID) {
|
||||
for _, i := range in {
|
||||
p, _ := primitive.ObjectIDFromHex(i.(string))
|
||||
out = append(out, p)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
ids := stringsTobjs(ctx.Arguments[0].([]any))
|
||||
ids = append(ids, stringsTobjs(ctx.Arguments[1].([]any))...)
|
||||
if len(ids) > 0 {
|
||||
if len(ids) == 1 {
|
||||
iv.mongoClient.Delete(invitation_collection_name, bson.M{"_id": ids[0], "deleted": true})
|
||||
} else {
|
||||
iv.mongoClient.DeleteMany(invitation_collection_name, bson.D{
|
||||
{Key: "_id", Value: bson.M{"$in": ids}},
|
||||
{Key: "deleted", Value: true},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (iv *invitation) InviteAsFriend(w http.ResponseWriter, r *http.Request) {
|
||||
// 1. mongodb에 추가
|
||||
// 1-1. block이 되어있다면(==이미 도큐먼트가 있다면) 마치 성공인 것처럼 아무것도 안하고 끝
|
||||
// 2. mongodb에 추가가 성공하면 publish
|
||||
var ivdoc invitationDoc
|
||||
|
||||
if err := gocommon.MakeDecoder(r).Decode(&ivdoc); err != nil {
|
||||
logger.Println("IniviteAsFriend failed:", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
ivdoc.Timestamp = time.Now().UTC().Unix()
|
||||
_, newid, err := iv.mongoClient.Update(invitation_collection_name, bson.M{
|
||||
"from": ivdoc.From,
|
||||
"to": ivdoc.To,
|
||||
}, bson.M{
|
||||
"$set": bson.M{
|
||||
"ts": ivdoc.Timestamp,
|
||||
"falias": ivdoc.FromAlias,
|
||||
"talias": ivdoc.ToAlias,
|
||||
},
|
||||
}, options.Update().SetUpsert(true))
|
||||
if err != nil {
|
||||
logger.Println("IniviteAsFriend failed:", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if newid != nil {
|
||||
ivdoc.Id = newid.(primitive.ObjectID)
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.To.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_received_tag,
|
||||
})
|
||||
} else {
|
||||
found, _ := iv.mongoClient.FindOne(invitation_collection_name, bson.M{
|
||||
"from": ivdoc.From,
|
||||
"to": ivdoc.To,
|
||||
}, options.FindOne().SetProjection(bson.M{"_id": 1}))
|
||||
|
||||
ivdoc.Id = found["_id"].(primitive.ObjectID)
|
||||
}
|
||||
|
||||
if !ivdoc.Id.IsZero() {
|
||||
iv.wsh.SendUpstreamMessage(&wshandler.UpstreamMessage{
|
||||
Target: ivdoc.From.Hex(),
|
||||
Body: []invitationDoc{ivdoc},
|
||||
Tag: invitation_sent_tag,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (iv *invitation) Block(w http.ResponseWriter, r *http.Request) {
|
||||
// 초대가 있으면
|
||||
// var bi struct {
|
||||
// From primitive.ObjectID
|
||||
// To primitive.ObjectID
|
||||
// FromAlias string
|
||||
// }
|
||||
// if err := gocommon.MakeDecoder(r).Decode(&bi); err != nil {
|
||||
// logger.Println("invitation.Block failed :", err)
|
||||
// w.WriteHeader(http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
|
||||
// now := time.Now().UTC().Unix()
|
||||
// // From이 To를 block했으므로 To가 From을 초대하는 것을 방지하려면 둘을 뒤집어서 문서를 만들어 놔야 함
|
||||
// // 이미 존재하는 초대일 수도 있다.
|
||||
// _, _, err := iv.mongoClient.Update(invitation_collection_name, bson.M{
|
||||
// "from": bi.To,
|
||||
// "to": bi.From,
|
||||
// }, bson.M{
|
||||
// "$set": invitationDoc{
|
||||
// ToAlias: bi.FromAlias,
|
||||
// Timestamp: now,
|
||||
// },
|
||||
// }, options.Update().SetUpsert(true))
|
||||
// if err != nil {
|
||||
// logger.Println("Block failed:", err)
|
||||
// w.WriteHeader(http.StatusInternalServerError)
|
||||
// return
|
||||
// }
|
||||
}
|
||||
134
core/social.go
134
core/social.go
@ -1,134 +0,0 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/flagx"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
"repositories.action2quare.com/ayo/gocommon/session"
|
||||
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
||||
)
|
||||
|
||||
var devflag = flagx.Bool("dev", false, "")
|
||||
|
||||
type SocialConfig struct {
|
||||
session.SessionConfig `json:",inline"`
|
||||
|
||||
MaingateApiToken string `json:"maingate_api_token"`
|
||||
RedisURL string `json:"social_redis_url"`
|
||||
MongoURL string `json:"social_storage_url"`
|
||||
}
|
||||
|
||||
var config SocialConfig
|
||||
|
||||
type Social struct {
|
||||
wsh *wshandler.WebsocketHandler
|
||||
mongoClient gocommon.MongoClient
|
||||
redison *gocommon.RedisonHandler
|
||||
httpApiBorker gocommon.HttpApiBroker
|
||||
}
|
||||
|
||||
// New :
|
||||
func New(ctx context.Context, wsh *wshandler.WebsocketHandler, inconfig *SocialConfig) (*Social, error) {
|
||||
if inconfig == nil {
|
||||
var loaded SocialConfig
|
||||
if err := gocommon.LoadConfig(&loaded); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inconfig = &loaded
|
||||
}
|
||||
|
||||
config = *inconfig
|
||||
opt, err := redis.ParseURL(config.RedisURL)
|
||||
if err != nil {
|
||||
return nil, logger.ErrorWithCallStack(err)
|
||||
}
|
||||
|
||||
mc, err := gocommon.NewMongoClient(ctx, config.MongoURL)
|
||||
if err != nil {
|
||||
return nil, logger.ErrorWithCallStack(err)
|
||||
}
|
||||
|
||||
so := &Social{
|
||||
wsh: wsh,
|
||||
redison: gocommon.NewRedisonHandler(ctx, redis.NewClient(opt)),
|
||||
mongoClient: mc,
|
||||
}
|
||||
|
||||
if err := so.prepare(ctx); err != nil {
|
||||
logger.Println("social prepare() failed :", err)
|
||||
return nil, logger.ErrorWithCallStack(err)
|
||||
}
|
||||
|
||||
return so, nil
|
||||
}
|
||||
|
||||
func (so *Social) Cleanup() {
|
||||
so.mongoClient.Close()
|
||||
}
|
||||
|
||||
func (so *Social) prepare(ctx context.Context) error {
|
||||
redisClient, err := gocommon.NewRedisClient(config.RedisURL)
|
||||
if err != nil {
|
||||
return logger.ErrorWithCallStack(err)
|
||||
}
|
||||
|
||||
so.redison = gocommon.NewRedisonHandler(redisClient.Context(), redisClient)
|
||||
|
||||
friends, err := makeFriends(ctx, so)
|
||||
if err != nil {
|
||||
return logger.ErrorWithCallStack(err)
|
||||
}
|
||||
so.wsh.AddHandler(wshandler.MakeWebsocketApiHandler(friends, "social"))
|
||||
so.httpApiBorker.AddHandler(gocommon.MakeHttpApiHandler(friends, "social"))
|
||||
|
||||
invitation, err := makeInvitation(ctx, so, friends)
|
||||
if err != nil {
|
||||
return logger.ErrorWithCallStack(err)
|
||||
}
|
||||
so.wsh.AddHandler(wshandler.MakeWebsocketApiHandler(invitation, "social"))
|
||||
so.httpApiBorker.AddHandler(gocommon.MakeHttpApiHandler(invitation, "social"))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (so *Social) RegisterHandlers(ctx context.Context, serveMux *http.ServeMux, prefix string) error {
|
||||
so.wsh.AddHandler(wshandler.MakeWebsocketApiHandler(so, "social"))
|
||||
pattern := gocommon.MakeHttpHandlerPattern(prefix, "api")
|
||||
serveMux.HandleFunc(pattern, so.api)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (so *Social) api(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
s := recover()
|
||||
if s != nil {
|
||||
logger.Error(s)
|
||||
}
|
||||
io.Copy(io.Discard, r.Body)
|
||||
r.Body.Close()
|
||||
}()
|
||||
|
||||
// 서버에서 오는 요청만 처리
|
||||
apitoken := r.Header.Get("MG-X-API-TOKEN")
|
||||
if apitoken != config.MaingateApiToken {
|
||||
// 서버가 보내는 쿼리만 허용
|
||||
logger.Println("MG-X-API-TOKEN is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
funcname := r.URL.Query().Get("call")
|
||||
if len(funcname) == 0 {
|
||||
logger.Println("query param 'call' is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
so.httpApiBorker.Call(funcname, w, r)
|
||||
}
|
||||
@ -1,65 +0,0 @@
|
||||
// warroom project main.go
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
func TestPubSub(t *testing.T) {
|
||||
opt0, _ := redis.ParseURL("redis://192.168.8.94:6380/0")
|
||||
opt1, _ := redis.ParseURL("redis://192.168.8.94:6380/1")
|
||||
|
||||
rc0 := redis.NewClient(opt0)
|
||||
rc1 := redis.NewClient(opt1)
|
||||
|
||||
go func() {
|
||||
time.Sleep(time.Second)
|
||||
rc1.Publish(context.Background(), "__testchan", "real???")
|
||||
fmt.Println("published")
|
||||
}()
|
||||
|
||||
pubsub := rc0.Subscribe(context.Background(), "__testchan")
|
||||
msg, err := pubsub.ReceiveMessage(context.Background())
|
||||
fmt.Println(msg.Payload, err)
|
||||
}
|
||||
func makeHash(chanName string, index uint32) string {
|
||||
for len(chanName) < 12 {
|
||||
chanName += chanName
|
||||
}
|
||||
left := chanName[:6]
|
||||
right := chanName[len(chanName)-6:]
|
||||
base := []byte(left + right)
|
||||
for i := 0; i < 12; i++ {
|
||||
base[i] += base[12-i-1]
|
||||
}
|
||||
|
||||
bs := make([]byte, 4)
|
||||
binary.LittleEndian.PutUint32(bs, index)
|
||||
for i, c := range bs {
|
||||
base[i] ^= c
|
||||
}
|
||||
var gid primitive.ObjectID
|
||||
copy(gid[:], base)
|
||||
|
||||
return gid.Hex()
|
||||
}
|
||||
|
||||
func TestNameHash(t *testing.T) {
|
||||
for i := 0; i < 10; i++ {
|
||||
makeHash("Urud", uint32(i))
|
||||
fmt.Printf("Urud:%d - %s\n", i, makeHash("Urud", uint32(i)))
|
||||
makeHash("Sheldon", uint32(i))
|
||||
fmt.Printf("Sheldon:%d - %s\n", i, makeHash("Sheldon", uint32(i)))
|
||||
}
|
||||
}
|
||||
|
||||
func TestReJSON(t *testing.T) {
|
||||
|
||||
}
|
||||
312
core/tavern.go
Normal file
312
core/tavern.go
Normal file
@ -0,0 +1,312 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
||||
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/bsonrw"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultMaxMemory = 32 << 10 // 32 KB
|
||||
)
|
||||
|
||||
func writeBsonArr(w io.Writer, src []bson.M) error {
|
||||
return writeBsonDoc(w, bson.M{
|
||||
"r": src,
|
||||
})
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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 {
|
||||
gocommon.RegionStorageConfig `json:",inline"`
|
||||
|
||||
GroupTypes map[string]*groupConfig `json:"tavern_group_types"`
|
||||
MaingateApiToken string `json:"maingate_api_token"`
|
||||
RedisURL string `json:"tavern_redis_url"`
|
||||
macAddr string
|
||||
}
|
||||
|
||||
var config TavernConfig
|
||||
|
||||
type Tavern struct {
|
||||
subTaverns []*subTavern
|
||||
wsh *wshandler.WebsocketHandler
|
||||
}
|
||||
|
||||
type subTavern struct {
|
||||
mongoClient gocommon.MongoClient
|
||||
redisClient *redis.Client
|
||||
wsh *wshandler.WebsocketHandler
|
||||
region string
|
||||
groups map[string]group
|
||||
methods map[string]reflect.Method
|
||||
}
|
||||
|
||||
func getMacAddr() (string, error) {
|
||||
ifas, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
for _, ifa := range ifas {
|
||||
a := ifa.HardwareAddr.String()
|
||||
if a != "" {
|
||||
a = strings.ReplaceAll(a, ":", "")
|
||||
return a, nil
|
||||
}
|
||||
}
|
||||
return "", errors.New("no net interface")
|
||||
}
|
||||
|
||||
// New :
|
||||
func New(context context.Context, wsh *wshandler.WebsocketHandler, inconfig *TavernConfig) (*Tavern, error) {
|
||||
if inconfig == nil {
|
||||
var loaded TavernConfig
|
||||
if err := gocommon.LoadConfig(&loaded); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
inconfig = &loaded
|
||||
}
|
||||
|
||||
config = *inconfig
|
||||
macaddr, err := getMacAddr()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.macAddr = macaddr
|
||||
tv := &Tavern{
|
||||
wsh: wsh,
|
||||
}
|
||||
|
||||
if err = tv.prepare(context); err != nil {
|
||||
logger.Println("tavern prepare() failed :", err)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return tv, nil
|
||||
}
|
||||
|
||||
func (tv *Tavern) Cleanup() {
|
||||
for _, st := range tv.subTaverns {
|
||||
st.mongoClient.Close()
|
||||
}
|
||||
}
|
||||
|
||||
func (tv *Tavern) prepare(ctx context.Context) error {
|
||||
for region, addr := range config.RegionStorage {
|
||||
var dbconn gocommon.MongoClient
|
||||
var err error
|
||||
var groupinstance group
|
||||
|
||||
var tmp *subTavern
|
||||
methods := make(map[string]reflect.Method)
|
||||
tp := reflect.TypeOf(tmp)
|
||||
for i := 0; i < tp.NumMethod(); i++ {
|
||||
method := tp.Method(i)
|
||||
methods[method.Name] = method
|
||||
}
|
||||
|
||||
redisClient, err := gocommon.NewRedisClient(addr.Redis["tavern"])
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
sub := &subTavern{
|
||||
wsh: tv.wsh,
|
||||
mongoClient: dbconn,
|
||||
redisClient: redisClient,
|
||||
region: region,
|
||||
methods: methods,
|
||||
}
|
||||
|
||||
groups := make(map[string]group)
|
||||
for typename, cfg := range config.GroupTypes {
|
||||
cfg.Name = typename
|
||||
if cfg.Transient {
|
||||
groupinstance, err = cfg.prepareInMemory(ctx, typename, sub)
|
||||
//} else {
|
||||
// TODO : db
|
||||
// if !dbconn.Connected() {
|
||||
// dbconn, err = gocommon.NewMongoClient(ctx, url.Mongo, region)
|
||||
// if err != nil {
|
||||
// return err
|
||||
// }
|
||||
// }
|
||||
// groupinstance, err = cfg.preparePersistent(ctx, region, dbconn, tv.wsh)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
groups[typename] = groupinstance
|
||||
}
|
||||
sub.groups = groups
|
||||
|
||||
tv.subTaverns = append(tv.subTaverns, sub)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (tv *Tavern) RegisterHandlers(ctx context.Context, serveMux *http.ServeMux, prefix string) error {
|
||||
for _, sub := range tv.subTaverns {
|
||||
tv.wsh.RegisterReceiver(sub.region, sub)
|
||||
var pattern string
|
||||
if sub.region == "default" {
|
||||
pattern = gocommon.MakeHttpHandlerPattern(prefix, "api")
|
||||
} else {
|
||||
pattern = gocommon.MakeHttpHandlerPattern(prefix, sub.region, "api")
|
||||
}
|
||||
serveMux.HandleFunc(pattern, sub.api)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (sub *subTavern) OnClientMessageReceived(sender *wshandler.Sender, messageType wshandler.WebSocketMessageType, body io.Reader) {
|
||||
if messageType == wshandler.Connected {
|
||||
logger.Println("OnClientMessageReceived : connected ", sender.Accid.Hex())
|
||||
} else if messageType == wshandler.Disconnected {
|
||||
logger.Println("OnClientMessageReceived : disconnected ", sender.Accid.Hex())
|
||||
} else if messageType == wshandler.BinaryMessage {
|
||||
var msg map[string][]any
|
||||
dec := json.NewDecoder(body)
|
||||
if err := dec.Decode(&msg); err == nil {
|
||||
for cmd, args := range msg {
|
||||
switch cmd {
|
||||
case "EnterChannel":
|
||||
sub.wsh.EnterRoom(sub.region, args[0].(string), sender.Accid)
|
||||
|
||||
case "LeaveChannel":
|
||||
sub.wsh.LeaveRoom(sub.region, args[0].(string), sender.Accid)
|
||||
|
||||
case "UpdateGroupMemberDocument":
|
||||
typename := args[0].(string)
|
||||
gidobj, _ := primitive.ObjectIDFromHex(args[1].(string))
|
||||
doc := args[2].(map[string]any)
|
||||
if group := sub.groups[typename]; group != nil {
|
||||
group.UpdateMemberDocument(gidobj, sender.Accid, doc)
|
||||
}
|
||||
|
||||
case "UpdateGroupDocument":
|
||||
typename := args[0].(string)
|
||||
gidobj, _ := primitive.ObjectIDFromHex(args[1].(string))
|
||||
doc := args[2].(map[string]any)
|
||||
if group := sub.groups[typename]; group != nil {
|
||||
group.UpdateGroupDocument(gidobj, doc)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) OnRoomCreated(region, name string) {
|
||||
_, err := sub.redisClient.Persist(context.Background(), name).Result()
|
||||
if err != nil {
|
||||
logger.Println("OnRoomCreate Persist failed :", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) OnRoomDestroyed(region, name string) {
|
||||
_, err := sub.redisClient.Expire(context.Background(), name, 3600*time.Second).Result()
|
||||
if err != nil {
|
||||
logger.Println("OnRoomDestroyed Persist failed :", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (sub *subTavern) api(w http.ResponseWriter, r *http.Request) {
|
||||
defer func() {
|
||||
s := recover()
|
||||
if s != nil {
|
||||
logger.Error(s)
|
||||
}
|
||||
io.Copy(io.Discard, r.Body)
|
||||
r.Body.Close()
|
||||
}()
|
||||
|
||||
// 서버에서 오는 요청만 처리
|
||||
apitoken := r.Header.Get("MG-X-API-TOKEN")
|
||||
if apitoken != config.MaingateApiToken {
|
||||
// 서버가 보내는 쿼리만 허용
|
||||
logger.Println("MG-X-API-TOKEN is missing")
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
operation := r.URL.Query().Get("operation")
|
||||
if len(operation) == 0 {
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
method, ok := sub.methods[operation]
|
||||
if !ok {
|
||||
// 없는 operation
|
||||
logger.Println("fail to call api. operation is not valid :", operation)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if r.PostForm == nil {
|
||||
r.ParseMultipartForm(defaultMaxMemory)
|
||||
}
|
||||
|
||||
args := []reflect.Value{
|
||||
reflect.ValueOf(sub),
|
||||
reflect.ValueOf(w),
|
||||
reflect.ValueOf(r),
|
||||
}
|
||||
|
||||
method.Func.Call(args)
|
||||
}
|
||||
77
core/tavern_test.go
Normal file
77
core/tavern_test.go
Normal file
@ -0,0 +1,77 @@
|
||||
// warroom project main.go
|
||||
package core
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"go.mongodb.org/mongo-driver/bson/primitive"
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
)
|
||||
|
||||
func TestPubSub(t *testing.T) {
|
||||
opt0, _ := redis.ParseURL("redis://192.168.8.94:6380/0")
|
||||
opt1, _ := redis.ParseURL("redis://192.168.8.94:6380/1")
|
||||
|
||||
rc0 := redis.NewClient(opt0)
|
||||
rc1 := redis.NewClient(opt1)
|
||||
|
||||
go func() {
|
||||
time.Sleep(time.Second)
|
||||
rc1.Publish(context.Background(), "__testchan", "real???")
|
||||
fmt.Println("published")
|
||||
}()
|
||||
|
||||
pubsub := rc0.Subscribe(context.Background(), "__testchan")
|
||||
msg, err := pubsub.ReceiveMessage(context.Background())
|
||||
fmt.Println(msg.Payload, err)
|
||||
}
|
||||
|
||||
func TestReJSON(t *testing.T) {
|
||||
rc := redis.NewClient(&redis.Options{Addr: "192.168.8.94:6380"})
|
||||
rh := gocommon.NewRedisonHandler(context.Background(), rc)
|
||||
|
||||
testDoc := map[string]any{
|
||||
"members": map[string]any{
|
||||
"mid2": map[string]any{
|
||||
"key": "val",
|
||||
"exp": 20202020,
|
||||
},
|
||||
"mid1": map[string]any{
|
||||
"key": "val",
|
||||
"exp": 10101010,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
gd := groupDoc{
|
||||
id: primitive.NewObjectID(),
|
||||
}
|
||||
|
||||
midin := primitive.NewObjectID()
|
||||
tid := gd.tid(midin)
|
||||
midout := gd.mid(tid)
|
||||
logger.Println(midin, tid, midout)
|
||||
|
||||
logger.Println(rh.JSONSet("jsontest", "$", testDoc))
|
||||
logger.Println(rh.JSONGet("jsontest", "$"))
|
||||
logger.Println(rh.JSONResp("jsontest", "$.members"))
|
||||
logger.Println(rh.JSONGetString("jsontest", "$.members..key"))
|
||||
logger.Println(rh.JSONGetInt64("jsontest", "$.members..exp"))
|
||||
logger.Println(rh.JSONObjKeys("jsontest", "$.members"))
|
||||
|
||||
err := rh.JSONMSet("jsontest", map[string]any{
|
||||
"$.members.mid1.key": "newval",
|
||||
"$.members.mid2.key": "newval",
|
||||
})
|
||||
logger.Println(err)
|
||||
|
||||
logger.Println(rh.JSONGet("jsontest", "$"))
|
||||
logger.Println(rh.JSONMDel("jsontest", []string{"$.members.mid1", "$.members.mid2"}))
|
||||
logger.Println(rh.JSONGet("jsontest", "$"))
|
||||
logger.Println(rh.JSONObjLen("jsontest", "$.members"))
|
||||
}
|
||||
6
go.mod
6
go.mod
@ -1,12 +1,11 @@
|
||||
module repositories.action2quare.com/ayo/social
|
||||
module repositories.action2quare.com/ayo/tavern
|
||||
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/gorilla/websocket v1.5.0
|
||||
go.mongodb.org/mongo-driver v1.11.7
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230911034515-1af5d7281946
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230719003337-29b2f258507d
|
||||
)
|
||||
|
||||
require (
|
||||
@ -14,6 +13,7 @@ require (
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/go-cmp v0.5.4 // indirect
|
||||
github.com/gorilla/websocket v1.5.0 // indirect
|
||||
github.com/klauspost/compress v1.16.6 // indirect
|
||||
github.com/montanaflynn/stats v0.7.1 // indirect
|
||||
github.com/pires/go-proxyproto v0.7.0 // indirect
|
||||
|
||||
30
go.sum
30
go.sum
@ -104,5 +104,31 @@ gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230911034515-1af5d7281946 h1:YSvgTNuHeKis37+FfOvzVLYCaXQ0oF+CWBTy4bRqq3g=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230911034515-1af5d7281946/go.mod h1:XvklTTSvQX5uviivGBcZo8eIL+mV94W2e4uBBXcT5JY=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230715080833-f0f459332d1a h1:n2FF/GQYtCsi57Lh5m9LyQ2IZQ8pIppscBzhpvugmZg=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230715080833-f0f459332d1a/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230716073702-8f6c87a8aeb8 h1:+wfozysATxEl9NOm03gUF7/kpAr3Chxjn5wjCnJsfQw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230716073702-8f6c87a8aeb8/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230716093911-66aea48fb732 h1:Aq4E8kn1mN5z4ZpRYo5VFj2KektVNrTTuk0HocYMDCk=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230716093911-66aea48fb732/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230717084540-29843802ff0e h1:/eG6tAQzEaN178Aib+/erjHrE/+IjIVLRSmP4gx6D7E=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230717084540-29843802ff0e/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718004527-4b35e0e6386b h1:K3YQXnVP/W6LzwGzqOxwKmFUD5IrrNPEWYcN/fSinck=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718004527-4b35e0e6386b/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718005518-289af24a8ffa h1:YmzJ1YccK3BxC/NbfB11SEUG1S6Lkz6ejg4kS3q/3Qc=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718005518-289af24a8ffa/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718020415-82abcddb497b h1:baO9csa0Esnp7UW+L8zJW/ygpjGHRve4rU2/1pVvXQg=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718020415-82abcddb497b/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718020838-c21017d2cd8b h1:FqLKDrFji0+giFwAJ3oV6dIOR6Sd/aaay76WgWIEVR8=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718020838-c21017d2cd8b/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718032106-40a603522d40 h1:VyFfS0d6pTX2HbZoDHOxJwag4aVSLOh/LrQXqfSJLBg=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718032106-40a603522d40/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718083804-d724cc84fa94 h1:iQPrRcZ6XfFblpVHxe/CIoWyTj7imF+3edIGSX6ZMM8=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718083804-d724cc84fa94/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718084512-89fa9e4ac585 h1:Wy6qjZ0uHfp02/H688zotRfzYGRPjun7Qay0Z9B/hSg=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718084512-89fa9e4ac585/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718105124-72a683fed2c0 h1:8LmRo2nKaLi4QCmO/agSpNTmCD0EdwFycjHjOweQJp8=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230718105124-72a683fed2c0/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230719003101-256bfd030c29 h1:ADScrqJgmk/TfyOu/6oXD3WkSH8sh3Bw360O8GKuEV8=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230719003101-256bfd030c29/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230719003337-29b2f258507d h1:eMzrvVkQfbs5X5dcw80TGGKtJ+6XELl7zNsWiuq4gzs=
|
||||
repositories.action2quare.com/ayo/gocommon v0.0.0-20230719003337-29b2f258507d/go.mod h1:PdpZ16O1czKKxCxn+0AFNaEX/0kssYwC3G8jR0V7ybw=
|
||||
|
||||
27
main.go
27
main.go
@ -1,3 +1,4 @@
|
||||
// warroom project main.go
|
||||
package main
|
||||
|
||||
import (
|
||||
@ -6,11 +7,10 @@ import (
|
||||
|
||||
"repositories.action2quare.com/ayo/gocommon/flagx"
|
||||
"repositories.action2quare.com/ayo/gocommon/wshandler"
|
||||
"repositories.action2quare.com/ayo/social/core"
|
||||
"repositories.action2quare.com/ayo/tavern/core"
|
||||
|
||||
"repositories.action2quare.com/ayo/gocommon"
|
||||
common "repositories.action2quare.com/ayo/gocommon"
|
||||
"repositories.action2quare.com/ayo/gocommon/logger"
|
||||
"repositories.action2quare.com/ayo/gocommon/session"
|
||||
)
|
||||
|
||||
var prefix = flagx.String("prefix", "", "")
|
||||
@ -19,33 +19,28 @@ func main() {
|
||||
flagx.Parse()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
var config core.SocialConfig
|
||||
if err := gocommon.LoadConfig(&config); err != nil {
|
||||
var config core.TavernConfig
|
||||
if err := common.LoadConfig(&config); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
consumer, err := session.NewConsumerWithConfig(ctx, config.SessionConfig)
|
||||
wsh, err := wshandler.NewWebsocketHandler()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
wsh, err := wshandler.NewWebsocketHandler(consumer, config.RedisURL)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
if so, err := core.New(ctx, wsh, &config); err != nil {
|
||||
if tv, err := core.New(ctx, wsh, &config); err != nil {
|
||||
panic(err)
|
||||
} else {
|
||||
serveMux := http.NewServeMux()
|
||||
wsh.RegisterHandlers(serveMux, *prefix)
|
||||
so.RegisterHandlers(ctx, serveMux, *prefix)
|
||||
server := gocommon.NewHTTPServer(serveMux)
|
||||
logger.Println("social is started")
|
||||
tv.RegisterHandlers(ctx, serveMux, *prefix)
|
||||
server := common.NewHTTPServer(serveMux)
|
||||
logger.Println("tavern is started")
|
||||
wsh.Start(ctx)
|
||||
server.Start()
|
||||
cancel()
|
||||
so.Cleanup()
|
||||
tv.Cleanup()
|
||||
wsh.Cleanup()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user