diff --git a/db/mongodb/mongodb.go b/db/mongodb/mongodb.go index 9c8449d..35ea702 100644 --- a/db/mongodb/mongodb.go +++ b/db/mongodb/mongodb.go @@ -10,15 +10,21 @@ import ( "gopkg.in/yaml.v3" "github.com/Mrs4s/go-cqhttp/db" - "github.com/Mrs4s/go-cqhttp/modules/config" ) -type MongoDBImpl struct { +type database struct { uri string db string mongo *mongo.Database } +// config mongodb 相关配置 +type config struct { + Enable bool `yaml:"enable"` + URI string `yaml:"uri"` + Database string `yaml:"database"` +} + const ( MongoGroupMessageCollection = "group-messages" MongoPrivateMessageCollection = "private-messages" @@ -26,8 +32,8 @@ const ( ) func init() { - db.Register("mongodb", func(node yaml.Node) db.Database { - conf := new(config.MongoDBConfig) + db.Register("database", func(node yaml.Node) db.Database { + conf := new(config) _ = node.Decode(conf) if conf.Database == "" { conf.Database = "gocq-database" @@ -35,11 +41,11 @@ func init() { if !conf.Enable { return nil } - return &MongoDBImpl{uri: conf.URI, db: conf.Database} + return &database{uri: conf.URI, db: conf.Database} }) } -func (m *MongoDBImpl) Open() error { +func (m *database) Open() error { cli, err := mongo.Connect(context.Background(), options.Client().ApplyURI(m.uri)) if err != nil { return errors.Wrap(err, "open mongo connection error") @@ -48,14 +54,14 @@ func (m *MongoDBImpl) Open() error { return nil } -func (m *MongoDBImpl) GetMessageByGlobalID(id int32) (db.StoredMessage, error) { +func (m *database) GetMessageByGlobalID(id int32) (db.StoredMessage, error) { if r, err := m.GetGroupMessageByGlobalID(id); err == nil { return r, nil } return m.GetPrivateMessageByGlobalID(id) } -func (m *MongoDBImpl) GetGroupMessageByGlobalID(id int32) (*db.StoredGroupMessage, error) { +func (m *database) GetGroupMessageByGlobalID(id int32) (*db.StoredGroupMessage, error) { coll := m.mongo.Collection(MongoGroupMessageCollection) var ret db.StoredGroupMessage if err := coll.FindOne(context.Background(), bson.D{{"globalId", id}}).Decode(&ret); err != nil { @@ -64,7 +70,7 @@ func (m *MongoDBImpl) GetGroupMessageByGlobalID(id int32) (*db.StoredGroupMessag return &ret, nil } -func (m *MongoDBImpl) GetPrivateMessageByGlobalID(id int32) (*db.StoredPrivateMessage, error) { +func (m *database) GetPrivateMessageByGlobalID(id int32) (*db.StoredPrivateMessage, error) { coll := m.mongo.Collection(MongoPrivateMessageCollection) var ret db.StoredPrivateMessage if err := coll.FindOne(context.Background(), bson.D{{"globalId", id}}).Decode(&ret); err != nil { @@ -73,7 +79,7 @@ func (m *MongoDBImpl) GetPrivateMessageByGlobalID(id int32) (*db.StoredPrivateMe return &ret, nil } -func (m *MongoDBImpl) GetGuildChannelMessageByID(id string) (*db.StoredGuildChannelMessage, error) { +func (m *database) GetGuildChannelMessageByID(id string) (*db.StoredGuildChannelMessage, error) { coll := m.mongo.Collection(MongoGuildChannelMessageCollection) var ret db.StoredGuildChannelMessage if err := coll.FindOne(context.Background(), bson.D{{"_id", id}}).Decode(&ret); err != nil { @@ -82,19 +88,19 @@ func (m *MongoDBImpl) GetGuildChannelMessageByID(id string) (*db.StoredGuildChan return &ret, nil } -func (m *MongoDBImpl) InsertGroupMessage(msg *db.StoredGroupMessage) error { +func (m *database) InsertGroupMessage(msg *db.StoredGroupMessage) error { coll := m.mongo.Collection(MongoGroupMessageCollection) _, err := coll.UpdateOne(context.Background(), bson.D{{"_id", msg.ID}}, bson.D{{"$set", msg}}, options.Update().SetUpsert(true)) return errors.Wrap(err, "insert error") } -func (m *MongoDBImpl) InsertPrivateMessage(msg *db.StoredPrivateMessage) error { +func (m *database) InsertPrivateMessage(msg *db.StoredPrivateMessage) error { coll := m.mongo.Collection(MongoPrivateMessageCollection) _, err := coll.UpdateOne(context.Background(), bson.D{{"_id", msg.ID}}, bson.D{{"$set", msg}}, options.Update().SetUpsert(true)) return errors.Wrap(err, "insert error") } -func (m *MongoDBImpl) InsertGuildChannelMessage(msg *db.StoredGuildChannelMessage) error { +func (m *database) InsertGuildChannelMessage(msg *db.StoredGuildChannelMessage) error { coll := m.mongo.Collection(MongoGuildChannelMessageCollection) _, err := coll.UpdateOne(context.Background(), bson.D{{"_id", msg.ID}}, bson.D{{"$set", msg}}, options.Update().SetUpsert(true)) return errors.Wrap(err, "insert error") diff --git a/go.mod b/go.mod index 8ba6cc2..28b155c 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,7 @@ go 1.18 require ( github.com/Microsoft/go-winio v0.5.1 - github.com/Mrs4s/MiraiGo v0.0.0-20220302134146-348e317a3400 + github.com/Mrs4s/MiraiGo v0.0.0-20220317085721-6d84141b8dd3 github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc github.com/fumiama/go-hide-param v0.1.4 github.com/gabriel-vasile/mimetype v1.4.0 diff --git a/go.sum b/go.sum index d60cf46..1a7f972 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/Microsoft/go-winio v0.5.1 h1:aPJp2QD7OOrhO5tQXqQoGSJc+DjDtWTGLOmNyAm6FgY= github.com/Microsoft/go-winio v0.5.1/go.mod h1:JPGBdM1cNvN/6ISo+n8V5iA4v8pBzdOpzfwIujj1a84= -github.com/Mrs4s/MiraiGo v0.0.0-20220302134146-348e317a3400 h1:5e0KDN118/RKItDmUcJyCkyyK/EGItYYJ5TVX/jb6xM= -github.com/Mrs4s/MiraiGo v0.0.0-20220302134146-348e317a3400/go.mod h1:qJWkRO5vry/sUHthX5kh6go2llYIVAJ+Mq8p+N/FW+8= +github.com/Mrs4s/MiraiGo v0.0.0-20220317085721-6d84141b8dd3 h1:U3UumMt052Ii1gGrkKM1MbX1uxCzxKhlfuNQ42LtIRQ= +github.com/Mrs4s/MiraiGo v0.0.0-20220317085721-6d84141b8dd3/go.mod h1:qJWkRO5vry/sUHthX5kh6go2llYIVAJ+Mq8p+N/FW+8= github.com/RomiChan/protobuf v0.0.0-20220227114948-643565fff248 h1:1jRB6xuBKwfgZrg0bA7XJin0VeNwG9iJKx9RXwDobt4= github.com/RomiChan/protobuf v0.0.0-20220227114948-643565fff248/go.mod h1:CKKOWC7mBxd36zxsCB1V8DTrwlTNRQvkSVbYqyUiGEE= github.com/RomiChan/websocket v1.4.3-0.20220123145318-307a86b127bc h1:AAx50/fb/xS4lvsdQg+bFbGvqSDhyV1MF+p2PLCamZ0= diff --git a/modules/config/config.go b/modules/config/config.go index 1386aa6..9c3c65e 100644 --- a/modules/config/config.go +++ b/modules/config/config.go @@ -8,7 +8,6 @@ import ( "os" "regexp" "strings" - "sync" log "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" @@ -75,13 +74,6 @@ type Server struct { Default string } -// MongoDBConfig mongodb 相关配置 -type MongoDBConfig struct { - Enable bool `yaml:"enable"` - URI string `yaml:"uri"` - Database string `yaml:"database"` -} - // Parse 从默认配置文件路径中获取 func Parse(path string) *Config { file, err := os.ReadFile(path) @@ -98,16 +90,11 @@ func Parse(path string) *Config { return config } -var ( - serverconfs []*Server - mu sync.Mutex -) +var serverconfs []*Server // AddServer 添加该服务的简介和默认配置 func AddServer(s *Server) { - mu.Lock() serverconfs = append(serverconfs, s) - mu.Unlock() } // generateConfig 生成配置文件 diff --git a/server/scf.go b/server/scf.go index 29546ae..f41b359 100644 --- a/server/scf.go +++ b/server/scf.go @@ -190,9 +190,9 @@ func (c *lambdaClient) next() *http.Request { if resp.StatusCode != http.StatusOK { return nil } - req := new(http.Request) - invoke := new(lambdaInvoke) - _ = json.NewDecoder(resp.Body).Decode(invoke) + var req http.Request + var invoke lambdaInvoke + _ = json.NewDecoder(resp.Body).Decode(&invoke) if invoke.HTTPMethod == "" { // 不是 api 网关 return nil } @@ -211,5 +211,5 @@ func (c *lambdaClient) next() *http.Request { query[k] = []string{v} } req.URL.RawQuery = query.Encode() - return req + return &req }