telegraf/internal/webserver/webserver.go

35 lines
669 B
Go
Raw Normal View History

2016-05-25 17:38:25 +00:00
package webserver
import (
"fmt"
"log"
"net/http"
"sync"
2016-05-25 17:38:25 +00:00
"github.com/gorilla/mux"
)
type Webserver struct {
ServiceAddress string
2016-05-27 08:19:33 +00:00
Router *mux.Router
onceStart sync.Once
2016-05-25 17:38:25 +00:00
}
func NewWebserver(serviceAddress string) *Webserver {
return &Webserver{Router: mux.NewRouter(), ServiceAddress: serviceAddress}
2016-05-25 17:38:25 +00:00
}
func (wb *Webserver) listen() {
log.Printf("Started the webhook server on %s\n", wb.ServiceAddress)
2016-05-27 08:19:33 +00:00
err := http.ListenAndServe(fmt.Sprintf("%s", wb.ServiceAddress), wb.Router)
2016-05-25 17:38:25 +00:00
if err != nil {
log.Printf("Error starting webhook server: %v", err)
2016-05-25 17:38:25 +00:00
}
}
func (wb *Webserver) StartOnce() {
wb.onceStart.Do(func() {
go wb.listen()
})
2016-05-25 17:38:25 +00:00
}