package setting import ( "errors" "github.com/go-ini/ini" "github.com/jacobsa/go-serial/serial" "github.com/kptyun/KPTCOMM/pkg/util" "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 ServerName string DisplayName string Description string GRFD string } var ServerSetting = &Server{} var CommSetting = &serial.OpenOptions{} var SnowIds *util.IdWorker type Database struct { Type string User string Password string Host string Name string ShowXormlog bool ShowGetSqllog bool CacheApiSql bool TablePrefix string } var DatabaseSetting = &Database{} 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) } SnowIds,err =util.NewIdWorker(1) if err != nil { log.Fatalf("setting.Setup, fail to create SnowIds: %v", err) } DatabaseSetting.ShowXormlog = false DatabaseSetting.ShowGetSqllog = false DatabaseSetting.CacheApiSql = false mapTo("app", AppSetting) mapTo("server", ServerSetting) mapTo("database", DatabaseSetting) mapTo("comm", CommSetting) 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) } }