Skip to content

Instantly share code, notes, and snippets.

@soheilhy
Last active August 29, 2015 14:25
Show Gist options
  • Save soheilhy/9ab915cc08a0b2257089 to your computer and use it in GitHub Desktop.
Save soheilhy/9ab915cc08a0b2257089 to your computer and use it in GitHub Desktop.
EnQHTTPHandler
type httpHandler struct {
Hive beehive.Hive // Hive represents the hive our handler is registered on.
}
// EnQHTTPHandler provides the HTTP endpoint for enqueuing tasks.
type EnQHTTPHandler httpHandler
func (h *EnQHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
q, ok := mux.Vars(r)["queue"]
if !ok {
http.Error(w, "unkown queue", http.StatusBadRequest)
return
}
b, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "cannot read request body", http.StatusBadRequest)
return
}
e := Enque{
Task: Task{
Queue: Queue(q),
Body: b,
},
}
ctx, cnl := context.WithTimeout(context.Background(), httpTimeout)
defer cnl()
if _, err := h.Hive.Sync(ctx, e); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
fmt.Fprintf(w, "task enqueued\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment