Пример #1
0
func handleRest(w http.ResponseWriter, db nosql.Database, remainder string, r *http.Request) {
	strs := strings.SplitN(remainder, "/", 2)

	method := r.Method

	exists, err := db.Exists(ctx)
	if err != nil {
		http.Error(w, "error in db.Exists()", 500)
		return
	}
	if !exists {
		http.Error(w, "database doesn't exist", 404)
		return
	}
	tableName := strs[0]

	t := db.Table(tableName)
	if len(strs) == 1 {
		handleTable(w, db, t, method)
		return
	}

	// TODO(bprosnitz) Is there a race between table Exists and Create?
	exists, err = t.Exists(ctx)
	if err != nil {
		http.Error(w, "error in table.Exists()", 500)
		return
	}
	if !exists {
		http.Error(w, "table doesn't exist", 404)
		return
	}

	key := strs[1]
	body, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "error reading message body", 500)
		return
	}

	fmt.Printf("DB: %s Table: %s Key: %s method: %s value: %q\n", db.Name(), tableName, key, method, body)
	row := t.Row(key)
	handleRow(w, row, key, string(body), method)
}