Browse Source

优化项目初始化配置

hsun 2 years ago
parent
commit
a0b8fe3f1b
6 changed files with 88 additions and 56 deletions
  1. 36 43
      core/config.go
  2. 40 0
      global/global.go
  3. 0 1
      services/wechat/template_msg.go
  4. 1 3
      services/wechat/wechat.go
  5. 6 6
      services/wx_app/wx_app.go
  6. 5 3
      utils/directory.go

+ 36 - 43
core/config.go

@@ -1,45 +1,38 @@
 package core
 
-import (
-	"fmt"
-	"github.com/fsnotify/fsnotify"
-	"github.com/spf13/viper"
-	"hongze/hongze_yb/global"
-	"hongze/hongze_yb/utils"
-)
-
-const ConfigFile = "config/config.yaml"                                //本地(测试)环境下的配置文件地址
-const ProConfigFile = "/home/code/config/hongze_yb/config/config.yaml" //生产环境下的配置文件地址
-
-func init() {
-	v := viper.New()
-
-	configFilePath := ConfigFile
-
-	//如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
-	if !utils.FileIsExist(configFilePath) {
-		configFilePath = ProConfigFile
-	}
-
-	//设置配置文件
-	v.SetConfigFile(configFilePath)
-
-	err := v.ReadInConfig()
-	if err != nil {
-		panic(fmt.Errorf("读取配置失败,Err: %s \n", err))
-	}
-
-	//持续监听文件
-	v.WatchConfig()
-
-	v.OnConfigChange(func(e fsnotify.Event) {
-		fmt.Println("配置文件变更:", e.Name)
-		if err := v.Unmarshal(&global.CONFIG); err != nil {
-			fmt.Println("配置重赋值失败,Err:", err)
-		}
-		fmt.Println(global.CONFIG)
-	})
-	if err := v.Unmarshal(&global.CONFIG); err != nil {
-		fmt.Println("配置初始化赋值失败,Err:", err)
-	}
-}
+//
+//const ConfigFile = "config/config.yaml"                                //本地(测试)环境下的配置文件地址
+//const ProConfigFile = "/home/code/config/hongze_yb/config/config.yaml" //生产环境下的配置文件地址
+//
+//func init() {
+//	v := viper.New()
+//
+//	configFilePath := ConfigFile
+//
+//	//如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
+//	if !utils.FileIsExist(configFilePath) {
+//		configFilePath = ProConfigFile
+//	}
+//
+//	//设置配置文件
+//	v.SetConfigFile(configFilePath)
+//
+//	err := v.ReadInConfig()
+//	if err != nil {
+//		panic(fmt.Errorf("读取配置失败,Err: %s \n", err))
+//	}
+//
+//	//持续监听文件
+//	v.WatchConfig()
+//
+//	v.OnConfigChange(func(e fsnotify.Event) {
+//		fmt.Println("配置文件变更:", e.Name)
+//		if err := v.Unmarshal(&global.CONFIG); err != nil {
+//			fmt.Println("配置重赋值失败,Err:", err)
+//		}
+//		fmt.Println(global.CONFIG)
+//	})
+//	if err := v.Unmarshal(&global.CONFIG); err != nil {
+//		fmt.Println("配置初始化赋值失败,Err:", err)
+//	}
+//}

+ 40 - 0
global/global.go

@@ -1,11 +1,15 @@
 package global
 
 import (
+	"fmt"
+	"github.com/fsnotify/fsnotify"
 	"github.com/go-redis/redis/v8"
 	"github.com/olivere/elastic/v7"
 	oplogging "github.com/op/go-logging"
+	"github.com/spf13/viper"
 	"gorm.io/gorm"
 	"hongze/hongze_yb/config"
+	"hongze/hongze_yb/utils"
 	"io"
 )
 
@@ -18,3 +22,39 @@ var (
 	Redis         *redis.Client //redis链接
 	EsClient      *elastic.Client
 )
