http.go 823 B

12345678910111213141516171819202122232425262728293031323334
  1. package nebula
  2. import (
  3. "fmt"
  4. "net/http"
  5. _ "net/http/pprof"
  6. "github.com/sirupsen/logrus"
  7. "github.com/slackhq/nebula/config"
  8. )
  9. // startHttp returns a function to start an http server with pprof support and optionally a provided stats
  10. // http handler.
  11. func startHttp(l *logrus.Logger, c *config.C, listen string, statsHandler statsHandlerFunc) (func(), error) {
  12. if listen == "" {
  13. return nil, nil
  14. }
  15. var statsPath string
  16. if statsHandler != nil {
  17. statsPath = c.GetString("stats.path", "")
  18. if statsPath == "" {
  19. return nil, fmt.Errorf("stats.path should not be empty")
  20. }
  21. }
  22. return func() {
  23. l.Infof("Go pprof handler listening on %s at /debug/pprof", listen)
  24. if statsHandler != nil {
  25. http.Handle(statsPath, statsHandler(listen, statsPath))
  26. }
  27. l.Fatal(http.ListenAndServe(listen, nil))
  28. }, nil
  29. }