Skip to content

Instantly share code, notes, and snippets.

@soheilhy soheilhy/queue.go
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.