Last active
December 6, 2018 17:46
-
-
Save nesv/9219811 to your computer and use it in GitHub Desktop.
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
package main | |
import ( | |
"fmt" | |
"net/http" | |
"time" | |
) | |
// A buffered channel that we can send work requests on. | |
var WorkQueue = make(chan WorkRequest, 100) | |
func Collector(w http.ResponseWriter, r *http.Request) { | |
// Make sure we can only be called with an HTTP POST request. | |
if r.Method != "POST" { | |
w.Header().Set("Allow", "POST") | |
w.WriteHeader(http.StatusMethodNotAllowed) | |
return | |
} | |
// Parse the delay. | |
delay, err := time.ParseDuration(r.FormValue("delay")) | |
if err != nil { | |
http.Error(w, "Bad delay value: "+err.Error(), http.StatusBadRequest) | |
return | |
} | |
// Check to make sure the delay is anywhere from 1 to 10 seconds. | |
if delay.Seconds() < 1 || delay.Seconds() > 10 { | |
http.Error(w, "The delay must be between 1 and 10 seconds, inclusively.", http.StatusBadRequest) | |
return | |
} | |
// Now, we retrieve the person's name from the request. | |
name := r.FormValue("name") | |
// Just do a quick bit of sanity checking to make sure the client actually provided us with a name. | |
if name == "" { | |
http.Error(w, "You must specify a name.", http.StatusBadRequest) | |
return | |
} | |
// Now, we take the delay, and the person's name, and make a WorkRequest out of them. | |
work := WorkRequest{Name: name, Delay: delay} | |
// Push the work onto the queue. | |
WorkQueue <- work | |
fmt.Println("Work request queued") | |
// And let the user know their work request was created. | |
w.WriteHeader(http.StatusCreated) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment