Last active
August 29, 2015 14:25
-
-
Save soheilhy/9ab915cc08a0b2257089 to your computer and use it in GitHub Desktop.
EnQHTTPHandler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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