feat: add watch config

main
zggsong 2 years ago
parent f32ba74612
commit 0b1afe6283

@ -1,8 +1,11 @@
package config package config
import ( import (
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"github.com/spf13/viper" "github.com/spf13/viper"
"luxshare-daily-report/global"
"luxshare-daily-report/model"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -13,7 +16,7 @@ import (
// @Description: 初始化配置文件 // @Description: 初始化配置文件
// @return Config // @return Config
// //
func InitialConfig() Config { func InitialConfig() model.Config {
workPath, _ := os.Executable() workPath, _ := os.Executable()
filePath := path.Dir(workPath) filePath := path.Dir(workPath)
filePath = filepath.Join(filePath, "/config/.yml") filePath = filepath.Join(filePath, "/config/.yml")
@ -22,24 +25,22 @@ func InitialConfig() Config {
viper.AddConfigPath("./config") viper.AddConfigPath("./config")
viper.AddConfigPath("../config") viper.AddConfigPath("../config")
viper.AddConfigPath(filePath) 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() err := viper.ReadInConfig()
if err != nil { if err != nil {
log.Println("未找到配置,请先添加配置!!!") log.Println("[ERROR] 未找到配置,请先添加配置")
os.Exit(1) os.Exit(1)
} }
log.Printf("[INFO] Loaded Config Success...") log.Printf("[INFO] Loaded Config Success...")
return getConfig() return getConfig()
} }
type Config struct { func getConfig() model.Config {
Quality string var config model.Config
UserName string
PassWord string
BarkUrl string
}
func getConfig() Config {
var config Config
config.Quality = viper.GetString("img.quality") config.Quality = viper.GetString("img.quality")
config.UserName = viper.GetString("info.username") config.UserName = viper.GetString("info.username")
config.PassWord = viper.GetString("info.password") config.PassWord = viper.GetString("info.password")

@ -1,8 +1,11 @@
package global package global
import "luxshare-daily-report/config" import (
"luxshare-daily-report/model"
)
var ( var (
GLO_CONFIG config.Config GLO_CONFIG model.Config
GLO_RECV_CHAN chan map[string]string GLO_CONFIG_CHAN chan model.Config
GLO_RECV_CHAN chan map[string]string
) )

@ -3,6 +3,7 @@ package main
import ( import (
"luxshare-daily-report/config" "luxshare-daily-report/config"
"luxshare-daily-report/global" "luxshare-daily-report/global"
"luxshare-daily-report/model"
"luxshare-daily-report/serve" "luxshare-daily-report/serve"
"net/http" "net/http"
"os" "os"
@ -46,6 +47,7 @@ func main() {
// 监听目录下文件 // 监听目录下文件
//go util.ListeningDirectory("upload") //go util.ListeningDirectory("upload")
global.GLO_RECV_CHAN = make(chan map[string]string) global.GLO_RECV_CHAN = make(chan map[string]string)
global.GLO_CONFIG_CHAN = make(chan model.Config)
mux := http.NewServeMux() mux := http.NewServeMux()
mux.HandleFunc("/", serve.HandlerRoot) mux.HandleFunc("/", serve.HandlerRoot)
@ -68,6 +70,8 @@ func main() {
func chanHandler() { func chanHandler() {
for { for {
select { select {
case newConf := <-global.GLO_CONFIG_CHAN:
global.GLO_CONFIG = newConf
case result := <-global.GLO_RECV_CHAN: case result := <-global.GLO_RECV_CHAN:
for _, v := range result { for _, v := range result {
serve.CompressImageResource(v) serve.CompressImageResource(v)

@ -69,3 +69,14 @@ type BarkResp struct {
Message string `json:"message"` Message string `json:"message"`
Timestamp int `json:"timestamp"` Timestamp int `json:"timestamp"`
} }
//
// Config
// @Description: config model
//
type Config struct {
Quality string
UserName string
PassWord string
BarkUrl string
}

Loading…
Cancel
Save