56 lines
1.0 KiB
Go
56 lines
1.0 KiB
Go
package am
|
|
|
|
import (
|
|
"net/http"
|
|
"path/filepath"
|
|
)
|
|
|
|
func handleReport(w http.ResponseWriter, r *http.Request) {
|
|
apiKey := filepath.Base(r.URL.Path)
|
|
|
|
src, err := db.SourceGetByKey(apiKey)
|
|
if err != nil {
|
|
respondNotAuthorized(w)
|
|
return
|
|
}
|
|
|
|
r.ParseForm()
|
|
|
|
action := r.Form.Get("action")
|
|
switch action {
|
|
|
|
case "ping":
|
|
if err = db.SourceUpdateLastSeenAt(src.SourceID); err != nil {
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
} else {
|
|
w.WriteHeader(http.StatusOK)
|
|
}
|
|
|
|
case "log":
|
|
_ = db.SourceUpdateLastSeenAt(src.SourceID)
|
|
|
|
text := r.Form.Get("text")
|
|
_ = db.LogInsert(Entry{
|
|
SourceID: src.SourceID,
|
|
Text: text,
|
|
})
|
|
runLogAction(src.Name, text)
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
case "alert":
|
|
_ = db.SourceUpdateLastSeenAt(src.SourceID)
|
|
|
|
text := r.Form.Get("text")
|
|
_ = db.LogInsert(Entry{
|
|
SourceID: src.SourceID,
|
|
Text: text,
|
|
Alert: true,
|
|
})
|
|
runAlertAction(src.Name, text)
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
default:
|
|
http.Error(w, "Unknown action: "+action, http.StatusBadRequest)
|
|
}
|
|
}
|