diff --git a/README.md b/README.md index a3b5750..4d3fd88 100644 --- a/README.md +++ b/README.md @@ -1,5 +1 @@ # am - -* X Go client -* X Command line client: `am https://_:@addr ` -* alert timeout not updated \ No newline at end of file diff --git a/timeout.go b/background.go similarity index 100% rename from timeout.go rename to background.go diff --git a/main.go b/main.go index d3638eb..ad3b5ca 100644 --- a/main.go +++ b/main.go @@ -51,19 +51,19 @@ func Main() { http.HandleFunc("/report", handleReport) // User routes. - routeAdmin("/user/insert", handleUserInsert) - routeUser("/user/list", handleUserList) - routeUser("/user/view/", handleUserView) - routeAdmin("/user/update/", handleUserUpdate) - routeAdmin("/user/delete/", handleUserDelete) + handle_admin("/user/insert", handleUserInsert) + handle_user("/user/list", handleUserList) + handle_user("/user/view/", handleUserView) + handle_admin("/user/update/", handleUserUpdate) + handle_admin("/user/delete/", handleUserDelete) - routeAdmin("/source/insert", handleSourceInsert) - routeUser("/source/list", handleSourceList) - routeUser("/source/view/", handleSourceView) - routeAdmin("/source/update/", handleSourceUpdate) - routeAdmin("/source/delete/", handleSourceDelete) + handle_admin("/source/insert", handleSourceInsert) + handle_user("/source/list", handleSourceList) + handle_user("/source/view/", handleSourceView) + handle_admin("/source/update/", handleSourceUpdate) + handle_admin("/source/delete/", handleSourceDelete) - routeUser("/log/list", handleLogList) + handle_user("/log/list", handleLogList) if port == "443" { http.Serve(autocert.NewListener(host), http.DefaultServeMux) diff --git a/report-handler.go b/report-handler.go new file mode 100644 index 0000000..6912955 --- /dev/null +++ b/report-handler.go @@ -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) + } +} diff --git a/source-server.go b/source-server.go deleted file mode 100644 index bab077b..0000000 --- a/source-server.go +++ /dev/null @@ -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) - -} diff --git a/user-server.go b/user-handlers.go similarity index 97% rename from user-server.go rename to user-handlers.go index 77cc1cf..e1f88e9 100644 --- a/user-server.go +++ b/user-handlers.go @@ -79,8 +79,7 @@ func checkCSRF(w http.ResponseWriter, r *http.Request) bool { return formVal == cookieVal } -// TODO: Rename function. -func routeUser(path string, h func(w http.ResponseWriter, r *http.Request)) { +func handle_user(path string, h func(w http.ResponseWriter, r *http.Request)) { http.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { if _, ok := getReqUser(w, r); !ok { respondNotAuthorized(w) @@ -94,8 +93,7 @@ func routeUser(path string, h func(w http.ResponseWriter, r *http.Request)) { }) } -// TODO: Rename function. -func routeAdmin(path string, h func(w http.ResponseWriter, r *http.Request)) { +func handle_admin(path string, h 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 { respondNotAuthorized(w)