+
+const ConfigFile = "config/config.yaml"                                //本地(测试)环境下的配置文件地址
+const ProConfigFile = "/home/code/config/hongze_yb/config/config.yaml" //生产环境下的配置文件地址
+
+func init() {
+	v := viper.New()
+
+	configFilePath := ConfigFile
+
+	//如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
+	if !utils.FileIsExist(configFilePath) {
+		configFilePath = ProConfigFile
+	}
+
+	//设置配置文件
+	v.SetConfigFile(configFilePath)
+
+	err := v.ReadInConfig()
+	if err != nil {
+		panic(fmt.Errorf("读取配置失败,Err: %s \n", err))
+	}
+
+	//持续监听文件
+	v.WatchConfig()
+
+	v.OnConfigChange(func(e fsnotify.Event) {
+		fmt.Println("配置文件变更:", e.Name)
+		if err := v.Unmarshal(&CONFIG); err != nil {
+			fmt.Println("配置重赋值失败,Err:", err)
+		}
+		fmt.Println(CONFIG)
+	})
+	if err := v.Unmarshal(&CONFIG); err != nil {
+		fmt.Println("配置初始化赋值失败,Err:", err)
+	}
+}

+ 0 - 1
services/wechat/template_msg.go

@@ -185,7 +185,6 @@ func SendQuestionReplyWxMsg(questionId, userId int, openid, questionTitle string
 			go alarm_msg.SendAlarmMsg(alarmMsg, 3)
 		}
 	}()
-	initConf()
 
 	openIdList := make([]*OpenIdList, 0)
 	openIdList = append(openIdList, &OpenIdList{

+ 1 - 3
services/wechat/wechat.go

@@ -22,7 +22,7 @@ var (
 	WxYbAppId                       string // 研报小程序AppID
 )
 
-func initConf() {
+func init() {
 	WxYbAppId = "wxb059c872d79b9967"
 	if global.CONFIG.Serve.RunMode == "debug" {
 		WxAppId = "wx9b5d7291e581233a"
@@ -38,8 +38,6 @@ func initConf() {
 }
 
 func GetWxChat() (officialAccount *officialaccount.OfficialAccount) {
-	initConf() //初始化参数
-
 	wc := wechat.NewWechat()
 	memory := cache.NewMemory()
 	conf := &config.Config{

+ 6 - 6
services/wx_app/wx_app.go

@@ -26,6 +26,11 @@ func init() {
 	WxId = `gh_75abb562a946`
 	WxAppSecret = `1737c73e9f69a21de1a345b8f0800258`
 	WxPlatform = 6 //弘则研报来源
+	if global.CONFIG.Serve.RunMode == "release" {
+		EnvVersion = "release"
+	} else {
+		EnvVersion = "trial"
+	}
 }
 
 func GetWxApp() (miniprogram *miniprogram.MiniProgram) {
@@ -69,15 +74,10 @@ func GetDecryptInfo(sessionKey, encryptedData, iv string) (decryptData *encrypto
 
 // GetSunCode 获取太阳码
 func GetSunCode(page, scene string) (resp []byte, err error) {
-	// 此处因初始化顺序问题不放在init中
-	env := "trial"
-	if global.CONFIG.Serve.RunMode == "release" {
-		env = "release"
-	}
 	codePars := qrcode.QRCoder{
 		Page:       page,
 		Scene:      scene,
-		EnvVersion: env,
+		EnvVersion: EnvVersion,
 	}
 	wechatClient := GetWxApp()
 	qr := wechatClient.GetQRCode()

+ 5 - 3
utils/directory.go

@@ -1,7 +1,7 @@
 package utils
 
 import (
-	"hongze/hongze_yb/global"
+	"fmt"
 	"os"
 )
 
@@ -35,10 +35,12 @@ func CreateDir(dirs ...string) (err error) {
 			return err
 		}
 		if !exist {
-			global.LOG.Debug("create directory ", v)
+			fmt.Println("create directory ", v)
+			//global.LOG.Debug("create directory ", v)
 			err = os.MkdirAll(v, os.ModePerm)
 			if err != nil {
-				global.LOG.Error("create directory", v, " error:", err)
+				fmt.Println("create directory", v, " error:", err)
+				//global.LOG.Error("create directory", v, " error:", err)
 			}
 		}
 	}