Пример #1
0
func update_full_index(ctx appengine.Context, w http.ResponseWriter) {
	// collect all packages into a single string (full index)
	query := datastore.NewQuery("PackageMeta").Order("Name")
	buf := bytes.NewBuffer(nil)
	for it := query.Run(ctx); ; {
		var meta gopm_index.PackageMeta
		_, err := it.Next(&meta)

		if err == datastore.Done {
			break
		}

		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		// serialize PackageMeta to json
		json, err := meta.ToJson()
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}

		// append to the buffer
		reader := bytes.NewReader(json)
		written, err := io.Copy(buf, reader)
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		if written != int64(len(json)) {
			http.Error(w, fmt.Sprintf("Indexing package '%v' failed.", meta.Name), http.StatusInternalServerError)
			return
		}

		// append a newline
		io.Copy(buf, bytes.NewBuffer([]byte("\n")))
	}

	// store full index to a special place
	index := &FullIndex{buf.Bytes()}
	key := datastore.NewKey(ctx, "FullIndex", "full_index", 0, nil)
	_, err := datastore.Put(ctx, key, index)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}