本文整理汇总了Golang中github.com/groob/ape/datastore.Datastore类的典型用法代码示例。如果您正苦于以下问题:Golang Datastore类的具体用法?Golang Datastore怎么用?Golang Datastore使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Datastore类的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: handleManifestsReplace
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)
}
}
示例2: handleManifestsUpdate
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)
}
}
示例3: handlePkgsinfoReplace
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)
}
}
示例4: handleManifestsList
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)
}
}
示例5: handleManifestsDelete
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)
}
}
示例6: handleManifestsShow
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)
}
}
示例7: handlePkgsinfoList
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)
}
}
示例8: handleManifestsCreate
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)
}
}
示例9: handlePkgsinfoCreate
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)
}
}
示例10: handlePkgsCreate
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
}
}
}
示例11: handlePkgsDelete
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)
}
}