Esempio n. 1
0
func newServerStack(engine *engine.Engine) *negroni.Negroni {
	context := handlers.NewContext(engine)

	router := mux.NewRouter().StrictSlash(true)
	router.HandleFunc("/", context.IndexHandler).Methods("GET")
	router.HandleFunc("/execute", context.ExecuteHandler).Methods("POST")
	router.HandleFunc("/executews", context.ExecuteWSHandler).Methods("GET")
	router.HandleFunc("/info", context.InfoHandler).Methods("GET")

	server := negroni.New(xrequestid.New(16),
		context,
		negronilogrus.NewCustomMiddleware(logrus.InfoLevel, &logrus.JSONFormatter{}, "straitjacket"),
		cors.Default(),
		negroni.NewStatic(http.Dir("public")))
	server.UseHandler(router)
	return server
}
Esempio n. 2
0
File: main.go Progetto: 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)
}