2016-07-31 23:56:28 +00:00
|
|
|
package particle
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/gorilla/schema"
|
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ParticleWebhook struct {
|
|
|
|
Path string
|
|
|
|
acc telegraf.Accumulator
|
|
|
|
}
|
|
|
|
|
|
|
|
var decoder = schema.NewDecoder()
|
|
|
|
|
|
|
|
func (pwh *ParticleWebhook) Register(router *mux.Router, acc telegraf.Accumulator) {
|
|
|
|
router.HandleFunc(pwh.Path, pwh.eventHandler).Methods("POST")
|
|
|
|
log.Printf("Started '%s' on %s\n", meas, pwh.Path)
|
|
|
|
pwh.acc = acc
|
|
|
|
}
|
|
|
|
|
|
|
|
func (pwh *ParticleWebhook) eventHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
defer r.Body.Close()
|
|
|
|
|
2016-08-01 00:30:34 +00:00
|
|
|
if err := r.ParseForm(); err != nil {
|
2016-07-31 23:56:28 +00:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
e, err := NewEvent(r, &ParticleEvent{})
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := e.NewMetric()
|
|
|
|
pwh.acc.AddFields(meas, p.Fields(), p.Tags(), p.Time())
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewEvent(r *http.Request, event Event) (Event, error) {
|
|
|
|
if err := decoder.Decode(event, r.PostForm); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return event, nil
|
2016-08-01 00:30:34 +00:00
|
|
|
}
|