Exemple #1
0
func main() {

	flag.BoolVar(&help, "h", false, "help?")
	flag.Parse()

	if help {
		fmt.Println(helpMessage)
		os.Exit(0)
	}

	var logLevel log.Level

	switch util.GetEnvWithDefault("LOG_LEVEL", "warn") {
	case "debug":
		logLevel = log.DebugLevel
	case "info":
		logLevel = log.InfoLevel
	case "error":
		logLevel = log.ErrorLevel
	default:
		logLevel = log.WarnLevel
	}

	log.SetLevel(logLevel)
	fmt.Printf("Using log level [%v]\n", logLevel)

	roshi := util.GetEnvWithDefault("ROSHI_URL", "http://localhost:6302")
	streamsService, err := service.NewRoshiStreamService(roshi, time.Duration(util.GetEnvIntWithDefault("ROSHI_TIMEOUT", 5))*time.Second)
	if err != nil {
		log.Panic(err)
	}

	authConfig := api.AuthConfig{
		Username: []byte(util.GetEnvWithDefault("AUTH_USERNAME", "ello")),
		Password: []byte(util.GetEnvWithDefault("AUTH_PASSWORD", "password")),
		Enabled:  util.IsEnvPresent("AUTH_ENABLED"),
	}
	log.Infof(authConfig.String())

	if util.IsEnvPresent("LIBRATO_TOKEN") {
		go librato.Librato(metrics.DefaultRegistry,
			10e9, // interval
			os.Getenv("LIBRATO_EMAIL"),    // account owner email address
			os.Getenv("LIBRATO_TOKEN"),    // Librato API token
			os.Getenv("LIBRATO_HOSTNAME"), // source
			[]float64{0.95},               // percentiles to send
			time.Millisecond,              // time unit
		)
	}

	router := httprouter.New()

	streamsController := api.NewStreamController(streamsService, authConfig)
	streamsController.Register(router)

	healthController := api.NewHealthController(startTime, commit, roshi)
	healthController.Register(router)

	n := negroni.New(
		negroni.NewRecovery(),
		nlog.NewCustomMiddleware(logLevel, &log.TextFormatter{}, "web"),
	)
	n.UseHandler(router)

	port := util.GetEnvWithDefault("PORT", "8080")
	serverAt := ":" + port
	n.Run(serverAt)
}
	log.SetLevel(log.DebugLevel)

	router = httprouter.New()

	StreamID, _ := uuid.V4()

	streamService = &mockStreamService{
		internal: generateFakeResponse(StreamID),
	}

	authConfig := api.AuthConfig{
		Username: []byte("ello"),
		Password: []byte("password"),
		Enabled:  false,
	}
	streamController := api.NewStreamController(streamService, authConfig)

	streamController.Register(router)
})

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

func generateFakeResponse(streamID uuid.UUID) []model.StreamItem {
	//fake data
	uuid1, _ := uuid.V4()
	uuid2, _ := uuid.V4()

	return []model.StreamItem{