示例#1
0
func (r *TodoResource) Delete(res http.ResponseWriter, req *http.Request) {
	id, err := rest.PathInt(req, "id")
	if err != nil {
		rest.SetBadRequestResponse(res)
		return
	}
	var todo Todo

	db, err := r.Db.Get()
	if err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
	if db.First(&todo, id).RecordNotFound() {
		rest.SetNotFoundResponse(res)
		return
	}

	db.Delete(&todo)

	if err := rest.SetNoContentResponse(res); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
示例#2
0
func (r *LocationResource) Add(res http.ResponseWriter, req *http.Request) {
	var location api.Location

	if err := rest.Bind(req, &location); err != nil {
		rest.SetBadRequestResponse(res)
		return
	}
	if location.City == "" || location.State == "" {
		rest.SetBadRequestResponse(res)
		return
	}

	var found api.Location
	if location.Id != 0 && !r.Db.First(&found, location.Id).RecordNotFound() {
		rest.SetConflictResponse(res)
		return
	}
	location.Id = 0

	r.Db.Save(&location)

	if err := r.includeConditions(&location); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
	if err := rest.SetCreatedResponse(res, location, fmt.Sprintf("location/%d", location.Id)); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
示例#3
0
func (r *TodoResource) Update(res http.ResponseWriter, req *http.Request) {
	var todo Todo

	id, err := rest.PathInt(req, "id")
	if err != nil {
		rest.SetBadRequestResponse(res)
		return
	}
	if err := rest.Bind(req, &todo); err != nil {
		rest.SetBadRequestResponse(res)
		return
	}
	todo.Id = id

	var found Todo
	db, err := r.Db.Get()
	if err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
	if db.First(&found, id).RecordNotFound() {
		rest.SetNotFoundResponse(res)
		return
	}

	db.Save(&todo)

	if err := rest.SetOKResponse(res, todo); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}

}
示例#4
0
func (r *TodoResource) GetAll(res http.ResponseWriter, req *http.Request) {
	var todos []Todo

	db, err := r.Db.Get()
	if err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
	db.Find(&todos)

	if err := rest.SetOKResponse(res, todos); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
示例#5
0
func (r *ReportResource) GetAll(res http.ResponseWriter, req *http.Request) {
	c, g := metrics.Snapshot()

	if err := rest.SetOKResponse(res, api.Report{Counters: c, Gauges: g}); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
示例#6
0
func (h *Handler) Bad(res http.ResponseWriter, req *http.Request) {
	bs, _ := ioutil.ReadAll(req.Body)
	s := string(bs)
	log.Printf("HTTP: Bad: '%s'", s)

	h.addStat("Bad", s)
	rest.SetInternalServerErrorResponse(res, fmt.Errorf("Setting Error"))
}
示例#7
0
func (r *LocationResource) Save(res http.ResponseWriter, req *http.Request) {
	var location api.Location

	id, err := rest.PathInt(req, "id")
	if err != nil {
		rest.SetBadRequestResponse(res)
		return
	}
	if err := rest.Bind(req, &location); err != nil {
		rest.SetBadRequestResponse(res)
		return
	}
	if location.Id != 0 && location.Id != id {
		rest.SetBadRequestResponse(res)
		return
	}
	location.Id = id
	if location.City == "" || location.State == "" {
		rest.SetBadRequestResponse(res)
		return
	}

	var found api.Location
	if r.Db.First(&found, id).RecordNotFound() {
		rest.SetNotFoundResponse(res)
		return
	}

	r.Db.Save(&location)

	if err = r.includeConditions(&location); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
	if err := rest.SetOKResponse(res, location); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
示例#8
0
func (r *LocationResource) Find(res http.ResponseWriter, req *http.Request) {
	id, err := rest.PathInt(req, "id")
	if err != nil {
		rest.SetBadRequestResponse(res)
		return
	}
	var location api.Location

	if r.Db.First(&location, id).RecordNotFound() {
		rest.SetNotFoundResponse(res)
		return
	}

	if err = r.includeConditions(&location); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
	if err := rest.SetOKResponse(res, location); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
示例#9
0
func (r *LocationResource) FindAll(res http.ResponseWriter, req *http.Request) {
	var locations []api.Location

	r.Db.Find(&locations)
	for i, _ := range locations {
		r.includeConditions(&locations[i])
	}

	if err := rest.SetOKResponse(res, locations); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
示例#10
0
func (r *TodoResource) Add(res http.ResponseWriter, req *http.Request) {
	var todo Todo

	db, err := r.Db.Get()
	if err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}

	if err := rest.Bind(req, &todo); err != nil {
		log.Print(err)
		rest.SetBadRequestResponse(res)
		return
	}

	db.Create(&todo)

	if err := rest.SetCreatedResponse(res, todo, fmt.Sprintf("todo/%d", todo.Id)); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
示例#11
0
func (r *TodoResource) Health(res http.ResponseWriter, req *http.Request) {
	//2xx => pass, 429 => warn, anything else => critical

	// if we can't get a db connection, set health to Critical
	db, err := r.Db.Get()
	if err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}

	var todos []Todo
	assoc := db.Find(&todos)
	if assoc.Error != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
	// set health to OK
	if err := rest.SetOKResponse(res, nil); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
示例#12
0
func (r *ReportResource) Gauge(res http.ResponseWriter, req *http.Request) {
	var g api.Gauge

	if err := rest.Bind(req, &g); err != nil {
		log.Print(err)
		rest.SetBadRequestResponse(res)
		return
	}

	metrics.Gauge(g.Name).Set(g.N)

	if err := rest.SetOKResponse(res, g); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
示例#13
0
func (r *ReportResource) Counter(res http.ResponseWriter, req *http.Request) {
	var ctr api.Counter

	if err := rest.Bind(req, &ctr); err != nil {
		log.Print(err)
		rest.SetBadRequestResponse(res)
		return
	}

	metrics.Counter(ctr.Name).AddN(ctr.N)

	if err := rest.SetOKResponse(res, ctr); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}