From 8237c0c56713508ca8b0cb14942b267cb3791958 Mon Sep 17 00:00:00 2001 From: Fabian Reinartz Date: Thu, 15 Oct 2015 12:47:15 +0200 Subject: [PATCH] Implement basic /alerts endpoint --- api.go | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/api.go b/api.go index ed8eeb07..5449b062 100644 --- a/api.go +++ b/api.go @@ -74,15 +74,29 @@ func (e *apiError) Error() string { } func (api *API) listAlerts(w http.ResponseWriter, r *http.Request) { - // alerts, err := api.alerts.GetAll() - // if err != nil { - // respondError(w, apiError{ - // typ: errorBadData, - // err: err, - // }, nil) - // return - // } - // respond(w, alerts) + alerts := api.alerts.GetPending() + defer alerts.Close() + + var ( + err error + res []*types.Alert + ) + // TODO(fabxc): enforce a sensible timeout. + for a := range alerts.Next() { + if err = alerts.Err(); err != nil { + break + } + res = append(res, a) + } + + if err != nil { + respondError(w, apiError{ + typ: errorBadData, + err: err, + }, nil) + return + } + respond(w, types.Alerts(res...)) } func (api *API) addAlerts(w http.ResponseWriter, r *http.Request) {