Last active
December 17, 2015 16:30
-
-
Save jmhodges/5639674 to your computer and use it in GitHub Desktop.
demo hour code. written quickly.
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 ( | |
"io" | |
"log" | |
"math/rand" | |
"net/http" | |
"os" | |
) | |
var puppyFiles = []string{ | |
"serene.jpg", | |
"rabid_dog.gif", | |
"cool.gif", | |
"glasses.jpeg", | |
} | |
func main() { | |
http.Handle("/puppies", http.HandlerFunc(randomPuppy)) | |
http.Handle("/puppies/", http.HandlerFunc(randomPuppy)) | |
log.Fatal(http.ListenAndServe(":24004", nil)) | |
} | |
func randomPuppy(w http.ResponseWriter, r *http.Request) { | |
n := rand.Intn(len(puppyFiles)) | |
f, err := os.Open("./dog_images/" + puppyFiles[n]) | |
if err != nil { | |
w.WriteHeader(500) | |
w.Write([]byte("sorry, we broke")) | |
return | |
} | |
w.WriteHeader(200) | |
io.Copy(w, f) | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment