This repository has been archived on 2019-06-27. You can view files and clone it, but cannot push or open issues/pull-requests.
am/report-handler.go

56 lines
1.0 KiB
Go
Raw Permalink Normal View History

2019-06-10 19:31:34 +00:00
package am
import (
"net/http"
"path/filepath"
2019-06-10 19:31:34 +00:00
)
func handleReport(w http.ResponseWriter, r *http.Request) {
apiKey := filepath.Base(r.URL.Path)
2019-06-10 19:31:34 +00:00
src, err := db.SourceGetByKey(apiKey)
if err != nil {
respondNotAuthorized(w)
return
}
r.ParseForm()
2019-06-10 19:31:34 +00:00
action := r.Form.Get("action")
switch action {
case "ping":
2019-06-10 19:36:28 +00:00
if err = db.SourceUpdateLastSeenAt(src.SourceID); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
} else {
w.WriteHeader(http.StatusOK)
}
2019-06-10 19:31:34 +00:00
case "log":
2019-06-10 19:36:28 +00:00
_ = db.SourceUpdateLastSeenAt(src.SourceID)
2019-06-10 19:31:34 +00:00
text := r.Form.Get("text")
_ = db.LogInsert(Entry{
SourceID: src.SourceID,
Text: text,
})
2019-06-11 07:44:35 +00:00
runLogAction(src.Name, text)
2019-06-10 19:31:34 +00:00
w.WriteHeader(http.StatusOK)
case "alert":
2019-06-10 19:36:28 +00:00
_ = db.SourceUpdateLastSeenAt(src.SourceID)
2019-06-10 19:31:34 +00:00
text := r.Form.Get("text")
_ = db.LogInsert(Entry{
SourceID: src.SourceID,
Text: text,
Alert: true,
})
2019-06-11 07:44:35 +00:00
runAlertAction(src.Name, text)
2019-06-10 19:31:34 +00:00
w.WriteHeader(http.StatusOK)
2019-06-10 19:36:28 +00:00
default:
http.Error(w, "Unknown action: "+action, http.StatusBadRequest)
2019-06-10 19:31:34 +00:00
}
}