Ejemplo n.º 1
0
func main() {

	config := a.NewConfig("config.gcfg")
	echo := echo.New()
	s := slack.New(config.Slack.Token)

	var session *r.Session

	session, err := r.Connect(r.ConnectOpts{
		Address:  fmt.Sprint(config.Database.URL, ":", config.Database.Port),
		Database: config.Database.Name,
		MaxIdle:  10,
		MaxOpen:  10,
	})
	if err != nil {
		log.Fatalln(err.Error())
	}
	session.SetMaxOpenConns(5)

	r.DBCreate(config.Database.Name).Exec(session)
	if err != nil {
		log.Println(err)
	}

	_, err = r.DB(config.Database.Name).TableCreate("quotes").RunWrite(session)
	if err != nil {
		fmt.Print(err)
	}
	_, err = r.DB(config.Database.Name).TableCreate("activities").RunWrite(session)
	if err != nil {
		fmt.Print(err)
	}

	// Middleware
	echo.Use(mw.Logger())
	echo.Use(mw.Recover())

	appcontext := &a.AppContext{
		Slack:  s,
		Config: config,
		Storage: &storage.Storage{
			Name:    "quotes",
			URL:     "192.168.10.10",
			Session: session,
		},
	}

	go appcontext.Monitor()

	//Routes
	a.Route(echo, appcontext)

	addr := fmt.Sprintf(":%d", config.App.Port)
	log.Printf("Starting server on: %s", addr)
	echo.Run(addr)
}
Ejemplo n.º 2
0
func initDb() {
	var session *r.Session

	session, err := r.Connect(r.ConnectOpts{
		Address: "localhost:28015",
	})
	if err != nil {
		log.Fatalln(err.Error())
	}

	session.SetMaxOpenConns(5)

	r.TableDrop("cars").Run(session)

	resp, err := r.TableCreate("cars").RunWrite(session)
	if err != nil {
		log.Println(err)
	}

	fmt.Printf("%d table created\n", resp.TablesCreated)

	s = session
}
Ejemplo n.º 3
0
func (bmo *BMO) Compute(input *os.File) {

	var err error
	var cur *r.Cursor
	var session *r.Session

	// set up database connection pool
	session, err = r.Connect(r.ConnectOpts{
		Addresses:     bmo.nodes,
		Database:      bmo.database,
		DiscoverHosts: true,
	})
	session.SetMaxOpenConns(POOL_SIZE)
	if err != nil {
		log.Fatalln(err)
	}
	// ensure table is present
	var tableNames []string
	cur, err = r.DB(bmo.database).TableList().Run(session)
	if err != nil {
		log.Fatalln(err)
	}
	cur.All(&tableNames)
	set := make(map[string]bool)
	for _, v := range tableNames {
		set[v] = true
	}
	if !set[bmo.table] {
		log.Println("Creating table ", bmo.table)
		_, err = r.DB(bmo.database).TableCreate(bmo.table).RunWrite(session)
		if err != nil {
			log.Fatalln("Error creating table: ", err)
			os.Exit(1)
		}
	}

	// deliver the messages
	decoder := json.NewDecoder(input)
	ms := make([]Message, INSERT_BATCH_SIZE)
	var m *Message
	var i uint64
	var ignoreLast bool

	pool, _ := tunny.CreatePoolGeneric(POOL_SIZE).Open()
	defer pool.Close()

	table := r.Table(bmo.table)
	insertOptions := r.InsertOpts{Durability: "soft"}

	insert := func() {
		j := i
		if !ignoreLast {
			j += 1
		}
		_, err = table.Insert(ms[:j], insertOptions).RunWrite(session)
		if err != nil {
			log.Fatal(err)
			os.Exit(1)
		}
	}

	for {
		i = bmo.seq % INSERT_BATCH_SIZE
		m = &ms[i]
		err = decoder.Decode(&m)

		switch {
		case err == io.EOF:
			ignoreLast = true
			pool.SendWork(insert)
			return
		case err != nil:
			ignoreLast = true
			pool.SendWork(insert)
			log.Fatal("Can't parse json input, \"", err, "\". Object #", bmo.seq, ", after ", m)
			os.Exit(1)
		default:
			if i+1 == INSERT_BATCH_SIZE {
				ignoreLast = false
				pool.SendWork(insert)
			}
		}

		bmo.seq += 1
	}
}