You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

httplistener.go 1.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package httplistener
  2. import (
  3. "github.com/irccloud/go-ircevent"
  4. "github.com/juju/loggo"
  5. "github.com/spf13/viper"
  6. "net/http"
  7. )
  8. var log = loggo.GetLogger("HTTPListener")
  9. type HTTPListener struct {
  10. http http.Server
  11. irc *irc.Connection
  12. }
  13. func New(irc *irc.Connection) (*HTTPListener, error) {
  14. hl := HTTPListener{}
  15. hl.irc = irc
  16. hl.http = http.Server{Addr: viper.GetString("http.listen")}
  17. mux := http.NewServeMux()
  18. mux.HandleFunc("/send", hl.genericHandler)
  19. if viper.IsSet("http.listeners.grafana") {
  20. log.Infof("Listening for Grafana webhooks at /grafana")
  21. mux.HandleFunc("/grafana", hl.grafanaAlertHandler)
  22. }
  23. if viper.IsSet("http.listeners.github") {
  24. log.Infof("Listening for GitHub webhooks at /github")
  25. mux.HandleFunc("/github", hl.githubHandler)
  26. }
  27. hl.http.Handler = mux
  28. log.Infof("Listening for HTTP requests on %s", viper.GetString("http.listen"))
  29. if viper.GetBool("http.tls") {
  30. go hl.http.ListenAndServeTLS(viper.GetString("http.tls_cert"), viper.GetString("http.tls_key"))
  31. } else {
  32. go hl.http.ListenAndServe()
  33. }
  34. return &hl, nil
  35. }