Пример #1
0
// SetupEnvironment sets up the environment for tests. This should only be
// called once per package and only in the first _test.go file of the package
// that needs it.
func SetupEnvironment() {
	fmt.Println("Setting up test environment...")
	// Need base directory for config and other files
	err := os.Chdir("../../bin")
	if err != nil {
		panic("Unable to change directory for tests")
	}
	// Remove old test files
	deleteFiles(constants.TestTempDirectory)

	// Use testing configuration
	config.CreateTestConfig()
	constants.IsTest = true

	// Dump is not in test directory and needs config access
	deleteFiles(constants.DumpFileFullPath(
		config.Config.DebugConfig.ServerDumpFilename))
}
Пример #2
0
func getServers(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")
	var asl *models.APIServerList

	if config.Config.DebugConfig.ServerDumpFileAsMasterList {
		asl = useDumpFileAsMasterList(constants.DumpFileFullPath(
			config.Config.DebugConfig.ServerDumpFilename))
	} else {
		asl = models.MasterList
	}
	// Empty (i.e. during first retrieval/startup)
	if asl == nil {
		writeJSONResponse(w, models.GetDefaultServerList())
		return
	}
	srvfilters := getSrvFilterFromQString(r.URL.Query(), getServersQueryStrings)
	logger.WriteDebug("server list will be filtered with: %v", srvfilters)
	list := filterServers(srvfilters, asl)
	writeJSONResponse(w, list)
}
Пример #3
0
func dumpServersToDisk(gamename string, sl *models.APIServerList) error {
	j, err := json.Marshal(sl)
	if err != nil {
		return logger.LogAppErrorf("Error marshaling json: %s", err)
	}
	t := time.Now()
	if err := util.CreateDirectory(constants.DumpDirectory); err != nil {
		return logger.LogAppErrorf("Couldn't create '%s' dir: %s\n",
			constants.DumpDirectory, err)
	}
	// Windows doesn't allow ":" in filename so use '-' separators for time
	err = util.CreateByteFile(j, constants.DumpFileFullPath(
		fmt.Sprintf("%s-servers-%d-%02d-%02d.%02d-%02d-%02d.json",
			gamename, t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second())),
		true)
	if err != nil {
		return logger.LogAppErrorf("Error creating server dump file: %s", err)
	}
	return nil
}
Пример #4
0
func init() {
	test.SetupEnvironment()
	testURLBase = fmt.Sprintf("http://:%d", config.Config.WebConfig.APIWebPort)
	db.InitDBs()

	// create dump server file
	err := util.CreateDirectory(constants.DumpDirectory)
	if err != nil {
		panic("Unable to create dump directory used in tests")
	}
	err = util.CreateByteFile(constants.TestServerDumpJSON, constants.DumpFileFullPath(
		config.Config.DebugConfig.ServerDumpFilename), true)
	if err != nil {
		panic(fmt.Sprintf("Test dump file creation error: %s", err))
	}

	// launch server
	go func() {
		r := mux.NewRouter().StrictSlash(true)
		for _, ar := range apiRoutes {
			var handler http.Handler
			handler = compressGzip(ar.handlerFunc, config.Config.WebConfig.CompressResponses)

			r.Methods(ar.method).
				MatcherFunc(pathQStrToLowerMatcherFunc(r, ar.path, ar.queryStrings,
					getRequiredQryStringCount(ar.queryStrings))).
				Name(ar.name).
				Handler(http.TimeoutHandler(handler,
					time.Duration(config.Config.WebConfig.APIWebTimeout)*time.Second,
					`{"error":"Timeout"}`))
		}
		err := http.ListenAndServe(fmt.Sprintf(":%d", config.Config.WebConfig.APIWebPort), r)
		if err != nil {
			panic("Unable to start web server")
		}
	}()
}