package setting import ( "errors" "github.com/go-ini/ini" "github.com/jacobsa/go-serial/serial" "log" "os" "os/exec" "path/filepath" "runtime" "strings" "time" ) type App struct { JwtSecret string PageSize int PrefixUrl string RuntimeRootPath string FileSavePath string FileMaxSize int // 普通文件上传的限制大小,单位byte, 最大单位1GB FileAllowType []string // 允许上传的文件后缀名 ImageSavePath string ImageMaxSize int ImageAllowExts []string ThumbnailSavePath string ThumbnailMaxWidth int ThumbnailMaxHeight int ExportSavePath string QrCodeSavePath string FontSavePath string LogSavePath string LogSaveName string LogFileExt string TimeFormat string } var AppSetting = &App{} type Server struct { RunMode string HttpPort int ReadTimeout time.Duration WriteTimeout time.Duration NoAuth int Mdns_servicename string Mdns_serviceport int HttpsPort int Https int CrtPath string KeyPath string MailStart string CronFormula string WXworkStart string WXworkAgentid string WXworkSecret string WXworkCorpid string ServerName string DisplayName string Description string EasName string EasPassword string EasUrl string EasUrlTem string Netcard string Broadcast int BroadcastAddr string UseAPI int SendRoute int64 ControlComm int RelayDev int } var ServerSetting = &Server{} var CommSetting = &serial.OpenOptions{} var ControlCommSetting = &serial.OpenOptions{} type Database struct { Type string User string Password string Host string Name string ShowXormlog bool ShowGetSqllog bool Showline bool Showlog bool CacheApiSql bool TablePrefix string } var DatabaseSetting = &Database{} type Mqtt struct { Enable bool Host string Port int Nodevalue string Path string Topic_Up string Topic_Down string Qos int CleanSession bool SaslEnable bool Username string Saslpassword string TlsEnable bool Clientcert string Clientkey string Cacert string } var MqttSetting = &Mqtt{} var cfg *ini.File var CurrentPath string func GetCurrentPath() (string, error) { file, err := exec.LookPath(os.Args[0]) if err != nil { return "", err } path, err := filepath.Abs(file) if err != nil { return "", err } //fmt.Println("path111:", path) if runtime.GOOS == "windows" { path = strings.Replace(path, "\\", "/", -1) } //fmt.Println("path222:", path) i := strings.LastIndex(path, "/") if i < 0 { return "", errors.New(`Can't find "/" or "\".`) } //fmt.Println("path333:", path) return string(path[0 : i+1]), nil } func Setup(dir string) { var err error if strings.Trim(dir, " ") == "" { CurrentPath, err = GetCurrentPath() } else { CurrentPath = dir } cfg, err = ini.Load(CurrentPath + "conf/app.ini") if err != nil { log.Fatalf("setting.Setup, fail to parse 'conf/app.ini': %v", err) } DatabaseSetting.ShowXormlog = false DatabaseSetting.ShowGetSqllog = false DatabaseSetting.CacheApiSql = false mapTo("app", AppSetting) mapTo("server", ServerSetting) mapTo("database", DatabaseSetting) mapTo("comm", CommSetting) mapTo("Controlcomm", ControlCommSetting) AppSetting.ImageMaxSize = AppSetting.ImageMaxSize * 1024 * 1024 ServerSetting.ReadTimeout = ServerSetting.ReadTimeout * time.Second ServerSetting.WriteTimeout = ServerSetting.ReadTimeout * time.Second ServerSetting.CrtPath = CurrentPath + ServerSetting.CrtPath ServerSetting.KeyPath = CurrentPath + ServerSetting.KeyPath } func mapTo(section string, v interface{}) { err := cfg.Section(section).MapTo(v) if err != nil { log.Fatalf("Cfg.MapTo RedisSetting err: %v", err) } }