Add the /events endpoint to return all events
@@ -7,6 +7,7 @@ import (
"github.com/gorilla/mux"
"github.com/sensu/sensu-go/backend/store"
+ "github.com/sensu/sensu-go/types"
)
// EventsController handles requests for /events
@@ -16,6 +17,7 @@ type EventsController struct {
// Register the EventsController with a mux.Router.
func (c *EventsController) Register(r *mux.Router) {
+ r.HandleFunc("/events", c.events).Methods(http.MethodGet)
r.HandleFunc("/events/{entity}", c.entityEvents).Methods(http.MethodGet)
r.HandleFunc("/events/{entity}/{check}", c.entityCheckEvents).Methods(http.MethodGet)
// TODO(greg): Handle a PUT to /events
@@ -72,3 +74,26 @@ func (c *EventsController) entityCheckEvents(w http.ResponseWriter, r *http.Requ
w.Header().Set("Content-Type", "application/json")
fmt.Fprint(w, string(jsonStr))
}
+
+func (c *EventsController) events(w http.ResponseWriter, r *http.Request) {
+ events, err := c.Store.GetEvents()
+ if err != nil {
+ http.Error(w, "error getting events", http.StatusInternalServerError)
+ return
+ }
+
+ // If the events slice is empty, we want to return an empty array instead
+ // of null for easier consumption
+ if len(events) == 0 {
+ events = make([]*types.Event, 0)
+ }
+
+ jsonStr, err := json.Marshal(events)
+ if err != nil {
+ http.Error(w, "error marshalling response", http.StatusInternalServerError)
+ return
+ }
+
+ w.Header().Set("Content-Type", "application/json")
+ fmt.Fprint(w, string(jsonStr))
+}
