feat: add watch config

main
zggsong 2 years ago
parent f32ba74612
commit 0b1afe6283

@ -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")

@ -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
)

@ -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)

@ -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
}

Loading…
Cancel
Save