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.2KB

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