Commit 8981285d authored by santiaago's avatar santiaago
Browse files

check errors when calling route.Context.Get

Fixes #28
parent d1c69432
Loading
Loading
Loading
Loading
+33 −22
Original line number Diff line number Diff line
@@ -15,13 +15,25 @@ import (
	"github.com/taironas/tinygraphs/write"
)

func init() {
	log.SetFlags(log.Lshortfile)
}

// Color is the handler for /checkerboard/:colorId
// build a 6x6 checkerboard with alternate colors based on the number passed in the url
func Color(w http.ResponseWriter, r *http.Request) {
	id, _ := route.Context.Get(r, "colorId")
	if colorId, err := strconv.ParseInt(id, 0, 64); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {

	var err error
	var id string
	if id, err = route.Context.Get(r, "colorId"); err != nil {
		log.Println("Unable to get 'key' value: ", err)
		id = "0"
	}

	var colorId int64
	if colorId, err = strconv.ParseInt(id, 0, 64); err != nil {
		colorId = 0
	}
	size := extract.Size(r)
	colorMap := colors.MapOfColorPatterns()
	var c1, c2 color.RGBA
@@ -43,7 +55,6 @@ func Color(w http.ResponseWriter, r *http.Request) {
		draw.Grid6X6SVG(w, c1, c2, size)
	}
}
}

// Checkerboard is the handler for /checkerboard
// build a 6x6 checkerboard with alternate black and white colors.
+81 −50
Original line number Diff line number Diff line
@@ -3,34 +3,43 @@ package isogrids
import (
	"crypto/md5"
	"fmt"
	"strconv"

	"image/color"
	"io"
	"log"
	"net/http"
	"strconv"

	"github.com/taironas/route"
	tgColors "github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw"
	"github.com/taironas/tinygraphs/extract"

	"github.com/taironas/tinygraphs/write"
)

func init() {
	log.SetFlags(log.Lshortfile)
}

