예제 #1
0
func SetKey(d db.DB) http.Handler {

	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		values := r.URL.Query()

		var key, val string

		for k := range values {
			key = k
		}

		val = values[key][0]

		if key == "" {
			http.Error(w, "missing key name", http.StatusBadRequest)
			return
		}

		if err := d.Set(key, val); err != nil {
			http.Error(w, "error setting values in db", http.StatusInternalServerError)
			return
		}

		//fmt.Printf("%v\n", val)
		w.WriteHeader(http.StatusOK)
		w.Write([]byte("saved"))

	})
}
예제 #2
0
func GetKey(d db.DB) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

		key := r.URL.Query().Get("key")

		if key == "" {
			http.Error(w, "missing key name", http.StatusBadRequest)
			return
		}

		val, err := d.Get(key)
		if err == db.ErrNotFound {
			http.Error(w, "not found", http.StatusNotFound)
			return
		} else if err != nil {
			http.Error(w, fmt.Sprintf("error getting from database %s", err), http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
		w.Write([]byte(val))

	})
}