Exemple #1
0
// load unmarshalls rule data from yaml files into the translator's rules
func (t *TranslatorRules) load(files []string) (errors []error) {

	for _, file := range files {
		_, statErr := os.Stat(file)
		if statErr == nil {
			contents, readErr := ioutil.ReadFile(file)

			if readErr != nil {
				errors = append(errors, translatorError{message: "can't open rules file: " + readErr.Error()})
			}

			tNew := new(TranslatorRules)
			yamlErr := confl.Unmarshal(contents, tNew)

			if yamlErr != nil {
				errors = append(errors, translatorError{message: "can't load rules YAML: " + yamlErr.Error()})
			} else {
				t.merge(tNew)
			}
		}
	}

	// set the plural rule func
	pRule, ok := pluralRules[t.Plural]
	if ok {
		t.PluralRuleFunc = pRule
	} else {
		if t.Plural == "" {
			errors = append(errors, translatorError{message: "missing plural rule: " + t.Plural})

		} else {
			errors = append(errors, translatorError{message: "invalid plural rule: " + t.Plural})
		}
		t.PluralRuleFunc = pluralRules["1"]
	}

	if t.Direction == "" {
		errors = append(errors, translatorError{message: "missing direction rule"})
		t.Direction = direction_ltr
	} else if t.Direction != direction_ltr && t.Direction != direction_rtl {
		errors = append(errors, translatorError{message: "invalid direction rule: " + t.Direction})
		t.Direction = direction_ltr
	}

	return
}
Exemple #2
0
func startTower() {
	var (
		appMainFile        = *_appMainFile
		appPort            = *_appPort
		pxyPort            = *_pxyPort
		appBuildDir        = *_appBuildDir
		portParamName      = *_portParamName
		runParams          = *_runParams
		configFile         = *_configFile
		verbose            = *_verbose
		adminPwd           = *_adminPwd
		adminIPs           = *_adminIPs
		allowBuild         = atob(build)
		suffix             = ".exe"
		_suffix            = ""
		watchedFiles       string
		watchedOtherDir    string
		ignoredPathPattern string
		offlineMode        bool
		disabledLogRequest bool
	)
	if configFile == "" {
		configFile = ConfigName
	}
	contents, err := ioutil.ReadFile(configFile)
	if err != nil {
		fmt.Println(err)
	} else {
		newmap := map[string]string{}
		yamlErr := confl.Unmarshal(contents, &newmap)
		if yamlErr != nil {
			fmt.Println(yamlErr)
		}
		appPort, _ = newmap["app_port"]
		pxyPort, _ = newmap["pxy_port"]
		appBuildDir, _ = newmap["app_buildDir"]
		portParamName, _ = newmap["app_portParamName"]
		runParams, _ = newmap["app_runParams"]
		watchedFiles, _ = newmap["watch"]
		watchedOtherDir, _ = newmap["watch_otherDir"] //编译模式下有效
		ignoredPathPattern, _ = newmap["watch_ignoredPath"]
		offlineModeStr, _ := newmap["offline_mode"]

		if v, ok := newmap["admin_pwd"]; ok {
			adminPwd = v
		}
		if v, ok := newmap["admin_ip"]; ok {
			adminIPs = v
		}
		if atob(offlineModeStr) {
			offlineMode = true
		}
		if logRequestStr, ok := newmap["log_request"]; ok {
			disabledLogRequest = atob(logRequestStr) == false
		}
		if pxyPort == "" {
			pxyPort = ProxyPort
		}
		if allowBuild {
			appMainFile, _ = newmap["main"] //编译模式下有效
		} else {
			appMainFile, _ = newmap["exec"] //非编译模式下有效
			if appMainFile == "" {
				fmt.Println("请设置exec参数用来指定执行文件位置")
				time.Sleep(time.Second * 10)
				return
			}
		}
	}

	err = dialAddress("127.0.0.1:"+pxyPort, 1)
	if err == nil {
		fmt.Println("Error: port (" + pxyPort + ") already in used.")
		os.Exit(1)
	}

	if verbose {
		fmt.Println("== Application Info")
		fmt.Printf("  build app with: %s\n", appMainFile)
		fmt.Printf("  redirect requests from localhost:%s to localhost:%s\n\n", ProxyPort, appPort)
	}
	if !allowBuild {
		if strings.Contains(appMainFile, `*`) {
			orgiMainFile := appMainFile
			appMainFile = findBinFile(appMainFile)
			if appMainFile == `` {
				if appBuildDir != `` {
					appMainFile = filepath.Join(appBuildDir, orgiMainFile)
					appMainFile = findBinFile(appMainFile)
				}
			}
		}
		if err := checkBinFile(appMainFile, suffix, &_suffix, &appBuildDir); err != nil {
			fmt.Println(err)
			time.Sleep(time.Second * 300)
			return
		}
	} else {
		if appBuildDir == `` {
			appMainFile, _ = filepath.Abs(appMainFile)
			appBuildDir = filepath.Dir(appMainFile)
		}
	}
	app = NewApp(appMainFile, appPort, appBuildDir, portParamName)
	app.OfflineMode = offlineMode
	app.DisabledLogRequest = disabledLogRequest
	if runParams != `` {
		app.RunParams = strings.Split(runParams, ` `)
	}
	watchedDir := app.Root
	if !allowBuild {
		if app.BuildDir != `` {
			watchedDir = app.BuildDir
		}
	}
	if watchedOtherDir != "" {
		watchedDir = watchedOtherDir + "|" + watchedDir
	}
	watcher := NewWatcher(watchedDir, watchedFiles, ignoredPathPattern)
	proxy := NewProxy(&app, &watcher)
	proxy.AdminPwd = adminPwd
	if adminIPs != `` {
		proxy.AdminIPs = strings.Split(adminIPs, `,`)
	}
	if allowBuild {
		watcher.OnChanged = func(file string) {
			fmt.Println(`== Build Mode.`)
			watcher.Reset()
			fileName := filepath.Base(file)
			if strings.HasPrefix(fileName, BinPrefix) {
				fmt.Println(`忽略`, fileName, `更改`)
				return
			}
			if !app.SupportMutiPort() {
				fmt.Println(`Unspecified switchable other ports.`)
				return
			}
			port := app.UseRandPort()
			for i := 0; i < 3 && port == app.Port; i++ {
				app.Clean()
				time.Sleep(time.Second)
				port = app.UseRandPort()
			}
			if port == app.Port {
				fmt.Println(`取得的端口与当前端口相同,无法编译切换`)
				return
			}
			err = app.Start(true, port)
			if err != nil {
				fmt.Println(err)
			}
		}
	} else {
		watcher.OnChanged = func(file string) {
			fmt.Println(`== Switch Mode.`)
			watcher.Reset()
			if !app.SupportMutiPort() {
				fmt.Println(`Unspecified switchable other ports.`)
				return
			}
			port := app.UseRandPort()
			for i := 0; i < 3 && port == app.Port; i++ {
				app.Clean()
				time.Sleep(time.Second)
				port = app.UseRandPort()
			}
			if port == app.Port {
				fmt.Println(`取得的端口与当前端口相同,无法切换`)
				return
			}

			fileName := filepath.Base(file)
			if !strings.HasPrefix(fileName, BinPrefix) {
				fmt.Println(`忽略非`, BinPrefix, `前缀文件更改`)
				return
			}
			if _suffix != "" {
				fileName = strings.TrimSuffix(fileName, _suffix)
			}
			newAppBin := fileName
			fileName = strings.TrimPrefix(fileName, BinPrefix)
			newFileTs, err := strconv.ParseInt(fileName, 10, 64)
			if err != nil {
				fmt.Println(err)
				return
			}
			fileName = strings.TrimPrefix(AppBin, BinPrefix)
			oldFileTs, err := strconv.ParseInt(fileName, 10, 64)
			if err != nil {
				fmt.Println(err)
				return
			}
			if newFileTs <= oldFileTs {
				fmt.Println(`新文件时间戳小于旧文件,忽略`)
				return
			}
			AppBin = newAppBin
			err = app.Start(true, port)
			if err != nil {
				fmt.Println(err)
			}
		}
		watcher.OnlyWatchBin = true
		app.DisabledBuild = true
	}
	proxy.Port = pxyPort
	go func() {
		mustSuccess(watcher.Watch())
	}()
	err = app.Start(true, app.Port)
	if err != nil {
		fmt.Println(err)
	}
	mustSuccess(proxy.Listen())
}
Exemple #3
0
// loadMessages loads all messages from the properly named locale message yaml
// files in the requested messagesPaths.  if multiple paths are provided, paths
// further down the list take precedence over earlier paths.
func loadMessages(locale string, messagesPaths []string) (messages map[string]string, errors []error) {

	messages = make(map[string]string)

	found := false
	for _, p := range messagesPaths {
		p = strings.TrimRight(p, pathSeparator)
		file := p + pathSeparator + locale + ".yaml"

		_, statErr := os.Stat(file)
		if statErr == nil {
			contents, readErr := ioutil.ReadFile(file)
			if readErr != nil {
				errors = append(errors, translatorError{message: "can't open messages file: " + readErr.Error()})
			} else {
				newmap := map[string]string{}
				yamlErr := confl.Unmarshal(contents, &newmap)
				if yamlErr != nil {
					errors = append(errors, translatorError{message: "can't load messages YAML: " + yamlErr.Error()})
				} else {
					found = true
					for key, value := range newmap {
						messages[key] = value
					}
				}
			}
		}

		// now look for a directory named after this locale and get an *.yaml children
		dir := p + pathSeparator + locale
		info, statErr := os.Stat(dir)
		if statErr == nil && info.IsDir() {
			// found the directory - now look for *.yaml files
			files, globErr := filepath.Glob(dir + pathSeparator + "*.yaml")
			if globErr != nil {
				errors = append(errors, translatorError{message: "can't glob messages files: " + globErr.Error()})
			}
			for _, file := range files {
				contents, readErr := ioutil.ReadFile(file)
				if readErr != nil {
					errors = append(errors, translatorError{message: "can't open messages file: " + readErr.Error()})
				} else {
					newmap := map[string]string{}
					yamlErr := confl.Unmarshal(contents, &newmap)
					if yamlErr != nil {
						errors = append(errors, translatorError{message: "can't load messages YAML: " + yamlErr.Error()})
					} else {
						found = true
						for key, value := range newmap {
							messages[key] = value
						}
					}
				}
			}
		}
	}

	if !found {
		errors = append(errors, translatorError{message: "no messages files found: " + locale})
	}

	return
}