func()

in analytics/server.go [103:140]


func (app *app) ReceiveClusterAndWriteToSQL(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		w.WriteHeader(http.StatusMethodNotAllowed)
		w.Write([]byte("Method not allowed."))
		return
	}
	// Read incoming JSON POST into Go struct
	var c Cluster
	decoder := json.NewDecoder(r.Body)
	err := decoder.Decode(&c)
	if err != nil {
		log.Error(err)
	}
	defer r.Body.Close()

	// Validate request
	if err := validateRequest(c); err != nil {
		w.WriteHeader(http.StatusBadRequest)
		w.Write([]byte(err.Error()))
	}

	// Log received cluster
	log.Infof("Received cluster: ID: %s", c.ClusterId)

	// Write to Cloud SQL
	// TODO - make async?
	if err := writeToCloudSQL(app, c); err != nil {
		log.Error(err)
		w.WriteHeader(http.StatusInternalServerError)
		w.Write([]byte(err.Error()))
		return
	}

	// Give back what the user sent + status 200
	w.WriteHeader(http.StatusOK)
	json.NewEncoder(w).Encode(c)
	return
}