diff --git a/config/config.go b/config/config.go index c2a8859..4f2c1c4 100644 --- a/config/config.go +++ b/config/config.go @@ -1,8 +1,11 @@ package config import ( + "github.com/fsnotify/fsnotify" log "github.com/sirupsen/logrus" "github.com/spf13/viper" + "luxshare-daily-report/global" + "luxshare-daily-report/model" "os" "path" "path/filepath" @@ -13,7 +16,7 @@ import ( // @Description: 初始化配置文件 // @return Config // -func InitialConfig() Config { +func InitialConfig() model.Config { workPath, _ := os.Executable() filePath := path.Dir(workPath) filePath = filepath.Join(filePath, "/config/.yml") @@ -22,24 +25,22 @@ func InitialConfig() Config { viper.AddConfigPath("./config") viper.AddConfigPath("../config") viper.AddConfigPath(filePath) + viper.OnConfigChange(func(e fsnotify.Event) { + log.Printf("[INFO] 配置文件更新: %v", e.Name) + global.GLO_CONFIG_CHAN <- getConfig() + }) + viper.WatchConfig() err := viper.ReadInConfig() if err != nil { - log.Println("未找到配置,请先添加配置!!!") + log.Println("[ERROR] 未找到配置,请先添加配置") os.Exit(1) } log.Printf("[INFO] Loaded Config Success...") return getConfig() } -type Config struct { - Quality string - UserName string - PassWord string - BarkUrl string -} - -func getConfig() Config { - var config Config +func getConfig() model.Config { + var config model.Config config.Quality = viper.GetString("img.quality") config.UserName = viper.GetString("info.username") config.PassWord = viper.GetString("info.password") diff --git a/global/global.go b/global/global.go index 04a2363..386195a 100644 --- a/global/global.go +++ b/global/global.go @@ -1,8 +1,11 @@ package global -import "luxshare-daily-report/config" +import ( + "luxshare-daily-report/model" +) var ( - GLO_CONFIG config.Config - GLO_RECV_CHAN chan map[string]string + GLO_CONFIG model.Config + GLO_CONFIG_CHAN chan model.Config + GLO_RECV_CHAN chan map[string]string ) diff --git a/main.go b/main.go index d4b0406..2f96b3f 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "luxshare-daily-report/config" "luxshare-daily-report/global" + "luxshare-daily-report/model" "luxshare-daily-report/serve" "net/http" "os" @@ -46,6 +47,7 @@ func main() { // 监听目录下文件 //go util.ListeningDirectory("upload") global.GLO_RECV_CHAN = make(chan map[string]string) + global.GLO_CONFIG_CHAN = make(chan model.Config) mux := http.NewServeMux() mux.HandleFunc("/", serve.HandlerRoot) @@ -68,6 +70,8 @@ func main() { func chanHandler() { for { select { + case newConf := <-global.GLO_CONFIG_CHAN: + global.GLO_CONFIG = newConf case result := <-global.GLO_RECV_CHAN: for _, v := range result { serve.CompressImageResource(v) diff --git a/model/model.go b/model/model.go index bd43f02..21f82b7 100644 --- a/model/model.go +++ b/model/model.go @@ -69,3 +69,14 @@ type BarkResp struct { Message string `json:"message"` Timestamp int `json:"timestamp"` } + +// +// Config +// @Description: config model +// +type Config struct { + Quality string + UserName string + PassWord string + BarkUrl string +}