Code cleanup.
parent
082cfca0c3
commit
5b86e618fc
|
@ -1,5 +1 @@
|
||||||
# am
|
# am
|
||||||
|
|
||||||
* X Go client
|
|
||||||
* X Command line client: `am https://_:<api_key>@addr <action> <log or alert>`
|
|
||||||
* alert timeout not updated
|
|
22
main.go
22
main.go
|
@ -51,19 +51,19 @@ func Main() {
|
||||||
http.HandleFunc("/report", handleReport)
|
http.HandleFunc("/report", handleReport)
|
||||||
|
|
||||||
// User routes.
|
// User routes.
|
||||||
routeAdmin("/user/insert", handleUserInsert)
|
handle_admin("/user/insert", handleUserInsert)
|
||||||
routeUser("/user/list", handleUserList)
|
handle_user("/user/list", handleUserList)
|
||||||
routeUser("/user/view/", handleUserView)
|
handle_user("/user/view/", handleUserView)
|
||||||
routeAdmin("/user/update/", handleUserUpdate)
|
handle_admin("/user/update/", handleUserUpdate)
|
||||||
routeAdmin("/user/delete/", handleUserDelete)
|
handle_admin("/user/delete/", handleUserDelete)
|
||||||
|
|
||||||
routeAdmin("/source/insert", handleSourceInsert)
|
handle_admin("/source/insert", handleSourceInsert)
|
||||||
routeUser("/source/list", handleSourceList)
|
handle_user("/source/list", handleSourceList)
|
||||||
routeUser("/source/view/", handleSourceView)
|
handle_user("/source/view/", handleSourceView)
|
||||||
routeAdmin("/source/update/", handleSourceUpdate)
|
handle_admin("/source/update/", handleSourceUpdate)
|
||||||
routeAdmin("/source/delete/", handleSourceDelete)
|
handle_admin("/source/delete/", handleSourceDelete)
|
||||||
|
|
||||||
routeUser("/log/list", handleLogList)
|
handle_user("/log/list", handleLogList)
|
||||||
|
|
||||||
if port == "443" {
|
if port == "443" {
|
||||||
http.Serve(autocert.NewListener(host), http.DefaultServeMux)
|
http.Serve(autocert.NewListener(host), http.DefaultServeMux)
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package am
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
|
||||||
|
action := r.Form.Get("action")
|
||||||
|
switch action {
|
||||||
|
|
||||||
|
case "ping":
|
||||||
|
_ = db.SourceUpdateLastSeenAt(src.SourceID)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
case "log":
|
||||||
|
text := r.Form.Get("text")
|
||||||
|
_ = db.LogInsert(Entry{
|
||||||
|
SourceID: src.SourceID,
|
||||||
|
Text: text,
|
||||||
|
})
|
||||||
|
runLogAction(src.LogAction, src.Name, text)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
||||||
|
case "alert":
|
||||||
|
text := r.Form.Get("text")
|
||||||
|
_ = db.LogInsert(Entry{
|
||||||
|
SourceID: src.SourceID,
|
||||||
|
Text: text,
|
||||||
|
Alert: true,
|
||||||
|
})
|
||||||
|
runAlertAction(src.AlertAction, src.Name, text)
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,55 +0,0 @@
|
||||||
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)
|
|
||||||
|
|
||||||
}
|
|
|
@ -79,8 +79,7 @@ func checkCSRF(w http.ResponseWriter, r *http.Request) bool {
|
||||||
return formVal == cookieVal
|
return formVal == cookieVal
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Rename function.
|
func handle_user(path string, h func(w http.ResponseWriter, r *http.Request)) {
|
||||||
func routeUser(path string, h func(w http.ResponseWriter, r *http.Request)) {
|
|
||||||
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
if _, ok := getReqUser(w, r); !ok {
|
if _, ok := getReqUser(w, r); !ok {
|
||||||
respondNotAuthorized(w)
|
respondNotAuthorized(w)
|
||||||
|
@ -94,8 +93,7 @@ func routeUser(path string, h func(w http.ResponseWriter, r *http.Request)) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Rename function.
|
func handle_admin(path string, h func(w http.ResponseWriter, r *http.Request)) {
|
||||||
func routeAdmin(path string, h func(w http.ResponseWriter, r *http.Request)) {
|
|
||||||
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
|
||||||
if u, ok := getReqUser(w, r); !ok || !u.Admin {
|
if u, ok := getReqUser(w, r); !ok || !u.Admin {
|
||||||
respondNotAuthorized(w)
|
respondNotAuthorized(w)
|
Reference in New Issue