예제 #1
0
파일: main.go 프로젝트: kyleconroy/deckbrew
func Serve() error {
	cfg, err := config.FromEnv()
	if err != nil {
		return err
	}

	// Make sure to keep around some idle connections
	cfg.DB.DB.SetMaxIdleConns(10)

	reader, err := brew.NewReader(cfg)
	if err != nil {
		return err
	}

	return http.ListenAndServe(":"+cfg.Port, vhost.Handler{
		cfg.HostAPI:   api.New(cfg, reader),
		cfg.HostWeb:   web.New(cfg, reader),
		cfg.HostImage: image.New(),
	})
}
예제 #2
0
파일: etl.go 프로젝트: kyleconroy/deckbrew
func SyncCards() error {
	cfg, err := config.FromEnv()
	if err != nil {
		return err
	}
	path := "cards.json"
	log.Println("downloading cards from mtgjson.com")
	err = DownloadCards("http://mtgjson.com/json/AllSets-x.json.zip", path)
	if err != nil {
		return err
	}
	log.Println("loading cards into database")
	collection, err := LoadCollection(path)
	if err != nil {
		return err
	}
	client, err := brew.NewReader(cfg)
	if err != nil {
		return err
	}

	return CreateCollection(cfg.DB, client, collection)
}
예제 #3
0
func TestApi(t *testing.T) {
	cfg, err := config.FromEnv()
	if err != nil {
		t.Fatal(err)
	}

	reader, err := brew.NewReader(cfg)
	if err != nil {
		t.Fatal(err)
	}

	m := New(cfg, reader)

	ts := httptest.NewServer(m)
	defer ts.Close()

	urls := []string{
		"/mtg/cards",
		"/mtg/cards?type=creature",
		"/mtg/cards?subtype=zombie",
		"/mtg/cards?supertype=legendary",
		"/mtg/cards?color=red",
		"/mtg/cards?name=rats",
		"/mtg/cards?set=UNH",
		"/mtg/cards?rarity=mythic",
		"/mtg/cards?rarity=basic",
		"/mtg/cards?oracle=you+win+the+game",
		"/mtg/cards/time-vault",
		"/mtg/cards/typeahead?q=nessian",
		"/mtg/sets",
		"/mtg/sets/UNH",
		"/mtg/colors",
		"/mtg/types",
		"/mtg/supertypes",
		"/mtg/subtypes",
	}

	for _, url := range urls {
		res, err := http.Get(ts.URL + url)
		res.Body.Close()

		if err != nil {
			t.Error(err)
		}

		if res.StatusCode != 200 {
			t.Errorf("Expected %s to return 200, not %d", url, res.StatusCode)
		}
	}

	// Test Random
	res, err := http.Get(ts.URL + "/mtg/cards/random")
	res.Body.Close()
	if err != nil {
		t.Error(err)
	}

	if res.Request.URL.String() == "/mtg/cards/random" {
		t.Errorf("Expected /mtg/cards/random redirect to a new page")
	}

	loadFirstCard := func(u string) (brew.Card, error) {
		var card brew.Card

		res, err := http.Get(ts.URL + u)
		defer res.Body.Close()

		if err != nil {
			return card, err
		}

		if res.StatusCode != 200 {
			return card, fmt.Errorf("Expected %s to return 200, not %d", u, res.StatusCode)
		}

		blob, err := ioutil.ReadAll(res.Body)

		if err != nil {
			return card, err
		}

		var cards []brew.Card

		err = json.Unmarshal(blob, &cards)

		if err != nil {
			return card, err
		}

		return cards[0], nil
	}

	// Test Paging
	pageone, _ := loadFirstCard("/mtg/cards?page=1")
	pagetwo, _ := loadFirstCard("/mtg/cards?page=2")

	if pageone.Id == pagetwo.Id {
		t.Errorf("Page one and two both have the same first card, %s", pageone.Id)
	}
}