// Isogrids is the handler for /isogrids/:key
// builds a 10x10 grid with alternate colors based on the string passed in the url.
func Isogrids(w http.ResponseWriter, r *http.Request) {
	key, _ := route.Context.Get(r, "key")
	var err error
	var key string
	if key, err = route.Context.Get(r, "key"); err != nil {
		log.Println("Unable to get 'key' value: ", err)
		key = ""
	}

	h := md5.New()
	io.WriteString(h, key)
	key = fmt.Sprintf("%x", h.Sum(nil)[:])

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
		bg = colorMap[0][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {
	if fg, err = extract.Foreground(r); err != nil {
		fg = colorMap[0][1]
	}
	size := extract.Size(r)
@@ -41,27 +50,37 @@ func Isogrids(w http.ResponseWriter, r *http.Request) {
// Color is the handler for /isogrids/:colorId/:key
// builds a 10x10 grid with alternate colors based on the string passed in the url.
func Color(w http.ResponseWriter, r *http.Request) {
	id, _ := route.Context.Get(r, "colorId")
	var err error
	var id string
	if id, err = route.Context.Get(r, "colorId"); err != nil {
		log.Println("Unable to get 'colorId' value: ", err)
		id = "0"
	}

	colorId, err := strconv.ParseInt(id, 0, 64)
	if err != nil {
	var colorId int64
	if colorId, err = strconv.ParseInt(id, 0, 64); err != nil {
		colorId = 0
	}

	key, _ := route.Context.Get(r, "key")
	var key string
	if key, err = route.Context.Get(r, "key"); err != nil {
		log.Println("Unable to get 'key' value: ", err)
		key = ""
	}

	h := md5.New()
	io.WriteString(h, key)
	key = fmt.Sprintf("%x", h.Sum(nil)[:])

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
		bg = colorMap[int(colorId)][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {
	if fg, err = extract.Foreground(r); err != nil {
		fg = colorMap[int(colorId)][1]
	}

	size := extract.Size(r)
	write.ImageSVG(w)
	draw.Isogrids(w, key, bg, fg, size)
@@ -70,12 +89,12 @@ func Color(w http.ResponseWriter, r *http.Request) {
func Skeleton(w http.ResponseWriter, r *http.Request) {

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
	var err error
	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
		bg = colorMap[0][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {
	if fg, err = extract.Foreground(r); err != nil {
		fg = colorMap[0][1]
	}
	size := extract.Size(r)
@@ -86,12 +105,12 @@ func Skeleton(w http.ResponseWriter, r *http.Request) {
func Diagonals(w http.ResponseWriter, r *http.Request) {

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
	var err error
	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
		bg = colorMap[0][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {
	if fg, err = extract.Foreground(r); err != nil {
		fg = colorMap[0][1]
	}
	size := extract.Size(r)
@@ -102,12 +121,12 @@ func Diagonals(w http.ResponseWriter, r *http.Request) {
func HalfDiagonals(w http.ResponseWriter, r *http.Request) {

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
	var err error
	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
		bg = colorMap[0][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {
	if fg, err = extract.Foreground(r); err != nil {
		fg = colorMap[0][1]
	}
	size := extract.Size(r)
@@ -118,12 +137,12 @@ func HalfDiagonals(w http.ResponseWriter, r *http.Request) {
func GridBW(w http.ResponseWriter, r *http.Request) {

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
	var err error
	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
		bg = colorMap[0][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {
	if fg, err = extract.Foreground(r); err != nil {
		fg = colorMap[0][1]
	}
	size := extract.Size(r)
@@ -134,12 +153,12 @@ func GridBW(w http.ResponseWriter, r *http.Request) {
func Grid2Colors(w http.ResponseWriter, r *http.Request) {

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
	var err error
	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
		bg = colorMap[0][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {
	if fg, err = extract.Foreground(r); err != nil {
		fg = colorMap[0][1]
	}
	size := extract.Size(r)
@@ -150,12 +169,12 @@ func Grid2Colors(w http.ResponseWriter, r *http.Request) {
func Random(w http.ResponseWriter, r *http.Request) {

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
	var err error
	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
		bg = colorMap[0][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {
	if fg, err = extract.Foreground(r); err != nil {
		fg = colorMap[0][1]
	}
	size := extract.Size(r)
@@ -164,11 +183,18 @@ func Random(w http.ResponseWriter, r *http.Request) {
}

func RandomColor(w http.ResponseWriter, r *http.Request) {
	id, _ := route.Context.Get(r, "colorId")
	colorId, err := strconv.ParseInt(id, 0, 64)
	if err != nil {
	var err error
	var id string
	if id, err = route.Context.Get(r, "colorId"); err != nil {
		log.Println("Unable to get 'colorId' value: ", err)
		id = "0"
	}

	var colorId int64
	if colorId, err = strconv.ParseInt(id, 0, 64); err != nil {
		colorId = 0
	}

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
@@ -199,18 +225,23 @@ func RandomMirror(w http.ResponseWriter, r *http.Request) {
}

func RandomMirrorColor(w http.ResponseWriter, r *http.Request) {
	id, _ := route.Context.Get(r, "colorId")
	colorId, err := strconv.ParseInt(id, 0, 64)
	if err != nil {
	var err error
	var id string
	if id, err = route.Context.Get(r, "colorId"); err != nil {
		log.Println("Unable to get 'colorId' value: ", err)
		id = "0"
	}

	var colorId int64
	if colorId, err = strconv.ParseInt(id, 0, 64); err != nil {
		colorId = 0
	}
	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
		bg = colorMap[int(colorId)][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {
	if fg, err = extract.Foreground(r); err != nil {
		fg = colorMap[int(colorId)][1]
	}
	size := extract.Size(r)
+30 −22
Original line number Diff line number Diff line
@@ -42,10 +42,19 @@ func Random(w http.ResponseWriter, r *http.Request) {
// handler for "/squares/random/:colorId"
// generates a grid random image with a specific color based on the colorMap
func RandomColor(w http.ResponseWriter, r *http.Request) {
	id, _ := route.Context.Get(r, "colorId")
	if colorId, err := strconv.ParseInt(id, 0, 64); err != nil {
	var err error
	var id string
	if id, err = route.Context.Get(r, "colorId"); err != nil {
		log.Println("Unable to get 'colorId' value: ", err)
		id = "0"
	}

	var colorId int64
	if colorId, err = strconv.ParseInt(id, 0, 64); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		colorId = 0
	}

	size := extract.Size(r)
	colorMap := colors.MapOfColorPatterns()

@@ -68,4 +77,3 @@ func RandomColor(w http.ResponseWriter, r *http.Request) {
		draw.RandomGrid6X6SVG(w, c1, c2, size)
	}
}
}
+33 −9
Original line number Diff line number Diff line
@@ -4,7 +4,9 @@ import (
	"crypto/md5"
	"fmt"
	"image"
	"image/color"
	"io"
	"log"
	"net/http"
	"strconv"
	"strings"
@@ -17,10 +19,20 @@ import (
	"github.com/taironas/tinygraphs/write"
)

func init() {
	log.SetFlags(log.Lshortfile)
}

// Square is the handler for /squares/:key
// builds a 6x6 grid with alternate colors based on the number passed in the url.
func Square(w http.ResponseWriter, r *http.Request) {
	key, _ := route.Context.Get(r, "key")
	var err error
	var key string
	if key, err = route.Context.Get(r, "key"); err != nil {
		log.Println("Unable to get 'key' value: ", err)
		key = ""
	}

	h := md5.New()
	io.WriteString(h, key)
	key = fmt.Sprintf("%x", h.Sum(nil)[:])
@@ -37,14 +49,15 @@ func Square(w http.ResponseWriter, r *http.Request) {
	}

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
	var bg, fg color.RGBA
	if bg, err = extract.Background(r); err != nil {
		bg = colorMap[0][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {

	if fg, err = extract.Foreground(r); err != nil {
		fg = colorMap[0][1]
	}

	size := extract.Size(r)
	if f := extract.Format(r); f == format.JPEG {
		m := image.NewRGBA(image.Rect(0, 0, size, size))
@@ -61,13 +74,24 @@ func Square(w http.ResponseWriter, r *http.Request) {
// build a 6x6 grid with alternate colors based on the number passed in the url
func Color(w http.ResponseWriter, r *http.Request) {

	id, _ := route.Context.Get(r, "colorId")
	colorId, err := strconv.ParseInt(id, 0, 64)
	if err != nil {
	var err error
	var id string
	if id, err = route.Context.Get(r, "colorId"); err != nil {
		log.Println("Unable to get 'colorId' value: ", err)
		id = "0"
	}

	var colorId int64
	if colorId, err = strconv.ParseInt(id, 0, 64); err != nil {
		colorId = 0
	}

	key, _ := route.Context.Get(r, "key")
	var key string
	if key, err = route.Context.Get(r, "key"); err != nil {
		log.Println("Unable to get 'key' value: ", err)
		key = ""
	}

	h := md5.New()
	io.WriteString(h, key)
	key = fmt.Sprintf("%x", h.Sum(nil)[:])