Exemple #1
0
func pop(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
	name := queueName(ctx)
	q := queue.FromContext(ctx)
	var wait time.Duration
	if s := r.URL.Query().Get("wait"); s != "" {
		var err error
		wait, err = time.ParseDuration(s)
		if err != nil {
			return err
		}
	}
	var cancel chan struct{}
	if cn, ok := w.(http.CloseNotifier); ok {
		cancel = make(chan struct{})
		go func() {
			<-cn.CloseNotify()
			close(cancel)
		}()
	}
	envelope, err := q.Dequeue(name, wait, cancel)
	if err != nil {
		return err
	}
	return writeHTTP(w, envelope)
}
Exemple #2
0
func push(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
	name := queueName(ctx)
	q := queue.FromContext(ctx)
	props, err := newProperties(r)
	if err != nil {
		return err
	}
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	msg := &storage.Message{
		ContentType: r.Header.Get("Content-Type"),
		Body:        b,
	}
	meta, err := q.Enqueue(name, msg, props)
	if err != nil {
		return err
	}
	results := make(map[string]*pushResult)
	for k, v := range meta {
		results[k] = newPushResult(v)
	}
	return json.NewEncoder(w).Encode(results)
}
Exemple #3
0
func getProperties(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
	name := queueName(ctx)
	q := queue.FromContext(ctx)
	inherit := asBool(r.URL.Query().Get("inherit"))
	if props := q.Properties(name, inherit); props != nil {
		return json.NewEncoder(w).Encode(props)
	}
	w.WriteHeader(http.StatusNoContent)
	return nil
}
Exemple #4
0
func reply(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
	eid, err := uid.FromHashID(param.FromContext(ctx, "id"))
	if err != nil {
		return err
	}
	q := queue.FromContext(ctx)
	if err := q.Ack(eid); err != nil {
		return err
	}
	fmt.Fprintf(w, "ok")
	return nil
}
Exemple #5
0
func setProperties(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
	name := queueName(ctx)
	q := queue.FromContext(ctx)
	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return err
	}
	props := queue.NewProperties()
	if err := json.Unmarshal(b, props); err != nil {
		return err
	}
	q.SetProperties(name, props)
	return nil
}