56 lines
963 B
Go
56 lines
963 B
Go
package am
|
|
|
|
import (
|
|
"net/http"
|
|
)
|
|
|
|
func respondReportError(w http.ResponseWriter, msg string) {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
w.Write([]byte(msg))
|
|
}
|
|
|
|
func handleReport(w http.ResponseWriter, r *http.Request) {
|
|
r.ParseForm()
|
|
|
|
apiKey := r.Form.Get("key")
|
|
|
|
src, err := db.SourceGetByKey(apiKey)
|
|
if err != nil {
|
|
respondNotAuthorized(w)
|
|
return
|
|
}
|
|
|
|
// Always update last seen timestamp.
|
|
_ = db.SourceUpdateLastSeenAt(src.SourceID)
|
|
|
|
action := r.Form.Get("action")
|
|
var actionFunc func(string, string, string)
|
|
|
|
switch action {
|
|
case "ping":
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
|
|
case "log":
|
|
actionFunc = runLogAction
|
|
|
|
case "alert":
|
|
actionFunc = runAlertAction
|
|
|
|
default:
|
|
respondReportError(w, "Unknown action: "+action)
|
|
return
|
|
}
|
|
|
|
e := Entry{
|
|
SourceID: src.SourceID,
|
|
Text: r.Form.Get("text"),
|
|
Alert: action == "alert",
|
|
}
|
|
|
|
_ = db.LogInsert(e)
|
|
actionFunc(src.LogAction, src.Name, e.Text)
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
}
|