Skip to content

Commit

Permalink
fix : 解决了config中Init未更新全局变量错误
Browse files Browse the repository at this point in the history
  • Loading branch information
universero committed Aug 10, 2024
1 parent 49ce896 commit fcc7f4a
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 7 deletions.
12 changes: 8 additions & 4 deletions common/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package config

import (
"fmt"
"github.com/zeromicro/go-zero/core/stores/cache"
"os"

Expand All @@ -13,29 +14,32 @@ var config *Config
type Config struct {
service.ServiceConf
ListenOn string
Cache *cache.CacheConf
Cache cache.CacheConf
Mongo struct {
URL string
DB string
}
}

func Init() {
c := new(Config)
config = new(Config)
path := os.Getenv("CONFIG_PATH")
if path == "" {
path = "etc/config.yaml"
}
err := conf.Load(path, c)
err := conf.Load(path, config)
if err != nil {
panic(err)
}
err = c.SetUp()
err = config.SetUp()
if err != nil {
panic(err)
}
}

func Get() *Config {
if config == nil {
fmt.Println("config is nil")
}
return config
}
2 changes: 1 addition & 1 deletion infra/mapper/follow/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type MongoMapper struct {

func NewMongoMapper() IMongoMapper {
aConfig := config.Get()
conn := monc.MustNewModel(aConfig.Mongo.URL, aConfig.Mongo.DB, CollectionName, *aConfig.Cache)
conn := monc.MustNewModel(aConfig.Mongo.URL, aConfig.Mongo.DB, CollectionName, aConfig.Cache)
return &MongoMapper{
conn: conn,
}
Expand Down
2 changes: 1 addition & 1 deletion infra/mapper/like/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type MongoMapper struct {

func NewMongoMapper() IMongoMapper {
aConfig := config.Get()
conn := monc.MustNewModel(aConfig.Mongo.URL, aConfig.Mongo.DB, CollectionName, *aConfig.Cache)
conn := monc.MustNewModel(aConfig.Mongo.URL, aConfig.Mongo.DB, CollectionName, aConfig.Cache)
return &MongoMapper{
conn: conn,
}
Expand Down
2 changes: 1 addition & 1 deletion infra/mapper/share/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ type MongoMapper struct {

func NewMongoMapper() IMongoMapper {
aConfig := config.Get()
conn := monc.MustNewModel(aConfig.Mongo.URL, aConfig.Mongo.DB, CollectionName, *aConfig.Cache)
conn := monc.MustNewModel(aConfig.Mongo.URL, aConfig.Mongo.DB, CollectionName, aConfig.Cache)
return &MongoMapper{
conn: conn,
}
Expand Down

0 comments on commit fcc7f4a

Please sign in to comment.