예제 #1
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
	}

}
예제 #2
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
	}
}
예제 #3
0
func (h *Handler) Bar(res http.ResponseWriter, req *http.Request) {
	bs, _ := ioutil.ReadAll(req.Body)
	s := string(bs)
	log.Printf("HTTP: Bar: '%s'", s)

	h.addStat("Bar", s)
	rest.SetOKResponse(res, nil)
}
예제 #4
0
func (h *Handler) Slow(res http.ResponseWriter, req *http.Request) {
	bs, _ := ioutil.ReadAll(req.Body)
	s := string(bs)
	log.Printf("HTTP: Slow: '%s'", s)

	time.Sleep(5000 * time.Millisecond)

	h.addStat("Slow", s)
	rest.SetOKResponse(res, nil)
}
예제 #5
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
	}
}
예제 #6
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
	}
}
예제 #7
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
	}
}
예제 #8
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
	}
}
예제 #9
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
	}
}
예제 #10
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
	}
}
예제 #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 *TodoResource) Get(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
	}

	if err := rest.SetOKResponse(res, todo); err != nil {
		rest.SetInternalServerErrorResponse(res, err)
		return
	}
}
예제 #13
0
func Handle(res http.ResponseWriter, req *http.Request) {
	rest.SetOKResponse(res, nil)
}