2016-06-29 01:26:41 +08:00
|
|
|
package filestack
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
2021-09-29 05:16:32 +08:00
|
|
|
"io"
|
2016-06-29 01:26:41 +08:00
|
|
|
"net/http"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-12-01 03:31:10 +08:00
|
|
|
|
2016-06-29 01:26:41 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type FilestackWebhook struct {
|
|
|
|
|
Path string
|
|
|
|
|
acc telegraf.Accumulator
|
2021-12-01 03:31:10 +08:00
|
|
|
log telegraf.Logger
|
2016-06-29 01:26:41 +08:00
|
|
|
}
|
|
|
|
|
|
2021-12-01 03:31:10 +08:00
|
|
|
func (fs *FilestackWebhook) Register(router *mux.Router, acc telegraf.Accumulator, log telegraf.Logger) {
|
2016-06-29 01:26:41 +08:00
|
|
|
router.HandleFunc(fs.Path, fs.eventHandler).Methods("POST")
|
|
|
|
|
|
2021-12-01 03:31:10 +08:00
|
|
|
fs.log = log
|
|
|
|
|
fs.log.Infof("Started the webhooks_filestack on %s", fs.Path)
|
2016-06-29 01:26:41 +08:00
|
|
|
fs.acc = acc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (fs *FilestackWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
defer r.Body.Close()
|
2021-09-29 05:16:32 +08:00
|
|
|
body, err := io.ReadAll(r.Body)
|
2016-06-29 01:26:41 +08:00
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
event := &FilestackEvent{}
|
|
|
|
|
err = json.Unmarshal(body, event)
|
|
|
|
|
if err != nil {
|
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.acc.AddFields("filestack_webhooks", event.Fields(), event.Tags(), time.Unix(event.TimeStamp, 0))
|
|
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
}
|