func setGlobalConfig(conf config.Configer) error { //config globals if appname := conf.String("appname"); appname != "" { AppName = appname } else if appname == "" { return fmt.Errorf("AppName config value is null") } if usage := conf.String("usage"); usage != "" { Usage = usage } else if usage == "" { return fmt.Errorf("Usage config value is null") } if version := conf.String("version"); version != "" { Version = version } else if version == "" { return fmt.Errorf("Version config value is null") } if author := conf.String("author"); author != "" { Author = author } else if author == "" { return fmt.Errorf("Author config value is null") } if email := conf.String("email"); email != "" { Email = email } else if email == "" { return fmt.Errorf("Email config value is null") } return nil }
// ParseDIYToMap is used to convert the json object to map[string]string func ParseDIYToMap(cf config.Configer, name string) (m map[string]string, err error) { DIY, err := cf.DIY(name) if err != nil { return } itf, ok := DIY.(map[string]interface{}) if !ok { err = errors.New("value is not map[string]interface{}") return } m = make(map[string]string) for k, v := range itf { m[k] = v.(string) } return }
func assignSingleConfig(p interface{}, ac config.Configer) { pt := reflect.TypeOf(p) if pt.Kind() != reflect.Ptr { return } pt = pt.Elem() if pt.Kind() != reflect.Struct { return } pv := reflect.ValueOf(p).Elem() for i := 0; i < pt.NumField(); i++ { pf := pv.Field(i) if !pf.CanSet() { continue } name := pt.Field(i).Name switch pf.Kind() { case reflect.String: pf.SetString(ac.DefaultString(name, pf.String())) case reflect.Int, reflect.Int64: pf.SetInt(int64(ac.DefaultInt64(name, pf.Int()))) case reflect.Bool: pf.SetBool(ac.DefaultBool(name, pf.Bool())) case reflect.Struct: default: //do nothing here } } }
// ParseDIYToMap is used to convert the json object to map[string]map[string]string func ParseDIYToMaps(cf config.Configer, name string) (m map[string]map[string]string, err error) { DIY, err := cf.DIY(name) if err != nil { return } itf, ok := DIY.(map[string]interface{}) if !ok { err = errors.New("value is not map[string]interface{}") return } m = make(map[string]map[string]string) for k, v := range itf { mTmp := v.(map[string]interface{}) for k2, v2 := range mTmp { if _, exist := m[k]; !exist { m[k] = make(map[string]string) } m[k][k2] = v2.(string) } } return }
func assignConfig(ac config.Configer) error { // set the run mode first if envRunMode := os.Getenv("BEEGO_RUNMODE"); envRunMode != "" { BConfig.RunMode = envRunMode } else if runMode := ac.String("RunMode"); runMode != "" { BConfig.RunMode = runMode } for _, i := range []interface{}{BConfig, &BConfig.Listen, &BConfig.WebConfig, &BConfig.Log, &BConfig.WebConfig.Session} { assignSingleConfig(i, ac) } if sd := ac.String("StaticDir"); sd != "" { BConfig.WebConfig.StaticDir = map[string]string{} sds := strings.Fields(sd) for _, v := range sds { if url2fsmap := strings.SplitN(v, ":", 2); len(url2fsmap) == 2 { BConfig.WebConfig.StaticDir["/"+strings.Trim(url2fsmap[0], "/")] = url2fsmap[1] } else { BConfig.WebConfig.StaticDir["/"+strings.Trim(url2fsmap[0], "/")] = url2fsmap[0] } } } if sgz := ac.String("StaticExtensionsToGzip"); sgz != "" { extensions := strings.Split(sgz, ",") fileExts := []string{} for _, ext := range extensions { ext = strings.TrimSpace(ext) if ext == "" { continue } if !strings.HasPrefix(ext, ".") { ext = "." + ext } fileExts = append(fileExts, ext) } if len(fileExts) > 0 { BConfig.WebConfig.StaticExtensionsToGzip = fileExts } } if lo := ac.String("LogOutputs"); lo != "" { los := strings.Split(lo, ";") for _, v := range los { if logType2Config := strings.SplitN(v, ",", 2); len(logType2Config) == 2 { BConfig.Log.Outputs[logType2Config[0]] = logType2Config[1] } else { continue } } } //init log logs.Reset() for adaptor, config := range BConfig.Log.Outputs { err := logs.SetLogger(adaptor, config) if err != nil { fmt.Fprintln(os.Stderr, fmt.Sprintf("%s with the config %q got err:%s", adaptor, config, err.Error())) } } logs.SetLogFuncCall(BConfig.Log.FileLineNum) return nil }
func setServerConfig(conf config.Configer) error { //config runtime if runmode := conf.String("runmode"); runmode != "" { RunMode = runmode } else if runmode == "" { return fmt.Errorf("RunMode config value is null") } if listenmode := conf.String("listenmode"); listenmode != "" { ListenMode = listenmode } else if listenmode == "" { return fmt.Errorf("ListenMode config value is null") } if httpscertfile := conf.String("httpscertfile"); httpscertfile != "" { HTTPSCertFile = httpscertfile } else if httpscertfile == "" { return fmt.Errorf("HttpsCertFile config value is null") } if httpskeyfile := conf.String("httpskeyfile"); httpskeyfile != "" { HTTPSKeyFile = httpskeyfile } else if httpskeyfile == "" { return fmt.Errorf("HttpsKeyFile config value is null") } if logpath := conf.String("log::filepath"); logpath != "" { LogPath = logpath } else if logpath == "" { return fmt.Errorf("LogPath config value is null") } if loglevel := conf.String("log::level"); loglevel != "" { LogLevel = loglevel } else if loglevel == "" { return fmt.Errorf("LogLevel config value is null") } if databasedriver := conf.String("database::driver"); databasedriver != "" { DatabaseDriver = databasedriver } else if databasedriver == "" { return fmt.Errorf("Database Driver config value is null") } if databaseuri := conf.String("database::uri"); databaseuri != "" { DatabaseURI = databaseuri } else if databaseuri == "" { return fmt.Errorf("Database URI config vaule is null") } // Deployment domain could be empty if domains := conf.String("deployment::domains"); domains != "" { Domains = domains } else if domains == "" { return fmt.Errorf("Deployment domains config vaule is null") } //TODO: Add a config option for provide Docker Registry V1. //TODO: Link @middle/header/setRespHeaders, @handler/dockerv1/-functions. if standalone := conf.String("dockerv1::standalone"); standalone != "" { DockerStandalone = standalone } else if standalone == "" { return fmt.Errorf("DockerV1 standalone value is null") } if registry := conf.String("dockerv1::version"); registry != "" { DockerRegistryVersion = registry } else if registry == "" { return fmt.Errorf("DockerV1 Registry Version value is null") } if storage := conf.String("dockerv1::storage"); storage != "" { DockerV1Storage = storage } else if storage == "" { return fmt.Errorf("DockerV1 Storage value is null") } if distribution := conf.String("dockerv2::distribution"); distribution != "" { DockerDistributionVersion = distribution } else if distribution == "" { return fmt.Errorf("DockerV2 Distribution Version value is null") } if storage := conf.String("dockerv2::storage"); storage != "" { DockerV2Storage = storage } else if storage == "" { return fmt.Errorf("DockerV2 Storage value is null") } if storage := conf.String("appc::storage"); storage != "" { AppcStorage = storage } else if storage == "" { return fmt.Errorf("Appc Storage value is null") } //config update service if uskeymanager := conf.String("updateserver::keymanager"); uskeymanager != "" { KeyManager = uskeymanager } else if uskeymanager == "" { return fmt.Errorf("Update Server Key manager config value is null") } if usstorage := conf.String("updateserver::storage"); usstorage != "" { Storage = usstorage } else if usstorage == "" { return fmt.Errorf("Update Server Storage config value is null") } //scan content if scanKey := conf.String("scancontent:scanKey"); scanKey != "" { ScanKey = scanKey } else if scanKey == "" { //auto-generate the scan key if it is empty //WARNING: if the dockyard server restarts, user received data like callbackID will be useless. var key fernet.Key key.Generate() ScanKey = string(key.Encode()) } return nil }