Exemple #1
0
//CreateUserSource returns a configured and connected user source
// an error on failed connection
func CreateUserSource(config *bongo.Config) (*UserSource, error) {
	connection, err := bongo.Connect(config)
	if err != nil {
		return nil, err
	}

	return &UserSource{connection: connection}, nil
}
Exemple #2
0
func init() {
	config := &bongo.Config{
		ConnectionString: "localhost",
		Database:         "meetmo",
	}
	dbConnection, err := bongo.Connect(config)
	if err != nil {
		log.Println("Error connection DB")
	}
	routes = mux.NewRouter()
	setupRoutes(dbConnection)
}
Exemple #3
0
//BootstrapAPI blub
func BootstrapAPI(config *bongo.Config) http.Handler {
	api := api2go.NewAPI("v1")
	connection, err := bongo.Connect(config)
	defer connection.Session.Close()

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

	api.AddResource(User{}, UserSource{connection: connection})

	return api.Handler()
}
Exemple #4
0
func getConnection() *bongo.Connection {
	conf := &bongo.Config{
		ConnectionString: "localhost",
		Database:         "bongoz",
	}

	conn, err := bongo.Connect(conf)

	if err != nil {
		panic(err)
	}

	return conn
}
Exemple #5
0
func main() {
	// mongodb connection
	var dbConfig *bongo.Config
	if dbUri := os.Getenv("MONGOLAB_URI"); dbUri != "" {
		dbConfig = &bongo.Config{ConnectionString: dbUri}
	} else {
		dbConfig = &bongo.Config{ConnectionString: dbUrl, Database: dbName}
	}
	conn, err := bongo.Connect(dbConfig)
	if err != nil {
		log.Fatal(err)
	}

	// oauth server
	oConfig := osin.NewServerConfig()
	oConfig.ErrorStatusCode = http.StatusBadRequest
	// allow grant_type password only
	oConfig.AccessExpiration = tokenExpiration
	oConfig.AllowedAccessTypes = osin.AllowedAccessType{osin.PASSWORD}
	oauthServer := osin.NewServer(oConfig, MongoStorage{conn})

	// TODO: move to some DB config file
	conn.Collection("Category").Collection().EnsureIndex(mgo.Index{
		Key:        []string{"slug"},
		Unique:     true,
		DropDups:   true,
		Background: true,
	})
	conn.Collection(routes.COL_CITIZEN).Collection().EnsureIndex(mgo.Index{
		Key:        []string{"username"},
		Unique:     true,
		DropDups:   true,
		Background: true,
	})

	// api routes config
	router := gin.Default()

	// CORS
	router.Use(cors.Middleware(cors.Config{
		Origins:         "*",
		Methods:         "GET, PUT, POST, DELETE",
		RequestHeaders:  "Origin, Authorization, Content-Type",
		ExposedHeaders:  "",
		MaxAge:          50 * time.Second,
		Credentials:     true,
		ValidateHeaders: false,
	}))

	// disable automatic trailing slash due to errors on client
	router.RedirectTrailingSlash = false

	v1 := router.Group("/v1")
	{
		reports := &routes.Reports{conn}
		r := v1.Group("reports")
		r.GET("", reports.List())
		r.POST("", util.RequireLogin(oauthServer), reports.Create())
		r.GET("/:id", reports.Get())

		categories := &routes.Categories{conn}
		c := v1.Group("categories")
		//c.GET("/", categories.List())
		//c.GET("/tree", categories.List()) // fix pending in httprouter
		c.GET("", categories.GetTree())
		c.GET("/:slug", categories.Get())

		citizens := &routes.Citizens{conn}
		cz := v1.Group("citizens")
		cz.POST("", citizens.Create())
		cz.POST("/login", citizens.Login(oauthServer))
		cz.GET("/:uname", util.RequireLogin(oauthServer), citizens.GetProfile())
		//cz.POST("/logout", citizens.Get())
	}

	// run server on configured port
	router.Run(":" + getPort())
}
Exemple #6
0
			var i int

			for i < maxUsers {
				i++
				user := User{Username: fmt.Sprintf("user_%d", i)}
				idString, err := userSource.Create(user, request)
				Expect(err).ToNot(HaveOccurred())

				if rand.Int()%2 == 0 {
					idsToFind = append(idsToFind, idString)
				}
			}

			By(fmt.Sprintf("Finding %d users", len(idsToFind)))

			resultSet, err := userSource.FindMultiple(idsToFind, request)
			Expect(err).ToNot(HaveOccurred())

			data, ok := resultSet.([]User)
			Expect(ok).To(Equal(true))
			Expect(data).To(HaveLen(len(idsToFind)))
		})
	})

	AfterEach(func() {
		if con, err := bongo.Connect(getDatabaseConfiguration()); err == nil {
			con.Session.DB("soyfer_test").DropDatabase()
		}
	})
})