Beispiel #1
0
func blobsHandler(blobrouter *router.Router) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		req := &router.Request{
			Type:      router.Read,
			Namespace: r.URL.Query().Get("ns"),
		}
		backend := blobrouter.Route(req)
		switch r.Method {
		case "GET":
			// FIXME(tsileo): Re-implement this!
			//start := r.URL.Query().Get("start")
			//end := r.URL.Query().Get("end")
			//slimit := r.URL.Query().Get("limit")
			//limit := 0
			//if slimit != "" {
			//	lim, err := strconv.Atoi(slimit)
			//	if err != nil {
			//		panic(err)
			//	}
			//	limit = lim
			//}
			blobs := make(chan string)
			errc := make(chan error, 1)
			go func() {
				errc <- backend.Enumerate(blobs)
			}()
			res := []string{}
			for blob := range blobs {
				res = append(res, blob)
			}
			if err := <-errc; err != nil {
				panic(err)
			}
			httputil.WriteJSON(w, map[string]interface{}{"blobs": res})
		default:
			w.WriteHeader(http.StatusMethodNotAllowed)
		}
	}
}
Beispiel #2
0
func blobHandler(blobrouter *router.Router) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		req := &router.Request{
			Type: router.Read,
			//	Namespace: r.URL.Query().Get("ns"),
		}
		backend := blobrouter.Route(req)
		switch r.Method {
		case "GET":
			blob, err := backend.Get(vars["hash"])
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
			}
			w.Write(blob)
			return
		case "HEAD":
			exists, err := backend.Exists(vars["hash"])
			if err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
			}
			if exists {
				return
			}
			http.Error(w, http.StatusText(404), 404)
			return
		case "DELETE":
			if err := backend.Delete(vars["hash"]); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
			}
			return
		default:
			w.WriteHeader(http.StatusMethodNotAllowed)
		}
	}
}
Beispiel #3
0
func vkvHandler(wg sync.WaitGroup, db *vkv.DB, kvUpdate chan *vkv.KeyValue, blobrouter *router.Router) func(http.ResponseWriter, *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		switch r.Method {
		case "GET":
			iversion := -1
			version := r.URL.Query().Get("version")
			if version != "" {
				iver, err := strconv.Atoi(version)
				if err != nil {
					panic(err)
				}
				iversion = iver
			}
			res, err := db.Get(vars["key"], iversion)
			if err != nil {
				if err == vkv.ErrNotFound {
					http.Error(w, http.StatusText(404), 404)
					return
				}
				panic(err)
			}
			httputil.WriteJSON(w, res)
		case "HEAD":
			exists, err := db.Check(vars["key"])
			if err != nil {
				panic(err)
			}
			if exists {
				return
			}
			http.Error(w, http.StatusText(404), 404)
			return
		case "DELETE":
			k := vars["key"]
			sversion := r.URL.Query().Get("version")
			if sversion == "" {
				http.Error(w, "version missing", 500)
				return
			}
			version, err := strconv.Atoi(sversion)
			if err != nil {
				http.Error(w, "bad version", 500)
				return
			}
			hash, err := db.MetaBlob(k, version)
			if err != nil {
				panic(err)
			}
			if err := db.DeleteVersion(k, version); err != nil {
				panic(err)
			}
			req := &router.Request{
				Type:     router.Read,
				MetaBlob: true,
			}
			if err := blobrouter.Route(req).Delete(hash); err != nil {
				http.Error(w, err.Error(), http.StatusInternalServerError)
			}
		case "PUT":
			wg.Add(1)
			defer wg.Done()
			k := vars["key"]
			hah, err := ioutil.ReadAll(r.Body)
			values, err := url.ParseQuery(string(hah))
			if err != nil {
				panic(err)
			}
			v := values.Get("value")
			sversion := values.Get("version")
			version := -1
			if sversion != "" {
				iversion, err := strconv.Atoi(sversion)
				if err != nil {
					http.Error(w, "bad version", 500)
					return
				}
				version = iversion
			}
			res, err := db.Put(k, v, version)
			// FIXME(tsileo): handle namespace in embeded client too
			res.SetNamespace(values.Get("ns"))
			if err != nil {
				panic(err)
			}
			kvUpdate <- res
			httputil.WriteJSON(w, res)
		default:
			w.WriteHeader(http.StatusMethodNotAllowed)
		}
	}
}