Ejemplo n.º 1
0
func main() {
	config := map[string]string{
		"storage:protocol": "tcp",
		"storage:port":     ":6379",
	}

	db, _ := redis.NewDB(config)
	storage := storage.New(db)

	router := httprouter.New()

	/*
	   DATASETS
	*/

	dataset := api.DatasetHandler{storage}

	router.POST("/datasets/", dataset.Create)
	router.GET("/datasets/", dataset.Index)

	/* COLLECTIONS */

	collection := api.CollectionHandler{storage}
	router.POST("/datasets/:dataset/collections/:collection", collection.Create)

	/* INSTANCE */

	instance := api.InstanceHandler{storage}
	router.POST("/datasets/:dataset/collections/:collection/items/", instance.Create)

	log.Fatal(http.ListenAndServe(":8000", router))
}
Ejemplo n.º 2
0
func ExampleDataset_Index() {
	d1 := &storage.Dataset{Name: "Iris"}
	d2 := &storage.Dataset{Name: "Iris"}

	db, _ := redis.NewDB(config)
	storage := storage.New(db)

	storage.Clean()
	storage.Insert(d1)
	storage.Insert(d2)

	req, _ := http.NewRequest("GET", "/datasets/", nil)
	dataset := &api.DatasetHandler{storage}

	resp := httptest.NewRecorder()
	dataset.Index(resp, req, nil)

	datasets := []string{}
	unmarshaller.IoReader(resp.Body, &datasets)

	fmt.Printf("%d\n", resp.Code)
	fmt.Printf("Size %d", len(datasets))

	// Output:
	// 200
	// Size 2
}
Ejemplo n.º 3
0
func ExampleJob_Create() {
	job := &models.Job{}
	body, _ := marshaller.IoReader(job)

	req, err := http.NewRequest("POST", "http://example.com/factories", body)
	if err != nil {
		log.Fatal(err)
	}

	w := httptest.NewRecorder()

	db, err := redis.NewDB(config)
	if err != nil {
		fmt.Printf("ERROR: %s\n", err)
		return
	}

	newCluster := cluster.NewCluster("test", db)
	newCluster.Jobs.Clean()

	newJob := api.JobHandler{newCluster}
	newJob.Create(w, req, nil)

	fmt.Printf("%d\n", newCluster.Jobs.Size())

	// Output:
	// 1
}
Ejemplo n.º 4
0
func ExampleDataset_Create() {
	ds := &storage.Dataset{Name: "Iris"}
	params, _ := marshaller.IoReader(ds)
	req, _ := http.NewRequest("POST", "/datasets/", params)

	db, _ := redis.NewDB(config)
	storage := storage.New(db)
	dataset := &api.DatasetHandler{storage}

	resp := httptest.NewRecorder()
	dataset.Create(resp, req, nil)

	fmt.Printf("%d", resp.Code)

	// Output:
	// 200
}
Ejemplo n.º 5
0
//
// /nodes/ => POST => register new node
//
func ExampleFactories_Register() {
	fa := models.Factory{
		Name: "factory_1",
		Host: "localhost:9001",
	}

	body, _ := marshaller.IoReader(fa)

	req, err := http.NewRequest("POST", "http://example.com/nodes", body)

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

	w := httptest.NewRecorder()

	db, _ := redis.NewDB(config)
	storage := storage.New(db)

	if err != nil {
		fmt.Printf("%s\n", err)
		return
	}

	dataset := storage.NewDataset("puaro")
	storage.Insert(dataset)

	newCluster := cluster.NewCluster("test", db)

	factory := api.FactoryHandler{newCluster}
	factory.Register(w, req, nil)

	fmt.Printf("%d", w.Code)

	newCluster.Factories.Clean()

	// Output:
	// 200
}
Ejemplo n.º 6
0
func ExampleFactories_Index() {
	factory1 := &models.Factory{Name: "factory_1"}
	factory2 := &models.Factory{Name: "factory_2"}
	factory3 := &models.Factory{Name: "factory_3"}

	body, _ := marshaller.IoReader(factory1)

	req, err := http.NewRequest("POST", "http://example.com/factories", body)
	if err != nil {
		log.Fatal(err)
	}

	w := httptest.NewRecorder()

	db, _ := redis.NewDB(config)
	storage := storage.New(db)

	dataset := storage.NewDataset("puaro")
	storage.Insert(dataset)

	newCluster := cluster.NewCluster("test", db)

	newCluster.Register(factory1)
	newCluster.Register(factory2)
	newCluster.Register(factory3)

	factory := api.FactoryHandler{newCluster}
	factory.Index(w, req, nil)

	factoriesResponse := []string{}

	json.Unmarshal([]byte(w.Body.String()), &factoriesResponse)

	fmt.Printf("%d - %d", w.Code, len(factoriesResponse))

	// Output:
	// 200 - 3
}
Ejemplo n.º 7
0
func ExampleInstance_Create() {
	config, _ := puaro.ConfigFromFile("../../configs/test.json")
	ds := &storage.Dataset{Name: "Iris"}

	attrs := &instance.Attributes{
		"attr1": "value1",
		"attr2": "value2",
		"attr3": "value3",
	}

	ps := httprouter.Params{
		httprouter.Param{"dataset", "iris"},
		httprouter.Param{"collection", "satosa"},
	}

	params, _ := marshaller.IoReader(attrs)
	req, _ := http.NewRequest("POST", "/", params)

	db, _ := redis.NewDB(config)
	storage := storage.New(db)

	storage.Clean()

	if err := storage.Insert(ds); err != nil {
		fmt.Printf("%s\n", err)
		return
	}

	instance := api.InstanceHandler{storage}
	resp := httptest.NewRecorder()
	instance.Create(resp, req, ps)

	fmt.Printf("RESPONSE CODE: %d\n", resp.Code)

	// Output:
	// RESPONSE CODE: 200
}