Commit 2cfffcf6 authored by santiaago's avatar santiaago
Browse files

support url /grid/square/themeId/string

Fixes #14
parent a93dfbcb
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ func main() {

	r.HandleFunc("/grid/?", grid.H6X6)
	r.HandleFunc("/grid/[0-8]/?", grid.Color)
	r.HandleFunc("/grid/square/[0-8]/[a-zA-Z0-9]+/?", grid.SquareColor)
	r.HandleFunc("/grid/square/[a-zA-Z0-9]+/?", grid.Square)

	r.HandleFunc("/grid/random/?", grid.Random)
+23 −0
Original line number Diff line number Diff line
@@ -31,3 +31,26 @@ func Square(w http.ResponseWriter, r *http.Request) {
		write.Image(w, &img)
	}
}

// gridColorHandler is the handler for /grid/square/[0-8]/[a-zA-Z0-9]+/?
// build a 6x6 grid with alternate colors based on the number passed in the url
func SquareColor(w http.ResponseWriter, r *http.Request) {

	if colorId, err := misc.PermalinkID(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		if id, err1 := misc.PermalinkString(r, 4); err1 == nil {
			m := image.NewRGBA(image.Rect(0, 0, 240, 240))
			colorMap := colors.MapOfColorPatterns()
			h := md5.New()
			io.WriteString(h, id)
			log.Printf("md5: %x", h.Sum(nil))
			key := fmt.Sprintf("%x", h.Sum(nil)[:])
			draw.Square(m, key, colorMap[int(colorId)][0], colorMap[int(colorId)][1])
			var img image.Image = m
			write.Image(w, &img)
		} else {
			log.Printf("error when extracting permalink string: %v", err)
		}
	}
}