Exemple #1
0
func (suite *UserStoreSuite) SetupSuite() {
	store, err := newTestUserStore()
	if err != nil {
		panic(err)
	}
	suite.store = store

	// create necessary DB, table, indices
	_, err = r.DbCreate("test").Run(suite.store.conn)
	if err != nil {
		panic(err)
	}
	_, err = r.Db("test").TableCreate("users").Run(suite.store.conn)
	if err != nil {
		panic(err)
	}
	_, err = r.Db("test").Table("users").IndexCreate("email").Run(suite.store.conn)
	if err != nil {
		panic(err)
	}

	suite.user = NewUser("*****@*****.**", "password")
	_, err = r.Db("test").Table("users").Insert(suite.user).RunWrite(suite.store.conn)
	if err != nil {
		panic(err)
	}
}
Exemple #2
0
func (db *RethinkDBAdapter) createTableWithOptions(tableName string, rdbOptions *r.TableCreateOpts) *CASServerError {
	logMessagef(db.LogLevel, "INFO", "Creating table [%s], options: %v", tableName, rdbOptions)

	// Check again that rdbOptions is not nil, optionally leave out argument
	var err error
	if rdbOptions == nil {

		// Create table with no options
		_, err = r.Db(db.dbName).TableCreate(tableName).Run(db.session)

	} else {

		// Set and get the table options (so that they can be retrieved later
		db.setTableSetupOptions(tableName, rdbOptions)
		options, err := db.getTableSetupOptions(tableName)
		if err != nil {
			casError := &FailedToCreateTableError
			casError.err = &err
			return casError
		}

		// Create table
		_, err = r.Db(db.dbName).TableCreate(tableName, *options).Run(db.session)
	}

	if err != nil {
		casError := &FailedToCreateTableError
		casError.err = &err
		return casError
	}

	return nil
}
Exemple #3
0
func main() {
	// Connecting to the database
	addr := *rethinkdbHost + ":" + *rethinkdbPort
	log.Info("Connecting to RethinkDB at ", addr)
	sess, err := r.Connect(r.ConnectOpts{
		Address:  addr,
		Database: *env,
	})
	if err != nil {
		log.Fatal(err.Error())
	}
	defer sess.Close()

	_, err = r.Db(*env).TableCreate(tmpTable).RunWrite(sess)
	if err != nil {
		log.Fatal("Couldn't create temp table. ", err.Error())
	}
	log.Info("Created temp table ", *tmpTable, " in db ", *env)

	// init tmp table with accounts

	// process array of table names (i.e. emails, files, etc.)
	for _, t := range bigTables {
		processTable(sess, t)
	}

	_, err = r.Db(*env).TableDrop(*tmpTable).RunWrite(sess)
	if err != nil {
		log.Fatal("Couldn't drop temp table. ", err.Error())
	}
	log.Info("Dropped temp table ", *tmpTable, " in db ", *env)
}
func Example_IndexCreate_compound() {
	sess, err := r.Connect(r.ConnectOpts{
		Address: url,
		AuthKey: authKey,
	})
	if err != nil {
		log.Fatalf("Error connecting to DB: %s", err)
	}

	// Setup database
	r.Db("test").TableDrop("table").Run(sess)
	r.Db("test").TableCreate("table").Run(sess)

	response, err := r.Db("test").Table("table").IndexCreateFunc("full_name", func(row r.Term) interface{} {
		return []interface{}{row.Field("first_name"), row.Field("last_name")}
	}).RunWrite(sess)
	if err != nil {
		log.Fatalf("Error creating index: %s", err)
	}

	fmt.Printf("%d index created", response.Created)

	// Output:
	// 1 index created
}
Exemple #5
0
func (sr *Runner) checkCleared(check stalker.Check) {
	log.Debugln("check cleared")
	log.Infof("%s %s detected as cleared", check.Hostname, check.Check)
	query := map[string]string{"hostname": check.Hostname, "check": check.Check}
	cursor, err := r.Db(STALKERDB).Table("notifications").Filter(query).Run(sr.rsess)
	if err != nil {
		log.Errorln("Error checking for existing notification:", err.Error())
		return
	}
	defer cursor.Close()
	result := stalker.Notification{}
	cursor.One(&result)
	if result.Active == false {
		log.Infoln("No notification to clear")
		return
	}
	_, err = r.Db(STALKERDB).Table("notifications").Filter(query).Delete().RunWrite(sr.rsess)
	if err != nil {
		log.Errorln("Error deleting notification entry:", err.Error())
		return
	}
	sr.emitClear(check)
	return

}
Exemple #6
0
func processTable(sess *r.Session, table string) {
	rows, err := r.Db(*env).Table(table).Run(sess)
	if err != nil {
		log.Fatal("Couldn't fetch rows for table ", table)
	}
	defer rows.Close()

	var doc map[string]interface{}
	var size uint64
	var id string
	var ok bool

	// WIP
	for rows.Next(&doc) {
		if size, ok = doc["size"].(uint64); !ok {
			if id, ok = doc["id"].(string); !ok {
				log.Warn("Found a document without ID! ", doc)
				continue
			}
			sizeTerm, err := r.Db(*env).Table(table).Get(id).
				CoerceTo("string").CoerceTo("binary").Count().Run(sess)
			err = sizeTerm.One(&size)
			if err != nil {
				log.Warn("Couldn't compute the size of document ", id, ". It might've been deleted.", err)
				continue
			}
		}
		fmt.Println(doc)
		return
	} else {

	}
}
Exemple #7
0
func init() {
	var err error
	session, err = gorethink.Connect(gorethink.ConnectOpts{
		Address: "127.0.0.1:28015",
	})
	if err != nil {
		panic(err)
	}

	key2find = uniuri.New()

	// Create a new table
	gorethink.Db("test").TableDrop("benchmark_keys_list").Run(session)
	gorethink.Db("test").TableCreate("benchmark_keys_list").Run(session)

	var klist []*KeysList

	// Populate with sample data
	for n := 0; n < 300; n++ {
		keys := rndStringSlice(999)
		keys = randomlyInsert(keys, key2find)

		y := uniuri.New()
		if n == 153 {
			table2search = y
		}

		klist = append(klist, &KeysList{
			ID:    y,
			Voted: keys,
		})
	}

	gorethink.Db("test").Table("benchmark_keys_list").Insert(klist).Run(session)
}
Exemple #8
0
func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)

	flag.Parse()

	// Connect to RethinkDB
	var err error
	session, err = r.Connect(r.ConnectOpts{
		Address: *rethinkAddress,
	})
	if err != nil {
		log.Fatal(err)
	}

	// Create the database and tables
	r.DbCreate(*rethinkName).Exec(session)
	r.Db(*rethinkName).TableCreate("invites").Exec(session)
	r.Db(*rethinkName).Table("invites").IndexCreate("email").Exec(session)
	r.Db(*rethinkName).Table("invites").IndexCreate("name").Exec(session)

	// Add a CORS middleware
	goji.Use(cors.New(cors.Options{
		AllowCredentials: true,
	}).Handler)

	// Add routes to goji
	goji.Get("/", index)
	goji.Post("/check", check)
	goji.Post("/free", free)
	goji.Post("/create", create)

	// Start the server
	goji.Serve()
}
Exemple #9
0
func main() {
	flag.Parse()

	log = logrus.New()
	if *logFormatterType == "text" {
		log.Formatter = &logrus.TextFormatter{
			ForceColors: *logForceColors,
		}
	} else if *logFormatterType == "json" {
		log.Formatter = &logrus.JSONFormatter{}
	}
	log.Level = logrus.DebugLevel

	if *enableHub || *enableSender {
		var err error
		session, err = r.Connect(r.ConnectOpts{
			Address: *rethinkdbAddress,
		})
		if err != nil {
			log.WithField("error", err.Error()).Fatal("Unable to connect to RethinkDB")
		}

		r.DbCreate(*rethinkdbDatabase).Exec(session)
		r.Db(*rethinkdbDatabase).TableCreate("templates").Exec(session)
		r.Db(*rethinkdbDatabase).Table("templates").IndexCreate("name").Exec(session)
		r.Db(*rethinkdbDatabase).Table("templates").IndexCreate("version").Exec(session)
		r.Db(*rethinkdbDatabase).TableCreate("hub_state").Exec(session)
		r.Db(*rethinkdbDatabase).Table("hub_state").IndexCreate("time").Exec(session)
	}

	up := strings.Split(*usernames, ",")
	pp := strings.Split(*passwords, ",")

	if len(up) != len(pp) {
		log.Fatal("length of usernames and passwords is different")
	}

	if *enableSender {
		go initTemplates()

		for i, username := range up {
			go initSender(username, pp[i])
		}
	}

	if *enableHub {
		change := make(chan struct{})

		go initChurner(change)
		go initHub(change)
	}

	select {}
}
Exemple #10
0
func BenchmarkDelete(b *testing.B) {
	for n := 0; n < b.N; n++ {
		_, err := gorethink.Db("test").Table("benchmark_keys_list").Get(table2search).Field("voted").DeleteAt(
			gorethink.Expr(gorethink.Db("test").Table("benchmark_keys_list").Get(table2search).Field("voted").IndexesOf(key2find).AtIndex(0)),
		).Run(session)
		if err != nil {
			b.Log(err)
			b.Fail()
		}
	}
}
func InitTable(s *r.Session, databaseName string, tableName string) error {
	exists, err := ListContains(s, r.Db(databaseName).TableList(), tableName)
	if err != nil {
		return err
	}
	if !exists {
		_, err = r.Db(databaseName).TableCreate(tableName).RunWrite(s)
		if err != nil {
			return err
		}
	}
	return nil
}
Exemple #12
0
func main() {
	rand.Seed(time.Now().UTC().UnixNano())

	var response []interface{}
	var session *r.Session

	session, err := r.Connect(map[string]interface{}{
		"address":     "localhost:28015",
		"database":    "test",
		"maxIdle":     10,
		"idleTimeout": time.Second * 10,
	})

	if err != nil {
		panic(err)
	}

	r.Db("test").TableCreate("test").Exec(session)

	objects := []interface{}{}

	for i := 3000; i < 3001; i++ {
		www := FetchURL("http://www.cnn.com")
		row := map[string]interface{}{"id": i, "g1": 6771, "www": www}
		objects = append(objects, row)
	}

	log.Println(objects)

	query := r.Db("test").Table("Table1").Insert(objects)

	_, err = query.Run(session)

	if err != nil {
		panic(err)
	}

	query = r.Db("test").Table("Table1").OrderBy("id")

	rows, err := query.Run(session)

	if err != nil {
		panic(err)
	}

	err = rows.ScanAll(&response)

	log.Println(response)
}
Exemple #13
0
// scan the notifications db for checks older than our expiration time and remove them. This will allow them be re-alerted on.
func (sm *Manager) expireNotifications() {
	log.Debugln("Expiring notifications")
	err := r.Db(STALKERDB).Table("notifications").Filter(r.Row.Field("ts").Lt(time.Now().Unix() - sm.notificationExpiration)).Delete().Exec(sm.rsess)
	if err != nil {
		log.Errorln("Error deleting expired notifications:", err.Error())
	}
}
Exemple #14
0
func createTable(name string) error {
	ensureSession()
	// TODO return the object in a useable format instead of just the error
	// object, error
	_, err := r.Db(*db).TableCreate(name).Run(session)
	return err
}
func InitDB() *rethink.Session {

	session, err := rethink.Connect(map[string]interface{}{
		"address":     "localhost:8080", //os.Getenv("RETHINKDB_URL"),
		"database":    "test",
		"maxIdle":     10,
		"idleTimeout": time.Second * 10,
	})

	if err != nil {
		log.Println(err)
	}

	err = rethink.DbCreate("test").Exec(session)
	if err != nil {
		log.Println(err)
	}

	_, err = rethink.Db("test").TableCreate("articles").RunWrite(session)
	if err != nil {
		log.Println(err)
	}

	return session
}
Exemple #16
0
// Find a user by API secret and key
func (db *RethinkDBAdapter) FindUserByApiKeyAndSecret(key, secret string) (*User, *CASServerError) {
	// Find the user
	cursor, err := r.
		Db(db.dbName).
		Table(db.apiKeysTableName).
		Get(key).
		Run(db.session)
	if err != nil {
		casErr := &FailedToFindUserByApiKeyAndSecretError
		casErr.err = &err
		return nil, casErr
	}

	// Get the user from the returned cursor
	var apiKeyPair *CasgoAPIKeyPair
	err = cursor.One(&apiKeyPair)
	if err != nil {
		casErr := &FailedToFindUserByApiKeyAndSecretError
		casErr.err = &err
		return nil, casErr
	}

	// Return error of the secret is invalid
	if apiKeyPair.Secret != secret {
		casErr := &FailedToFindUserByApiKeyAndSecretError
		return nil, casErr
	}

	return apiKeyPair.User, nil
}
Exemple #17
0
func DeleteUser(user models.User) {
	_, err := r.Db("harbor").Table("users").Get(user.Id).Delete().Run(service.Session())
	if err != nil {
		fmt.Println(err)
		return
	}
}
Exemple #18
0
func addItem(w http.ResponseWriter, q *http.Request) {
	//check if SKU exist, if not then generate SKU
	err := q.ParseMultipartForm(32 << 20)

	if err != nil {
		// Handle error
		fmt.Println(err)
	}
	if q.FormValue("SKU") != "" {

		slice := []string{"Image", "Image1", "Image2", "Image3"}

		images := checkifImageneedUpload(q, slice, q.FormValue("SKU"))

		product := Product{
			Title:       q.FormValue("Title"),
			Price:       q.FormValue("Price"),
			SKU:         q.FormValue("SKU"),
			Image:       images,
			Description: q.FormValue("Description"),
		}

		db := dbinit()
		defer db.Close()
		_, err = r.Db("goShop").Table("items").Insert(product).RunWrite(db)
		if err != nil {
			log.Println(err)
		}

		//fmt.Println(q.FormValue("cat"))

		http.Redirect(w, q, "/items", http.StatusFound)
	}

}
Exemple #19
0
// CreateTable creates a new table.
func CreateTable(tableName string) (bool, error) {

	if Session() == nil {
		return false, invalidSession()
	}

	results, err := r.Db(Db()).TableCreate(tableName).RunWrite(Session())
	if err != nil {
		return false, err
	}

	if results.Created == createdSuccessful {

		return true, nil
	}

	// *NOTE* Another way of getting results. For reference.
	//results, err := r.Db(dbName).TableCreate(tableName).Run(session)
	//if err != nil {
	//	return false, err
	//}

	//if results.Next() {
	//	var row TableResult
	//	results.Scan(&row)
	//	if row.Created == createdSuccssful {

	//		return true, nil
	//	}
	//}
	// -----------------------------------------------------------------

	return false, fmt.Errorf("Unable to create table `%s`.", tableName)
}
Exemple #20
0
func BenchmarkAppend(b *testing.B) {
	for n := 0; n < b.N; n++ {
		_, err := gorethink.Db("test").Table("benchmark_keys_list").Get(table2search).Field("voted").Append(uniuri.New()).Run(session)
		if err != nil {
			b.Log(err)
			b.Fail()
		}
	}
}
Exemple #21
0
func (db *RethinkDBAdapter) teardownTable(tableName string) *CASServerError {
	_, err := r.Db(db.dbName).TableDrop(tableName).Run(db.session)
	if err != nil {
		casError := &FailedToTeardownDatabaseError
		casError.err = &err
		return casError
	}
	return nil
}
Exemple #22
0
// randomly reshuffle all checks that need to be done right now and schedule
// them for a future time. i.e. if the stalker-manager was offline
// for an extended period of time.
// TODO: y u no actually shuffle!
// TODO: optomize Get & Update
func (sm *Manager) startupShuffle() {
	log.Debugln("Reshuffling checks")
	var err error
	rquery := r.Db(STALKERDB).Table("checks").Between(0, time.Now().Unix(), r.BetweenOpts{Index: "next", RightBound: "closed"})
	cursor, err := rquery.Run(sm.rsess)
	defer cursor.Close()
	if err != nil {
		log.Panic(err)
	}

	result := stalker.Check{}

	for cursor.Next(&result) {
		_, err := r.Db(STALKERDB).Table("checks").Get(result.ID).Update(map[string]int{"next": int(time.Now().Unix()) + stalker.RandIntInRange(1, sm.shuffleT)}).RunWrite(sm.rsess)
		stalker.OnlyLogIf(err)
	}

	stalker.OnlyLogIf(cursor.Err())
}
func Example_TableCreate() {
	sess, err := r.Connect(r.ConnectOpts{
		Address: url,
		AuthKey: authKey,
	})

	// Setup database
	r.Db("test").TableDrop("table").Run(sess)

	response, err := r.Db("test").TableCreate("table").RunWrite(sess)
	if err != nil {
		log.Fatalf("Error creating table: %s", err)
	}

	fmt.Printf("%d table created", response.Created)

	// Output:
	// 1 table created
}
Exemple #24
0
func (s *DBSession) CreateTable(name string, tableOpts ...r.TableCreateOpts) error {
	opts := r.TableCreateOpts{}
	if len(tableOpts) > 1 {
		log.Fatalln("createTable only takes 0 or 1 arguments")
	} else if len(tableOpts) == 1 {
		opts = tableOpts[0]
	}

	return r.Db(s.Config.Name).TableCreate(name, opts).Exec(s.S)
}
Exemple #25
0
func getAccountPublicKey(account *models.Account) (*openpgp.Entity, error) {
	if account.PublicKey != "" {
		cursor, err := gorethink.Db(cfg.RethinkDatabase).Table("keys").Get(account.PublicKey).Run(session)
		if err != nil {
			return nil, err
		}
		defer cursor.Close()

		var key *models.Key
		if err := cursor.One(&key); err != nil {
			return nil, err
		}

		keyring, err := openpgp.ReadArmoredKeyRing(strings.NewReader(key.Key))
		if err != nil {
			return nil, err
		}

		return keyring[0], nil
	}

	cursor, err := gorethink.Db(cfg.RethinkDatabase).Table("keys").GetAllByIndex("owner", account.ID).Run(session)
	if err != nil {
		return nil, err
	}
	defer cursor.Close()

	var keys []*models.Key
	if err := cursor.All(&keys); err != nil {
		return nil, err
	}

	if len(keys) == 0 {
		return nil, fmt.Errorf("Recipient has no public key")
	}

	keyring, err := openpgp.ReadArmoredKeyRing(strings.NewReader(keys[0].Key))
	if err != nil {
		return nil, err
	}

	return keyring[0], nil
}
func Example_Get() {
	type Person struct {
		Id        string `gorethink:"id, omitempty"`
		FirstName string `gorethink:"first_name"`
		LastName  string `gorethink:"last_name"`
		Gender    string `gorethink:"gender"`
	}

	sess, err := r.Connect(r.ConnectOpts{
		Address: url,
		AuthKey: authKey,
	})
	if err != nil {
		log.Fatalf("Error connecting to DB: %s", err)
	}

	// Setup table
	r.Db("test").TableDrop("table").Run(sess)
	r.Db("test").TableCreate("table").Run(sess)
	r.Db("test").Table("table").Insert(Person{"1", "John", "Smith", "M"}).Run(sess)

	// Fetch the row from the database
	res, err := r.Db("test").Table("table").Get("1").Run(sess)
	if err != nil {
		log.Fatalf("Error finding person: %s", err)
	}

	if res.IsNil() {
		log.Fatalf("Person not found")
	}

	// Scan query result into the person variable
	var person Person
	err = res.One(&person)
	if err != nil {
		log.Fatalf("Error scanning database result: %s", err)
	}
	fmt.Printf("%s %s (%s)", person.FirstName, person.LastName, person.Gender)

	// Output:
	// John Smith (M)
}
func NewRethinkDBStore(rethinkdbSession *gorethink.Session, db, table string, keyPairs ...[]byte) *RethinkDBStore {
	return &RethinkDBStore{
		Codecs: securecookie.CodecsFromPairs(keyPairs...),
		Options: &sessions.Options{
			Path:   "/",
			MaxAge: 86400 * 30,
		},
		term:             gorethink.Db(db).Table(table),
		rethinkdbSession: rethinkdbSession,
	}
}
Exemple #28
0
/*func (c MemberContainer) String() string {
	var result []string
	for _, member := range c.Members {
		// Format the tags as tag1=v1,tag2=v2,...
		var tagPairs []string
		for name, value := range member.Tags {
			tagPairs = append(tagPairs, fmt.Sprintf("%s=%s", name, value))
		}
		tags := strings.Join(tagPairs, ",")

		line := fmt.Sprintf("%s|%s|%s|%s",
			member.Name, member.Addr, member.Status, tags)

		result = append(result, line)
	}
	output, _ := columnize.SimpleFormat(result)
	return output
}
*/
func getAllMembers() {

	addr := "127.0.0.1:7373"
	client, err := RPCClient(addr)
	//client, err := RPCClient(*rpcAddr)
	if err != nil {
		fmt.Sprintf("Error connecting to Serf agent: %s", err)
		return
	}
	defer client.Close()

	members, err := client.Members()
	if err != nil {
		if debug {
			log.Println("Error retrieving members: %s", err)
		}
		return
	}
	//fmt.Println(members)

	result := MemberContainer{}

	for _, member := range members {

		addr := net.TCPAddr{IP: member.Addr, Port: int(member.Port)}

		result.Members = append(result.Members, Member{
			Name:   member.Name,
			Addr:   addr.String(),
			Port:   member.Port,
			Tags:   member.Tags,
			Status: member.Status,
			Proto: map[string]uint8{
				"min":     member.DelegateMin,
				"max":     member.DelegateMax,
				"version": member.DelegateCur,
			},
		})
	}
	r.Db("enforcer_db").TableDrop(tableMembers).Run(session)
	var tables []string
	tables = append(tables, tableMembers)
	createTable(tables, debug)

	// Insert the new item into the database
	_, err = r.Table(tableMembers).Insert(result.Members).RunWrite(session)
	if err != nil {
		if debug {
			log.Println("Error inserting data in rethindkdb", err)
		}
		return
	}
}
Exemple #29
0
func (m *Manager) initdb() {
	// create tables if needed
	tables := []string{tblNameConfig, tblNameEvents, tblNameAccounts, tblNameRoles, tblNameServiceKeys, tblNameExtensions, tblNameWebhookKeys}
	for _, tbl := range tables {
		_, err := r.Table(tbl).Run(m.session)
		if err != nil {
			if _, err := r.Db(m.database).TableCreate(tbl).Run(m.session); err != nil {
				logger.Fatalf("error creating table: %s", err)
			}
		}
	}
}
Exemple #30
0
func NewRethinkdbSession(address string, database string) (*Rethinkdb, error) {
	var session *rdb.Session
	session, err := rdb.Connect(rdb.ConnectOpts{
		Address:     address,
		Database:    database,
		MaxIdle:     10,
		IdleTimeout: time.Second * 10,
	})

	if err != nil {
		return nil, err
	}

	r := &Rethinkdb{
		session: session,
	}
	// initialize database
	rdb.Db(database).TableCreate(AUTH_TABLE).Run(session)
	rdb.Db(database).TableCreate(TOPIC_TABLE).Run(session)
	rdb.Db(database).TableCreate(POST_TABLE).Run(session)
	rdb.Db(database).TableCreate(USER_TABLE).Run(session)
	return r, nil
}