示例#1
0
func TestMain(m *testing.M) {
	// set up a mock API that apiplexy proxies to
	mockAPI := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
		res.WriteHeader(http.StatusOK)
		res.Write([]byte("API-OK"))
	}))
	defer mockAPI.Close()
	log.Printf("Launched mock API at %s.\n", mockAPI.URL)

	// set up apiplexy
	config := apiplexy.ApiplexConfig{}
	if err := yaml.Unmarshal([]byte(yaml_config), &config); err != nil {
		log.Fatalln(err)
	}
	config.Serve.Upstreams[0] = mockAPI.URL
	a, err := apiplexy.New(config)
	if err != nil {
		log.Fatalln(err)
	}
	ap = a

	r, _ := redis.Dial("tcp", config.Redis.Host+":"+strconv.Itoa(config.Redis.Port))
	r.Do("SELECT", 1)
	rd = r

	result := m.Run()

	// after testing, clean out database for future tests
	r.Do("FLUSHDB")

	os.Exit(result)
}
示例#2
0
func start(c *cli.Context) {
	configPath := c.String("config")
	yml, err := ioutil.ReadFile(os.ExpandEnv(configPath))
	if err != nil {
		fmt.Fprintf(os.Stderr, "Couldn't read config file: %s\n", err.Error())
		os.Exit(1)
	}
	config := apiplexy.ApiplexConfig{}
	err = yaml.Unmarshal(yml, &config)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Couldn't parse configuration: %s\n", err.Error())
		os.Exit(1)
	}

	ap, err := apiplexy.New(config)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Couldn't initialize API proxy. %s\n", err.Error())
		os.Exit(2)
	}

	server := &http.Server{
		Addr:    "0.0.0.0:" + strconv.Itoa(config.Serve.Port),
		Handler: ap,
	}
	fmt.Printf("Running server on port %d.\n", config.Serve.Port)
	server.ListenAndServe()
}
示例#3
0
func initApiplex(configPath string) (http.Handler, apiplexy.ApiplexConfig, error) {
	yml, err := ioutil.ReadFile(os.ExpandEnv(configPath))
	config := apiplexy.ApiplexConfig{}
	if err != nil {
		return nil, config, fmt.Errorf("Couldn't read config file: %s\n", err.Error())
	}
	err = yaml.Unmarshal(yml, &config)
	if err != nil {
		return nil, config, fmt.Errorf("Couldn't parse configuration: %s\n", err.Error())
	}
	ap, err := apiplexy.New(config)
	if err != nil {
		return nil, config, fmt.Errorf("Couldn't initialize API proxy. %s\n", err.Error())
	}
	return ap, config, nil
}