1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- package global
- import (
- "eta/mysteel_watch/config"
- "eta/mysteel_watch/utils"
- "fmt"
- "github.com/fsnotify/fsnotify"
- oplogging "github.com/op/go-logging"
- "github.com/patrickmn/go-cache"
- "github.com/spf13/viper"
- "io/fs"
- "os"
- "path/filepath"
- "syscall"
- "time"
- )
- var (
- CONFIG config.Config //配置文件
- LOG *oplogging.Logger
- CacheClient *cache.Cache
- )
- const ConfigFile = "config/config.yaml" //本地(测试)环境下的配置文件地址
- const ProConfigFile = "config/config.yaml" //生产环境下的配置文件地址
- func init() {
- v := viper.New()
- configFilePath := ConfigFile
- //如果不存在该配置文件,那么应该是线上环境,那么去寻找线上配置文件的路径
- if !utils.FileIsExist(configFilePath) {
- configFilePath = ProConfigFile
- }
- fmt.Println("configFilePath->", configFilePath)
- //设置配置文件
- 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)
- }
- CacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
- ReadDefaultFileModifyTime()
- }
- func ReadDefaultFileModifyTime() {
- cacheClient := CacheClient
- if cacheClient == nil {
- cacheClient = cache.New(365*24*time.Hour, 365*24*time.Hour)
- }
- err := filepath.Walk(CONFIG.Serve.IndexSaveDir, func(path string, info fs.FileInfo, err error) error {
- if err != nil {
- return err
- }
- if !info.IsDir() {
- fmt.Println(path)
- fileInfo, err := os.Stat(path)
- if err != nil {
- fmt.Println("os.Stat:", err.Error())
- }
- winFileAttr := fileInfo.Sys().(*syscall.Win32FileAttributeData)
- modifyTime := SecondToTime(winFileAttr.LastWriteTime.Nanoseconds() / 1e9).Format(utils.FormatDateTime)
- fmt.Println("最后修改时间:", modifyTime)
- cacheClient.Set(path, modifyTime, 24*time.Hour)
- }
- return nil
- })
- fmt.Println("Read err:", err)
- }
- // 把秒级的时间戳转为time格式
- func SecondToTime(sec int64) time.Time {
- return time.Unix(sec, 0)
- }
|