Esempio n. 1
0
func handleManifestsReplace(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		name := strings.TrimLeft(ps.ByName("name"), "/")
		accept := acceptHeader(r)
		manifest, err := db.Manifest(name)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to upddate manifest: %v", err))
			return
		}

		payload := &models.Manifest{}
		err = decodeRequest(r, payload)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to decode request payload: %v", err))
			return
		}

		if err := db.SaveManifest(manifest); err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to save manifest: %v", err))
			return
		}

		manifest = payload
		manifest.Filename = name

		// manifest updated ok, respond
		respondOK(rw, manifest, accept)
	}
}
Esempio n. 2
0
func handleManifestsUpdate(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		name := strings.TrimLeft(ps.ByName("name"), "/")
		accept := acceptHeader(r)
		manifest, err := db.Manifest(name)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to delete manifest from the datastore: %v", err))
			return
		}

		// decode payload from request body.
		payload := &models.ManifestPayload{}
		err = decodeRequest(r, payload)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to decode request payload: %v", err))
			return
		}

		// update manifest from payload fields.
		manifest.UpdateFromPayload(payload)

		if err := db.SaveManifest(manifest); err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to save manifest: %v", err))
			return
		}

		// manifest updated ok, respond
		respondOK(rw, manifest, accept)
	}
}
Esempio n. 3
0
func handlePkgsinfoReplace(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		name := strings.TrimLeft(ps.ByName("name"), "/")
		accept := acceptHeader(r)
		pkgsinfo, err := db.Pkgsinfo(name)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to upddate pkgsinfo: %v", err))
			return
		}

		payload := &models.PkgsInfo{}
		err = decodeRequest(r, payload)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to decode request payload: %v", err))
			return
		}

		if err := db.SavePkgsinfo(pkgsinfo); err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to save pkginfo: %v", err))
			return
		}

		pkgsinfo = payload
		pkgsinfo.Filename = name

		// manifest updated ok, respond
		respondOK(rw, pkgsinfo, accept)
	}
}
Esempio n. 4
0
func handleManifestsList(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		accept := acceptHeader(r)
		manifests, err := db.AllManifests()
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to fetch manifest list from the datastore: %v", err))
			return
		}
		respondOK(rw, manifests, accept)
	}
}
Esempio n. 5
0
func handleManifestsDelete(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		name := strings.TrimLeft(ps.ByName("name"), "/")
		accept := acceptHeader(r)
		err := db.DeleteManifest(name)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to delete manifest from the datastore: %v", err))
			return
		}
		rw.WriteHeader(http.StatusNoContent)
	}
}
Esempio n. 6
0
func handleManifestsShow(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		name := strings.TrimLeft(ps.ByName("name"), "/")
		accept := acceptHeader(r)
		manifest, err := db.Manifest(name)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to fetch manifest from the datastore: %v", err))
			return
		}
		respondOK(rw, manifest, accept)
	}
}
Esempio n. 7
0
func handlePkgsinfoList(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		accept := acceptHeader(r)
		pkgsinfos, err := db.AllPkgsinfos()
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to fetch pkgsinfo list from the datastore: %v", err))
			return
		}
		// apply any filters
		pkgsinfos = applyPkgsinfoFilters(pkgsinfos, r.URL.Query())
		respondOK(rw, pkgsinfos, accept)
	}
}
Esempio n. 8
0
func handleManifestsCreate(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		accept := acceptHeader(r)
		var manifest *models.Manifest
		var payload struct {
			Filename string `plist:"filename" json:"filename"`
			*models.Manifest
		}

		err := decodeRequest(r, &payload)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to decode request payload: %v", err))
			return
		}

		// filename is required in the payload
		if payload.Filename == "" {
			respondError(rw, http.StatusBadRequest, accept,
				errors.New("the name field is required to create a manifest"))
			return
		}

		// If the body contains a valid manifest, create it
		manifest = payload.Manifest
		manifest.Filename = payload.Filename

		// create manifest in datastore
		_, err = db.NewManifest(payload.Filename)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to create new manifest: %v", err))
			return
		}

		// save the manifest
		if err := db.SaveManifest(manifest); err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to save manifest: %v", err))
			return
		}

		respondCreated(rw, manifest, accept)
	}
}
Esempio n. 9
0
func handlePkgsinfoCreate(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		accept := acceptHeader(r)
		var pkgsinfo *models.PkgsInfo
		var payload struct {
			Filename string `plist:"filename" json:"filename"`
			*models.PkgsInfo
		}
		err := decodeRequest(r, &payload)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to decode request payload: %v", err))
			return
		}

		// filename is required in the payload
		if payload.Filename == "" {
			respondError(rw, http.StatusBadRequest, accept,
				errors.New("The filename field is required to create a pkgsinfo"))
			return
		}

		// If the body contains a valid pkgsinfo, create it
		pkgsinfo = payload.PkgsInfo
		pkgsinfo.Filename = payload.Filename

		// create pkgsinfo in datastore
		_, err = db.NewPkgsinfo(payload.Filename)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to create new pkgsinfo: %v", err))
			return
		}

		// save the pkgsinfo
		if err := db.SavePkgsinfo(pkgsinfo); err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to save pkgsinfo: %v", err))
			return
		}

		respondCreated(rw, pkgsinfo, accept)
	}
}
Esempio n. 10
0
File: pkgs.go Progetto: groob/ape
func handlePkgsCreate(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		accept := acceptHeader(r)

		// process the multipart form
		filename, file, err := processFileUpload(r)
		if err != nil {
			respondError(rw, http.StatusBadRequest, accept, err)
			return
		}

		// save to datastore
		if err = db.AddPkg(filename, file); err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to save file: %v", err))
			return
		}
	}
}
Esempio n. 11
0
File: pkgs.go Progetto: groob/ape
func handlePkgsDelete(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		name := strings.TrimLeft(ps.ByName("name"), "/")
		accept := acceptHeader(r)
		err := db.DeletePkg(name)
		// path error, return not found
		if _, ok := err.(*os.PathError); ok {
			respondError(rw, http.StatusNotFound, accept, err)
			return
		}

		// all other errors
		if err != nil {
			respondError(rw, http.StatusInternalServerError, accept,
				fmt.Errorf("Failed to delete pkgsinfo from the datastore: %v", err))
			return
		}
		rw.WriteHeader(http.StatusNoContent)
	}
}