github.com/xmidt-org/webpa-common@v1.11.9/device/drain/status.go (about)

     1  package drain
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  
     7  	"github.com/xmidt-org/webpa-common/xhttp"
     8  )
     9  
    10  // Status returns a JSON message describing the status of the drain job
    11  type Status struct {
    12  	Drainer Interface
    13  }
    14  
    15  func (s *Status) ServeHTTP(response http.ResponseWriter, request *http.Request) {
    16  	var (
    17  		active, job, progress = s.Drainer.Status()
    18  		message, err          = json.Marshal(
    19  			map[string]interface{}{
    20  				"active":   active,
    21  				"job":      job.ToMap(),
    22  				"progress": progress,
    23  			},
    24  		)
    25  	)
    26  
    27  	if err != nil {
    28  		xhttp.WriteError(response, http.StatusInternalServerError, err)
    29  		return
    30  	}
    31  
    32  	response.Header().Set("Content-Type", "application/json")
    33  	response.WriteHeader(http.StatusOK)
    34  	response.Write(message)
    35  }