コード例 #1
0
ファイル: env_test.go プロジェクト: ello/streams
func TestIsEnvPresent(t *testing.T) {
	key := "AbC_123"
	key2 := "AAAZZZ___"
	val := "zzddzz023adfg12345"
	_ = os.Setenv(key, val)

	result := util.IsEnvPresent(key)
	if !result {
		t.Error("Key is present, result should be true")
	}

	result = util.IsEnvPresent(key2)
	if result {
		t.Error("Key is not present, result should be false")
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: ello/streams
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)
}