コード例 #1
0
func createMySQLDatabase(jsonDB *mysqldb.JSONDB, mysqlUsername, mysqlPassword string) (err error) {
	conn, myErr := mysqldb.NewConnection(mysqlUsername, mysqlPassword)
	if myErr.Exists() {
		err = errors.New(myErr.Error())
		fmt.Println("Connection Error: ", err.Error())
		return
	}

	_, myErr = conn.GenerateNewDatabaseFromJSON(jsonDB)
	if myErr.Exists() && !myErr.Is(mysqldb.DATABASE_ALREADY_EXISTS) {
		err = errors.New(myErr.Error())
		fmt.Println("Generation Error: ", err.Error())
	}

	return
}
コード例 #2
0
ファイル: dataserver.go プロジェクト: mehulsbhatt/dataserver
func New(jsonDB *mysqldb.JSONDB, config DataServerConfig) (server *DataServer, err error) {
	server = new(DataServer)
	server.config = config
	fmt.Println("Loading default database.")
	conn, myErr := mysqldb.NewConnection(config.MySQLUsername, config.MySQLPassword)
	if myErr.Exists() {
		fmt.Println(myErr.Error())
		return
	}
	server.database = conn.NewDatabaseFromJSON(jsonDB)
	conn.UseDatabase(server.database)

	evtTbl := server.database.GetTable(EVENTS_TABLE_NAME)
	if evtTbl == nil {
		fmt.Println("Events table does not exist.")
		return
	}

	fmt.Println("Loading event tables.")
	dbErr := evtTbl.SelectAll(func(rows *sql.Rows) (stop bool) {
		stop = false
		eventName := ""
		tableName := ""
		definition := ""
		err = rows.Scan(&eventName, &tableName, &definition)
		if err != nil {
			fmt.Println(fmt.Sprintf("Error reading row. Extracted { event_name=%v, table_name=%v, definition=%v }", eventName, tableName, definition))
			return
		}

		jTbl, serr := server.CreateTableFromEventDefinition(definition)
		if serr != nil {
			err = serr
			fmt.Println(err.Error())
			stop = true
			return
		}

		server.database.NewTableFromJSON(jTbl)
		fmt.Println("Added Event Table: ", jTbl.KeyName)
		return
	})

	if dbErr.Exists() {
		fmt.Println("Couldn't select from event table: ", dbErr.Error())
		return
	}

	http.Handle("/", server)

	fmt.Println("Creating Startup Log")
	log := "Start Up\r\n"
	dateStr := mysqldb.CurrentTime()
	fileName := server.LogRoot() + dateStr + "_StartupLog.txt"
	fileName = strings.Replace(fileName, ":", "-", -1)
	err = ioutil.WriteFile(fileName, []byte(log), os.ModePerm)
	if err != nil {
		fmt.Println("Could not write to startup log: ", err.Error(), "\r\n", fileName)
	}

	fmt.Println("Starting Server")
	err = http.ListenAndServe(server.config.PortNumber, nil)
	if err != nil {
		fmt.Println("Failed to start server: ", err.Error())
	}

	fmt.Println("Stopping Server")
	return
}