github.com/angenalZZZ/gofunc@v0.0.0-20210507121333-48ff1be3917b/cmd/jsrun/config.go (about) 1 package main 2 3 import ( 4 "os" 5 "strings" 6 "time" 7 8 "github.com/angenalZZZ/gofunc/configfile" 9 "github.com/angenalZZZ/gofunc/data" 10 "github.com/angenalZZZ/gofunc/data/cache/store" 11 "github.com/angenalZZZ/gofunc/f" 12 "github.com/angenalZZZ/gofunc/log" 13 nat "github.com/angenalZZZ/gofunc/rpc/nats" 14 "github.com/go-redis/redis/v7" 15 16 _ "github.com/denisenkom/go-mssqldb" 17 _ "github.com/go-sql-driver/mysql" 18 _ "github.com/mattn/go-sqlite3" 19 ) 20 21 var ( 22 configInfo *Config 23 configFile = "jsrun.yaml" 24 configMod time.Time 25 scriptFile = "jsrun.js" 26 scriptMod time.Time 27 ) 28 29 // Config The Config Info For jsrun.yaml 30 type Config struct { 31 Db struct { 32 Type string 33 Conn string 34 } 35 Nats *nat.Connection 36 Redis *redis.Options 37 Log *log.Config 38 Script string 39 } 40 41 func initConfig() error { 42 configInfo = new(Config) 43 44 if isConfigMod() == false { 45 return os.ErrNotExist 46 } 47 48 if err := configfile.YamlTo(configFile, configInfo); err != nil { 49 return err 50 } 51 52 data.DbType, data.DbConn = configInfo.Db.Type, configInfo.Db.Conn 53 54 if store.RedisClient == nil && configInfo.Redis != nil && configInfo.Redis.Addr != "" { 55 store.RedisClient = redis.NewClient(configInfo.Redis) 56 } 57 58 return nil 59 } 60 61 func isConfigMod() bool { 62 if configFile == "" { 63 return false 64 } 65 info, err := os.Stat(configFile) 66 if os.IsNotExist(err) { 67 return false 68 } 69 if t := info.ModTime(); t.Unix() != configMod.Unix() { 70 configMod = t 71 return true 72 } 73 return false 74 } 75 76 func isScriptMod() bool { 77 if scriptFile == "" { 78 return false 79 } 80 info, err := os.Stat(scriptFile) 81 if os.IsNotExist(err) { 82 return false 83 } 84 if t := info.ModTime(); t.Unix() != scriptMod.Unix() { 85 scriptMod = t 86 return true 87 } 88 return false 89 } 90 91 func doScriptMod() error { 92 script, err := f.ReadFile(scriptFile) 93 if err != nil { 94 return err 95 } 96 97 configInfo.Script = strings.TrimSpace(string(script)) 98 return nil 99 }