github.com/fanux/shipyard@v0.0.0-20161009071005-6515ce223235/controller/api/events.go (about)

     1  package api
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"strconv"
     7  
     8  	log "github.com/Sirupsen/logrus"
     9  )
    10  
    11  func (a *Api) events(w http.ResponseWriter, r *http.Request) {
    12  	w.Header().Set("content-type", "application/json")
    13  
    14  	limit := -1
    15  	l := r.FormValue("limit")
    16  	if l != "" {
    17  		lt, err := strconv.Atoi(l)
    18  		if err != nil {
    19  			http.Error(w, err.Error(), http.StatusInternalServerError)
    20  			return
    21  		}
    22  		limit = lt
    23  	}
    24  	events, err := a.manager.Events(limit)
    25  	if err != nil {
    26  		http.Error(w, err.Error(), http.StatusInternalServerError)
    27  		return
    28  	}
    29  	if err := json.NewEncoder(w).Encode(events); err != nil {
    30  		http.Error(w, err.Error(), http.StatusInternalServerError)
    31  		return
    32  	}
    33  }
    34  
    35  func (a *Api) purgeEvents(w http.ResponseWriter, r *http.Request) {
    36  	w.Header().Set("content-type", "application/json")
    37  
    38  	if err := a.manager.PurgeEvents(); err != nil {
    39  		http.Error(w, err.Error(), http.StatusInternalServerError)
    40  		return
    41  	}
    42  	log.Info("cluster events purged")
    43  	w.WriteHeader(http.StatusNoContent)
    44  }