// LoadGopmfile loads and returns given gopmfile. func LoadGopmfile(fileName string) (*goconfig.ConfigFile, error) { if !base.IsFile(fileName) { return goconfig.LoadFromData([]byte("")) } gf, err := goconfig.LoadConfigFile(fileName) if err != nil { return nil, fmt.Errorf("Fail to load gopmfile: %v", err) } return gf, nil }
func LoadLocalNodes() (err error) { if !base.IsFile(LocalNodesFile) { os.MkdirAll(path.Dir(LocalNodesFile), os.ModePerm) os.Create(LocalNodesFile) } LocalNodes, err = goconfig.LoadConfigFile(LocalNodesFile) if err != nil { return fmt.Errorf("fail to load localnodes.list: %v", err) } return nil }
// LoadConfig loads gopm global configuration. func LoadConfig() (err error) { if !base.IsExist(ConfigFile) { os.MkdirAll(path.Dir(ConfigFile), os.ModePerm) if _, err = os.Create(ConfigFile); err != nil { return fmt.Errorf("fail to create config file: %v", err) } } Cfg, err = goconfig.LoadConfigFile(ConfigFile) if err != nil { return fmt.Errorf("fail to load config file: %v", err) } HttpProxy = Cfg.MustValue("settings", "HTTP_PROXY") return nil }
func init() { flag.Parse() DebugMode = *debugMode WebDebugMode = *webDebugMode ShowSql = *showSql if *versionInfo { fmt.Printf("%s\n", VersionString()) os.Exit(0) } if len(os.Args) == 2 && os.Args[1] == "reload" { wd, _ := os.Getwd() pidFile, err := os.Open(filepath.Join(wd, "instafig.pid")) if err != nil { log.Printf("Failed to open pid file: %s", err.Error()) os.Exit(1) } pids := make([]byte, 10) n, err := pidFile.Read(pids) if err != nil { log.Printf("Failed to read pid file: %s", err.Error()) os.Exit(1) } if n == 0 { log.Printf("No pid in pid file: %s", err.Error()) os.Exit(1) } _, err = exec.Command("kill", "-USR2", string(pids[:n])).Output() if err != nil { log.Printf("Failed to restart Instafig service: %s", err.Error()) os.Exit(1) } pidFile.Close() os.Exit(0) } if *maxThreadNum == 0 { *maxThreadNum = runtime.NumCPU() } runtime.GOMAXPROCS(*maxThreadNum) if *configFile == "__unset__" { p, _ := os.Getwd() *configFile = filepath.Join(p, "conf/config.ini") } confFile, err := filepath.Abs(*configFile) if err != nil { log.Printf("No correct config file: %s - %s", *configFile, err.Error()) os.Exit(1) } config, err := goconfig.LoadConfigFile(confFile) if err != nil { log.Printf("No correct config file: %s - %s", *configFile, err.Error()) os.Exit(1) } ClientAddr, _ = config.GetValue("", "http_addr") s := strings.Split(ClientAddr, ":") if len(s) != 1 && len(s) != 2 { log.Printf("No correct http_addr(%s)", ClientAddr) os.Exit(1) } port := "80" if len(s) == 2 { port = s[1] } if Port, err = strconv.Atoi(port); err != nil { log.Printf("No correct port(%s): %s", port, err.Error()) os.Exit(1) } if requestLogEnable, _ := config.GetValue("", "request_log_enable"); requestLogEnable == "yes" { RequestLogEnable = true } LogDir, _ = config.GetValue("", "log_dir") if LogDir, err = filepath.Abs(LogDir); err != nil { log.Printf("Bad log_dir value: %s - %s", LogDir, err.Error()) os.Exit(1) } if exec.Command("mkdir", "-p", LogDir).Run() != nil { log.Printf("Failed to create log_dir value: %s - %s", LogDir, err.Error()) os.Exit(1) } UserPassCodeEncryptKey, _ = config.GetValue("", "user_passcode_encrypt_key") SqliteDir, _ = config.GetValue("sqlite", "dir") if SqliteDir, err = filepath.Abs(SqliteDir); err != nil { log.Println("sqlite dir is not correct: " + err.Error()) os.Exit(1) } SqliteFileName, _ = config.GetValue("sqlite", "filename") NodeType, _ = config.GetValue("node", "type") NodeAddr, _ = config.GetValue("node", "node_addr") NodeAuth, _ = config.GetValue("node", "node_auth") if !IsMasterNode() { MasterAddr, _ = config.GetValue("node", "master_addr") } if !IsMasterNode() { intervalStr, _ := config.GetValue("node", "check_master_interval") if CheckMasterInerval, err = strconv.Atoi(intervalStr); err != nil { log.Printf("No correct expires: %s - %s", intervalStr, err.Error()) os.Exit(1) } expiresStr, _ := config.GetValue("node", "data_expires") if expiresStr != "" { if DataExpires, err = strconv.Atoi(expiresStr); err != nil { log.Printf("No correct expires: %s - %s", expiresStr, err.Error()) os.Exit(1) } if DataExpires <= CheckMasterInerval { DataExpires = CheckMasterInerval * 2 } } else { DataExpires = -1 } } if statisticEnable, _ := config.GetValue("statistic", "enable"); statisticEnable == "on" { StatisticEnable = true InfluxDB, _ = config.GetValue("statistic", "influx_db") InfluxURL, _ = config.GetValue("statistic", "influx_url") InfluxUser, _ = config.GetValue("statistic", "influx_user") InfluxPassword, _ = config.GetValue("statistic", "influx_password") batchCount, _ := config.GetValue("statistic", "influx_batch_points_count") if InfluxBatchPointsCount, err = strconv.Atoi(batchCount); err != nil { log.Println("influx_batch_point_count is not number: ", batchCount) os.Exit(1) } } if !DebugMode { // disable all console log nullFile, _ := os.Open(os.DevNull) log.SetOutput(nullFile) os.Stdout = nullFile } }