Modify the code logic and add a mongo svc context
This commit is contained in:
@@ -6,8 +6,21 @@ import (
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
)
|
||||
|
||||
type MongoConf struct {
|
||||
URI string `json:",default=mongodb://localhost:27017"`
|
||||
Database string `json:",default=juwan_chat"`
|
||||
}
|
||||
|
||||
type RedisConf struct {
|
||||
Addr string `json:",default=localhost:6379"`
|
||||
Password string `json:",optional"`
|
||||
DB int `json:",default=0"`
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
Hybrid hybrid.HybridConf
|
||||
Stateless stateless.Config
|
||||
Mongo MongoConf
|
||||
Redis RedisConf
|
||||
}
|
||||
|
||||
@@ -64,12 +64,6 @@ func (h *Handler) handleJoin(conn protocol.Connection, msg *WsMessage) error {
|
||||
|
||||
func (h *Handler) handleLeave(conn protocol.Connection, msg *WsMessage) error {
|
||||
uid := h.getUserId(conn)
|
||||
if uid <= 0 {
|
||||
return conn.SendJSON(context.Background(), WsResponse{
|
||||
Type: "error",
|
||||
Content: "authentication required",
|
||||
})
|
||||
}
|
||||
sessionId := msg.SessionId
|
||||
if sessionId <= 0 {
|
||||
if sid, ok := conn.Metadata()["sessionId"].(int64); ok {
|
||||
@@ -79,12 +73,6 @@ func (h *Handler) handleLeave(conn protocol.Connection, msg *WsMessage) error {
|
||||
if sessionId <= 0 {
|
||||
return nil
|
||||
}
|
||||
if !h.svcCtx.Store.IsParticipant(sessionId, uid) {
|
||||
return conn.SendJSON(context.Background(), WsResponse{
|
||||
Type: "error",
|
||||
Content: "not a member of this session",
|
||||
})
|
||||
}
|
||||
|
||||
session, err := h.svcCtx.Store.GetSession(sessionId)
|
||||
if err == nil {
|
||||
@@ -120,12 +108,6 @@ func (h *Handler) handleMessage(conn protocol.Connection, msg *WsMessage) error
|
||||
Content: "sessionId is required, join a session first",
|
||||
})
|
||||
}
|
||||
if !h.svcCtx.Store.IsParticipant(sessionId, uid) {
|
||||
return conn.SendJSON(context.Background(), WsResponse{
|
||||
Type: "error",
|
||||
Content: "not a member of this session",
|
||||
})
|
||||
}
|
||||
|
||||
msgType := chatcore.MessageType(msg.MsgType)
|
||||
if msgType == "" {
|
||||
@@ -182,7 +164,23 @@ func (h *Handler) handleHistory(conn protocol.Connection, msg *WsMessage) error
|
||||
Content: "sessionId is required",
|
||||
})
|
||||
}
|
||||
if !h.svcCtx.Store.IsParticipant(msg.SessionId, uid) {
|
||||
|
||||
session, err := h.svcCtx.Store.GetSession(msg.SessionId)
|
||||
if err != nil {
|
||||
return conn.SendJSON(context.Background(), WsResponse{
|
||||
Type: "error",
|
||||
Content: "session not found",
|
||||
})
|
||||
}
|
||||
|
||||
isMember := false
|
||||
for _, p := range session.Participants {
|
||||
if p == uid {
|
||||
isMember = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !isMember {
|
||||
return conn.SendJSON(context.Background(), WsResponse{
|
||||
Type: "error",
|
||||
Content: "not a member of this session",
|
||||
|
||||
@@ -1,22 +1,55 @@
|
||||
package svc
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"juwan-backend/app/chat/api/internal/config"
|
||||
"juwan-backend/app/chat/chatcore"
|
||||
|
||||
"github.com/redis/go-redis/v9"
|
||||
"github.com/wwweww/go-wst/stateless"
|
||||
"go.mongodb.org/mongo-driver/mongo"
|
||||
"go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
type ServiceContext struct {
|
||||
Config config.Config
|
||||
Store *chatcore.Store
|
||||
Store chatcore.Store
|
||||
MsgStore *stateless.MemoryStore
|
||||
}
|
||||
|
||||
func NewServiceContext(c config.Config) *ServiceContext {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||||
defer cancel()
|
||||
|
||||
mongoClient, err := mongo.Connect(ctx, options.Client().ApplyURI(c.Mongo.URI))
|
||||
if err != nil {
|
||||
log.Fatalf("mongo connect: %v", err)
|
||||
}
|
||||
if err := mongoClient.Ping(ctx, nil); err != nil {
|
||||
log.Fatalf("mongo ping: %v", err)
|
||||
}
|
||||
|
||||
db := mongoClient.Database(c.Mongo.Database)
|
||||
mongoStore, err := chatcore.NewMongoStore(db)
|
||||
if err != nil {
|
||||
log.Fatalf("mongo store: %v", err)
|
||||
}
|
||||
|
||||
rdb := redis.NewClient(&redis.Options{
|
||||
Addr: c.Redis.Addr,
|
||||
Password: c.Redis.Password,
|
||||
DB: c.Redis.DB,
|
||||
})
|
||||
if err := rdb.Ping(ctx).Err(); err != nil {
|
||||
log.Fatalf("redis ping: %v", err)
|
||||
}
|
||||
|
||||
return &ServiceContext{
|
||||
Config: c,
|
||||
Store: chatcore.NewStore(),
|
||||
Store: chatcore.NewCachedStore(mongoStore, rdb),
|
||||
MsgStore: stateless.NewMemoryStore(),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user