Skip to content

Instantly share code, notes, and snippets.

@jmhodges
Last active December 17, 2015 16:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmhodges/5639674 to your computer and use it in GitHub Desktop.
Save jmhodges/5639674 to your computer and use it in GitHub Desktop.
demo hour code. written quickly.
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