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/source-server.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)
}