// PutRegion accepts a request to retrieve information about a particular region. // // PUT /api/regions/:region // func PutRegion(c web.C, w http.ResponseWriter, r *http.Request) { var ( ctx = context.FromC(c) idStr = c.URLParams["region"] region model.Region ) id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { w.WriteHeader(http.StatusBadRequest) return } if !regionFromRequest(c, w, r, ®ion) { return } region.ID = id err = datastore.UpdateRegion(ctx, ®ion) if err != nil { log.FromContext(ctx).WithField("err", err).Error("Error updating region") w.WriteHeader(http.StatusNotFound) return } json.NewEncoder(w).Encode(®ion) }
// ListRegions accepts a request to retrieve a list of regions. // // GET /api/regions // func ListRegions(c web.C, w http.ResponseWriter, r *http.Request) { var ( ctx = context.FromC(c) limit = ToLimit(r) offset = ToOffset(r) ) regions, err := datastore.ListRegions(ctx, limit, offset) if err != nil { log.FromContext(ctx).WithField("err", err).Error("Error listing regions") w.WriteHeader(http.StatusNotFound) return } json.NewEncoder(w).Encode(regions) }
// GetHost accepts a request to retrieve information about a particular host. // // GET /api/hosts/:host // func GetHost(c web.C, w http.ResponseWriter, r *http.Request) { var ( ctx = context.FromC(c) idStr = c.URLParams["host"] ) id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { w.WriteHeader(http.StatusBadRequest) return } host, err := datastore.GetHost(ctx, id) if err != nil { log.FromContext(ctx).WithField("err", err).Error("Error getting host") w.WriteHeader(http.StatusNotFound) return } json.NewEncoder(w).Encode(host) }
// DeleteRegion accepts a request to delete a region. // // DELETE /api/regions/:region // func DeleteRegion(c web.C, w http.ResponseWriter, r *http.Request) { var ( ctx = context.FromC(c) idStr = c.URLParams["region"] ) id, err := strconv.ParseInt(idStr, 10, 64) if err != nil { w.WriteHeader(http.StatusBadRequest) return } err = datastore.DeleteRegion(ctx, id) if err != nil { log.FromContext(ctx).WithField("err", err).Error("Error deleting region") w.WriteHeader(http.StatusNotFound) return } w.WriteHeader(http.StatusNoContent) }