Example #1
0
func connectRDB() {
	var err error
	var session *r.Session

	c, err := dockertest.ConnectToRethinkDB(20, time.Second, func(url string) bool {
		if session, err = r.Connect(r.ConnectOpts{Address: url, Database: "hydra"}); err != nil {
			return false
		} else if _, err = r.DBCreate("hydra").RunWrite(session); err != nil {
			log.Printf("Database exists: %s", err)
			return false
		} else if _, err = r.TableCreate("hydra_policies").RunWrite(session); err != nil {
			log.Printf("Could not create table: %s", err)
			return false
		}

		rethinkManager = &RethinkManager{
			Session:  session,
			Table:    r.Table("hydra_policies"),
			Policies: make(map[string]Policy),
		}

		rethinkManager.Watch(context.Background())
		time.Sleep(time.Second)
		return true
	})
	if err != nil {
		log.Fatalf("Could not connect to database: %s", err)
	}

	containers = append(containers, c)
	managers["rethink"] = rethinkManager
}
Example #2
0
func NewManager(addr string, database string, authKey string, client *dockerclient.DockerClient, disableUsageInfo bool, authenticator auth.Authenticator) (Manager, error) {
	log.Debug("setting up rethinkdb session")
	session, err := r.Connect(r.ConnectOpts{
		Address:  addr,
		Database: database,
		AuthKey:  authKey,
	})
	if err != nil {
		return nil, err
	}
	log.Info("checking database")

	r.DBCreate(database).Run(session)
	m := &DefaultManager{
		database:         database,
		authKey:          authKey,
		session:          session,
		authenticator:    authenticator,
		store:            store,
		client:           client,
		storeKey:         storeKey,
		disableUsageInfo: disableUsageInfo,
	}
	m.initdb()
	m.init()
	return m, nil
}
func (suite *MutationUpdateSuite) SetupTest() {
	suite.T().Log("Setting up MutationUpdateSuite")
	// Use imports to prevent errors
	_ = time.Time{}
	_ = compare.AnythingIsFine

	session, err := r.Connect(r.ConnectOpts{
		Address: url,
	})
	suite.Require().NoError(err, "Error returned when connecting to server")
	suite.session = session

	r.DBDrop("test").Exec(suite.session)
	err = r.DBCreate("test").Exec(suite.session)
	suite.Require().NoError(err)
	err = r.DB("test").Wait().Exec(suite.session)
	suite.Require().NoError(err)

	r.DB("test").TableDrop("tbl").Exec(suite.session)
	err = r.DB("test").TableCreate("tbl").Exec(suite.session)
	suite.Require().NoError(err)
	err = r.DB("test").Table("tbl").Wait().Exec(suite.session)
	suite.Require().NoError(err)
	r.DB("test").TableDrop("tbl2").Exec(suite.session)
	err = r.DB("test").TableCreate("tbl2").Exec(suite.session)
	suite.Require().NoError(err)
	err = r.DB("test").Table("tbl2").Wait().Exec(suite.session)
	suite.Require().NoError(err)
}
Example #4
0
func main() {

	//on initialise nos routes
	todo := Todo{}
	todo.InitTodo()

	var err error

	session, err = r.Connect(r.ConnectOpts{
		Address:  "localhost:28015",
		Database: "Todolist",
	})
	createDbAndTables()

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

	config := swagger.Config{
		WebServices:    restful.RegisteredWebServices(), // you control what services are visible
		WebServicesUrl: "http://localhost:9000",
		ApiPath:        "/apidocs.json",

		// Optionally, specifiy where the UI is located
		SwaggerPath:     "/apidocs/",
		SwaggerFilePath: "swagger-ui/dist"}

	swagger.InstallSwaggerService(config)
	http.ListenAndServe(":9000", nil)
}
Example #5
0
func TestMain(m *testing.M) {
	var session *r.Session
	var err error

	c, err := dockertest.ConnectToRethinkDB(20, time.Millisecond*500, func(url string) bool {
		if session, err = r.Connect(r.ConnectOpts{Address: url, Database: "hydra"}); err != nil {
			return false
		} else if _, err = r.DBCreate("hydra").RunWrite(session); err != nil {
			logrus.Printf("Database exists: %s", err)
			return false
		} else if _, err = r.TableCreate("hydra_authorize_code").RunWrite(session); err != nil {
			logrus.Printf("Could not create table: %s", err)
			return false
		} else if _, err = r.TableCreate("hydra_id_sessions").RunWrite(session); err != nil {
			logrus.Printf("Could not create table: %s", err)
			return false
		} else if _, err = r.TableCreate("hydra_access_token").RunWrite(session); err != nil {
			logrus.Printf("Could not create table: %s", err)
			return false
		} else if _, err = r.TableCreate("hydra_implicit").RunWrite(session); err != nil {
			logrus.Printf("Could not create table: %s", err)
			return false
		} else if _, err = r.TableCreate("hydra_refresh_token").RunWrite(session); err != nil {
			logrus.Printf("Could not create table: %s", err)
			return false
		}

		rethinkManager = &FositeRehinkDBStore{
			Session:             session,
			AuthorizeCodesTable: r.Table("hydra_authorize_code"),
			IDSessionsTable:     r.Table("hydra_id_sessions"),
			AccessTokensTable:   r.Table("hydra_access_token"),
			ImplicitTable:       r.Table("hydra_implicit"),
			RefreshTokensTable:  r.Table("hydra_refresh_token"),
			AuthorizeCodes:      make(RDBItems),
			IDSessions:          make(RDBItems),
			AccessTokens:        make(RDBItems),
			Implicit:            make(RDBItems),
			RefreshTokens:       make(RDBItems),
		}
		rethinkManager.Watch(context.Background())
		time.Sleep(500 * time.Millisecond)
		return true
	})
	if session != nil {
		defer session.Close()
	}
	if err != nil {
		logrus.Fatalf("Could not connect to database: %s", err)
	}
	clientManagers["rethink"] = rethinkManager

	retCode := m.Run()
	c.KillRemove()
	os.Exit(retCode)
}
Example #6
0
func ExampleConnect() {
	var err error

	session, err = r.Connect(r.ConnectOpts{
		Address: url,
	})
	if err != nil {
		log.Fatalln(err.Error())
	}
}
Example #7
0
func ExampleConnect_cluster() {
	var err error

	session, err = r.Connect(r.ConnectOpts{
		Addresses: []string{url},
		//  Addresses: []string{url1, url2, url3, ...},
	})
	if err != nil {
		log.Fatalln(err.Error())
	}
}
Example #8
0
func ExampleConnect_connectionPool() {
	var err error

	session, err = r.Connect(r.ConnectOpts{
		Address:    url,
		InitialCap: 10,
		MaxOpen:    10,
	})
	if err != nil {
		log.Fatalln(err.Error())
	}
}
Example #9
0
func InitRethinkDB() {
	var err error

	Sess, err = r.Connect(r.ConnectOpts{
		Address:  "localhost:28015",
		Database: "SpottedWays",
	})
	if err != nil {
		log.Fatal(err)
	}
	createDBAndTables()
}
Example #10
0
func init() {
	var errC error
	conn, errC = r.Connect(r.ConnectOpts{
		Address:  "127.0.0.1:28015",
		Database: "test",
		Username: "******",
		Password: "******",
	})
	if errC != nil {
		fmt.Println("error en la conexion")
		return
	} else {
		fmt.Println("conexion establecida a la DB")
	}
}
Example #11
0
func (h *hub) run() {
	defer h.ticker.stop()

	// Open a connection to rethinkdb
	var err error
	h.session, err = r.Connect(r.ConnectOpts{
		Address:  "localhost:28015",
		Database: "pinghub",
	})
	if err != nil {
		log.Fatalln(err.Error())
	}
	defer h.session.Close()

	// Subscribe to the changefeed
	cursor, err := r.Table("pinghub").Changes().Field("new_val").Run(h.session)
	if err != nil {
		log.Fatalln(err)
	}
	defer cursor.Close()

	// Pipe changefeed messages into channel queues
	go func() {
		var rec record
		for cursor.Next(&rec) {
			if rec.Id != "" && rec.Text != "" {
				if channel, ok := h.channels[rec.Id]; ok {
					channel.queue <- command{cmd: BROADCAST, text: []byte(rec.Text)}
				}
			}
		}
	}()

	for cmd := range h.queue {
		// Forward cmds to their path's channel queues.
		switch cmd.cmd {
		case SUBSCRIBE:
			h.subscribe(cmd)
		case PUBLISH:
			h.publish(cmd)
		case REMOVE:
			h.remove(cmd)
		default:
			panic(fmt.Sprintf("unexpected hub cmd: %v\n", cmd))
		}
	}
}
Example #12
0
func TestMain(m *testing.M) {
	var session *r.Session
	var err error

	c, err := dockertest.ConnectToRethinkDB(20, time.Second, func(url string) bool {
		if session, err = r.Connect(r.ConnectOpts{Address: url, Database: "hydra"}); err != nil {
			return false
		} else if _, err = r.DBCreate("hydra").RunWrite(session); err != nil {
			log.Printf("Database exists: %s", err)
			return false
		} else if _, err = r.TableCreate("hydra_keys").RunWrite(session); err != nil {
			log.Printf("Could not create table: %s", err)
			return false
		}

		key, err := rand.RandomBytes(32)
		if err != nil {
			log.Printf("Could not watch: %s", err)
			return false
		}
		rethinkManager = &RethinkManager{
			Keys:    map[string]jose.JsonWebKeySet{},
			Session: session,
			Table:   r.Table("hydra_keys"),
			Cipher: &AEAD{
				Key: key,
			},
		}
		rethinkManager.Watch(context.Background())
		time.Sleep(100 * time.Millisecond)
		return true
	})
	if session != nil {
		defer session.Close()
	}
	if err != nil {
		log.Fatalf("Could not connect to database: %s", err)
	}
	managers["rethink"] = rethinkManager

	retCode := m.Run()
	c.KillRemove()
	os.Exit(retCode)
}
Example #13
0
func (c *RethinkDBConnection) GetSession() *r.Session {
	if c.session != nil {
		return c.session
	}

	var err error
	var username, password string
	database := c.URL.Path[1:]
	if c.URL.User != nil {
		password, _ = c.URL.User.Password()
		username = c.URL.User.Username()
	}

	if err := pkg.Retry(time.Second*15, time.Minute*2, func() error {
		logrus.Infof("Connecting with RethinkDB: %s (%s) (%s)", c.URL.String(), c.URL.Host, database)
		if c.session, err = r.Connect(r.ConnectOpts{
			Address:  c.URL.Host,
			Username: username,
			Password: password,
		}); err != nil {
			return errors.Errorf("Could not connect to RethinkDB: %s", err)
		}

		if _, err := r.DBList().Contains(database).Do(func(e r.Term) r.Term {
			return r.Branch(
				e,
				map[string]interface{}{"dbs_created": 0},
				r.DBCreate(database),
			)
		}).RunWrite(c.session); err != nil {
			return errors.Errorf("Could not create database: %s", err)
		}

		c.session.Use(database)
		logrus.Infof("Connected to RethinkDB!")
		return nil
	}); err != nil {
		logrus.Fatalf("Could not connect to RethinkDB: %s", err)
	}

	return c.session
}
Example #14
0
func TestMain(m *testing.M) {
	var session *r.Session
	var err error

	c, err := dockertest.ConnectToRethinkDB(20, time.Second, func(url string) bool {
		if session, err = r.Connect(r.ConnectOpts{Address: url, Database: "hydra"}); err != nil {
			return false
		} else if _, err = r.DBCreate("hydra").RunWrite(session); err != nil {
			log.Printf("Database exists: %s", err)
			return false
		} else if _, err = r.TableCreate("hydra_clients").RunWrite(session); err != nil {
			log.Printf("Could not create table: %s", err)
			return false
		}

		rethinkManager = &RethinkManager{
			Session: session,
			Table:   r.Table("hydra_clients"),
			Clients: make(map[string]*fosite.DefaultClient),
			Hasher: &hash.BCrypt{
				// Low workfactor reduces test time
				WorkFactor: 4,
			},
		}
		rethinkManager.Watch(context.Background())
		time.Sleep(100 * time.Millisecond)
		return true
	})
	if session != nil {
		defer session.Close()
	}
	if err != nil {
		log.Fatalf("Could not connect to database: %s", err)
	}
	clientManagers["rethink"] = rethinkManager

	retCode := m.Run()
	c.KillRemove()
	os.Exit(retCode)
}
Example #15
0
func Example() {
	session, err := r.Connect(r.ConnectOpts{
		Address: url,
	})
	if err != nil {
		log.Fatalln(err)
	}

	res, err := r.Expr("Hello World").Run(session)
	if err != nil {
		log.Fatalln(err)
	}

	var response string
	err = res.One(&response)
	if err != nil {
		log.Fatalln(err)
	}

	fmt.Println(response)

	// Output:
	// Hello World
}