Beispiel #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)
}
Beispiel #2
0
func main() {

	// Load config file
	config := Config{}
	var data, err = ioutil.ReadFile("config.yml")
	err = yaml.Unmarshal([]byte(data), &config)
	if err != nil {
		log.Fatalf("error: %v", err)
	}

	// Establish connection to DB
	var session *r.Session
	session, err = r.Connect(r.ConnectOpts{
		Address: config.DatabaseURI,
	})
	if err != nil {
		log.Fatalln(err.Error())
	}

	bootstrapDB(session, config)

	defer session.Close()

	log.Println("Connected")

	// Instantiate DAOs
	userDAO := UserDAO{DAO{Session: session}}
	postDAO := PostDAO{DAO{Session: session}}
	sessionDAO := SessionDAO{DAO{Session: session}}

	// Instantiate Controllers
	apiController := APIController{
		Controller{Session: session},
		userDAO,
		postDAO,
		sessionDAO,
	}

	goji.Get("/", apiController.Index)
	goji.Post("/post", apiController.CreatePost)
	goji.Get("/post/:id", apiController.GetPost)

	goji.Serve()

}
func seedRethinkdbChecks(s *r.Session) error {
	r.DB("alerts").TableCreate("checks").RunWrite(s)

	data := []*Check{
		&Check{
			Id:          "elasticsearch-es_heap",
			Type:        "elasticsearch",
			Name:        "es_heap",
			Level:       1,
			Title:       "Elasticsearch Heap",
			Description: "total heap usage by an elasticsearch node",
		},
		&Check{
			Id:          "elasticsearch-es_cluster_status",
			Type:        "elasticsearch",
			Name:        "es_heap",
			Level:       1,
			Title:       "Elasticsearch Cluster Status",
			Description: "cluster status (green/yello/red)",
		},
		&Check{
			Id:          "redis-role",
			Type:        "redis",
			Name:        "redis_role",
			Level:       1,
			Title:       "Redis Role",
			Description: "role of redis server (master/slave)",
		},
	}

	s.Use("alerts")
	for _, check := range data {
		r.Table("checks").Insert(check).RunWrite(s)
	}
	r.Table("checks").IndexCreateFunc("type_name", func(row r.Term) interface{} {
		return []interface{}{row.Field("type"), row.Field("name")}
	}).RunWrite(s)
	return nil
}
Beispiel #4
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
}
Beispiel #5
0
//killSession disconnects from the database
func killSession(session *r.Session) {
	session.Close()
}
	"testing"

	"github.com/StephanDollberg/go-json-rest-middleware-jwt"
	"github.com/ant0ine/go-json-rest/rest"
	"github.com/ant0ine/go-json-rest/rest/test"
	r "github.com/dancannon/gorethink"
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

const database = "travelPlanningTest"

var (
	Response         *httptest.ResponseRecorder
	Request          *http.Request
	testConn         *r.Session
	tst              *testing.T
	loginCredentials = map[string]string{"username": "******", "password": "******"}
)

func TestTravelPlanning(t *testing.T) {
	RegisterFailHandler(Fail)
	RunSpecs(t, "TravelPlanning Suite")
}

var _ = BeforeSuite(func() {
	testConn = NewDBSession(database)
})

var _ = AfterSuite(func() {
	_, err := r.DBDrop(database).RunWrite(testConn)
Beispiel #7
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
	}
}