Commit 901f5bb7 authored by santiaago's avatar santiaago
Browse files

change tinygraph routes to new impl or taironas/route :-)

parent 2b50c897
Loading
Loading
Loading
Loading
+17 −19
Original line number Diff line number Diff line
@@ -17,28 +17,26 @@ var root = flag.String("root", "app", "file system path")
func main() {
	r := new(route.Router)

	r.HandleFunc("/checkerboard/?", checkerboard.Checkerboard)
	r.HandleFunc("/checkerboard/[0-8]/?", checkerboard.Color)
	r.HandleFunc("/checkerboard", checkerboard.Checkerboard)
	r.HandleFunc("/checkerboard/:colorId", checkerboard.Color)

	r.HandleFunc("/squares/?", squares.Random)
	r.HandleFunc("/squares/random/?", squares.Random)
	r.HandleFunc("/squares/random/[0-8]/?", squares.RandomColor)
	r.HandleFunc("/squares/[a-zA-Z0-9\\.]+/?", squares.Square)      //cached
	r.HandleFunc("/squares/[0-8]/[a-zA-Z0-9\\.]+/?", squares.Color) // cached

	r.HandleFunc("/isogrids/skeleton/?", isogrids.Skeleton)
	r.HandleFunc("/isogrids/[a-zA-Z0-9]+/?", isogrids.Isogrids)
	r.HandleFunc("/isogrids/[0-8]/[a-zA-Z0-9]+/?", isogrids.Color)
	r.HandleFunc("/isogrids/diagonals/[a-zA-Z0-9]+/?", isogrids.Diagonals)
	r.HandleFunc("/isogrids/halfdiagonals/[a-zA-Z0-9]+/?", isogrids.HalfDiagonals)
	r.HandleFunc("/isogrids/gridbw/[a-zA-Z0-9]+/?", isogrids.GridBW)
	r.HandleFunc("/isogrids/grid2colors/[a-zA-Z0-9]+/?", isogrids.Grid2Colors)
	r.HandleFunc("/isogrids/random/[0-8]+/?", isogrids.RandomColor)
	r.HandleFunc("/isogrids/random/[a-zA-Z0-9]+/?", isogrids.Random)
	r.HandleFunc("/isogrids/random-mirror/[0-8]+/?", isogrids.RandomMirrorColor)
	r.HandleFunc("/isogrids/random-mirror/[a-zA-Z0-9]+/?", isogrids.RandomMirror)

	r.HandleFunc("/isogrids/[a-zA-Z0-9]+/?", isogrids.Isogrids)
	r.HandleFunc("/squares/random/:colorId", squares.RandomColor)
	r.HandleFunc("/squares/:key", squares.Square)         //cached
	r.HandleFunc("/squares/:colorId/:key", squares.Color) // cached

	r.HandleFunc("/isogrids/skeleton", isogrids.Skeleton)
	r.HandleFunc("/isogrids/:key", isogrids.Isogrids)
	r.HandleFunc("/isogrids/:colorId/:key", isogrids.Color)
	r.HandleFunc("/isogrids/diagonals", isogrids.Diagonals)
	r.HandleFunc("/isogrids/halfdiagonals", isogrids.HalfDiagonals)
	r.HandleFunc("/isogrids/gridbw", isogrids.GridBW)
	r.HandleFunc("/isogrids/grid2colors", isogrids.Grid2Colors)
	r.HandleFunc("/isogrids/random/:colorId", isogrids.RandomColor)
	r.HandleFunc("/isogrids/random", isogrids.Random)
	r.HandleFunc("/isogrids/random-mirror/:colorId", isogrids.RandomMirrorColor)
	r.HandleFunc("/isogrids/random-mirror", isogrids.RandomMirror)

	r.AddStaticResource(root)

+22 −11
Original line number Diff line number Diff line
package checkerboard

import (
	"image"
	"image/color"
	"log"
	"net/http"
	"strconv"

	"github.com/taironas/route"
	"github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/format"
	"github.com/taironas/tinygraphs/misc"
	"github.com/taironas/tinygraphs/write"
	"image"
	"image/color"
	"log"
	"net/http"
)

// Color is the handler for /checkerboard/[0-8]
// 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) {
	intID, err := misc.PermalinkID(r, 2)
	if err != nil {
	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 {
		size := extract.Size(r)
		colorMap := colors.MapOfColorPatterns()
		var c1, c2 color.RGBA
		if val, ok := colorMap[int(colorId)]; ok {
			c1 = val[0]
			c2 = val[1]
		} else {
			c1 = colorMap[0][0]
			c2 = colorMap[0][1]
		}

		if f := extract.Format(r); f == format.JPEG {
			m := image.NewRGBA(image.Rect(0, 0, size, size))
			draw.Grid6X6(m, colorMap[int(intID)][0], colorMap[int(intID)][1])
			draw.Grid6X6(m, c1, c2)
			var img image.Image = m
			write.ImageJPEG(w, &img)
		} else if f == format.SVG {
			write.ImageSVG(w)
			draw.Grid6X6SVG(w, colorMap[int(intID)][0], colorMap[int(intID)][1], size)
			draw.Grid6X6SVG(w, c1, c2, size)
		}
	}
}

// Checkerboard is the handler for /checkerboard/
// Checkerboard is the handler for /checkerboard
// build a 6x6 checkerboard with alternate black and white colors.
func Checkerboard(w http.ResponseWriter, r *http.Request) {
	size := extract.Size(r)
+139 −189
Original line number Diff line number Diff line
@@ -3,29 +3,26 @@ package isogrids
import (
	"crypto/md5"
	"fmt"
	"strconv"

	"io"
	"log"
	"net/http"

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

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

// Isogrids is the handler for /isogrids/[a-zA-Z0-9]+/?.
// 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) {

	if id, err := misc.PermalinkString(r, 2); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
	key := route.Context.Get(r, "key")
	h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])
	io.WriteString(h, key)
	key = fmt.Sprintf("%x", h.Sum(nil)[:])

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
@@ -40,18 +37,21 @@ func Isogrids(w http.ResponseWriter, r *http.Request) {
	write.ImageSVG(w)
	draw.Isogrids(w, key, bg, fg, size)
}
}

// Isogrids is the handler for /isogrids/[a-zA-Z0-9]+/?.
// 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) {
	if colorId, err := misc.PermalinkID(r, 2); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		if id, err1 := misc.PermalinkString(r, 3); err1 == nil {
	id := route.Context.Get(r, "colorId")

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

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

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
@@ -65,10 +65,6 @@ func Color(w http.ResponseWriter, r *http.Request) {
	size := extract.Size(r)
	write.ImageSVG(w)
	draw.Isogrids(w, key, bg, fg, size)
		} else {
			log.Printf("error when extracting permalink string: %v", err)
		}
	}
}

func Skeleton(w http.ResponseWriter, r *http.Request) {
@@ -89,13 +85,6 @@ func Skeleton(w http.ResponseWriter, r *http.Request) {

func Diagonals(w http.ResponseWriter, r *http.Request) {

	if id, err := misc.PermalinkString(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
@@ -107,19 +96,11 @@ func Diagonals(w http.ResponseWriter, r *http.Request) {
	}
	size := extract.Size(r)
	write.ImageSVG(w)
		draw.Diagonals(w, key, bg, fg, size)
	}
	draw.Diagonals(w, "", bg, fg, size)
}

func HalfDiagonals(w http.ResponseWriter, r *http.Request) {

	if id, err := misc.PermalinkString(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
@@ -131,19 +112,11 @@ func HalfDiagonals(w http.ResponseWriter, r *http.Request) {
	}
	size := extract.Size(r)
	write.ImageSVG(w)
		draw.HalfDiagonals(w, key, bg, fg, size)
	}
	draw.HalfDiagonals(w, "", bg, fg, size)
}

func GridBW(w http.ResponseWriter, r *http.Request) {

	if id, err := misc.PermalinkString(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
@@ -155,19 +128,11 @@ func GridBW(w http.ResponseWriter, r *http.Request) {
	}
	size := extract.Size(r)
	write.ImageSVG(w)
		draw.IsogridsBW(w, key, bg, fg, size)
	}
	draw.IsogridsBW(w, "", bg, fg, size)
}

func Grid2Colors(w http.ResponseWriter, r *http.Request) {

	if id, err := misc.PermalinkString(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
@@ -179,19 +144,11 @@ func Grid2Colors(w http.ResponseWriter, r *http.Request) {
	}
	size := extract.Size(r)
	write.ImageSVG(w)
		draw.Isogrids2Colors(w, key, bg, fg, size)
	}
	draw.Isogrids2Colors(w, "", bg, fg, size)
}

func Random(w http.ResponseWriter, r *http.Request) {

	if id, err := misc.PermalinkString(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
@@ -203,37 +160,30 @@ func Random(w http.ResponseWriter, r *http.Request) {
	}
	size := extract.Size(r)
	write.ImageSVG(w)
		draw.IsogridsRandom(w, key, bg, fg, size)
	}
	draw.IsogridsRandom(w, "", bg, fg, size)
}

func RandomColor(w http.ResponseWriter, r *http.Request) {
	if id, err := misc.PermalinkID(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
	id := route.Context.Get(r, "colorId")
	colorId, err := strconv.ParseInt(id, 0, 64)
	if err != nil {
		colorId = 0
	}
	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
			bg = colorMap[int(id)][0]
		bg = colorMap[int(colorId)][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {
			fg = colorMap[int(id)][1]
		fg = colorMap[int(colorId)][1]
	}
	size := extract.Size(r)
	write.ImageSVG(w)
	draw.IsogridsRandom(w, "", bg, fg, size)
}
}

func RandomMirror(w http.ResponseWriter, r *http.Request) {
	if id, err := misc.PermalinkString(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
@@ -245,25 +195,25 @@ func RandomMirror(w http.ResponseWriter, r *http.Request) {
	}
	size := extract.Size(r)
	write.ImageSVG(w)
		draw.IsogridsRandomMirror(w, key, bg, fg, size)
	}
	draw.IsogridsRandomMirror(w, "", bg, fg, size)
}

func RandomMirrorColor(w http.ResponseWriter, r *http.Request) {
	if id, err := misc.PermalinkID(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
	id := route.Context.Get(r, "colorId")
	colorId, err := strconv.ParseInt(id, 0, 64)
	if err != nil {
		colorId = 0
	}
	colorMap := tgColors.MapOfColorPatterns()
	bg, err1 := extract.Background(r)
	if err1 != nil {
			bg = colorMap[int(id)][0]
		bg = colorMap[int(colorId)][0]
	}
	fg, err2 := extract.Foreground(r)
	if err2 != nil {
			fg = colorMap[int(id)][1]
		fg = colorMap[int(colorId)][1]
	}
	size := extract.Size(r)
	write.ImageSVG(w)
	draw.IsogridsRandomMirror(w, "", bg, fg, size)
}
}
+22 −10
Original line number Diff line number Diff line
package squares

import (
	"image"
	"image/color"
	"log"
	"net/http"
	"strconv"

	"github.com/taironas/route"
	"github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/format"
	"github.com/taironas/tinygraphs/misc"
	"github.com/taironas/tinygraphs/write"
	"image"
	"log"
	"net/http"
)

// handler for "/squares/random"
@@ -36,24 +39,33 @@ func Random(w http.ResponseWriter, r *http.Request) {
	}
}

// handler for "/squares/random/[0-9]"
// 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) {
	intID, err := misc.PermalinkID(r, 3)
	if err != nil {
	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 {
		size := extract.Size(r)
		colorMap := colors.MapOfColorPatterns()

		var c1, c2 color.RGBA
		if val, ok := colorMap[int(colorId)]; ok {
			c1 = val[0]
			c2 = val[1]
		} else {
			c1 = colorMap[0][0]
			c2 = colorMap[0][1]
		}

		if f := extract.Format(r); f == format.JPEG {
			m := image.NewRGBA(image.Rect(0, 0, size, size))
			draw.RandomGrid6X6(m, colorMap[int(intID)][0], colorMap[int(intID)][1])
			draw.RandomGrid6X6(m, c1, c2)
			var img image.Image = m
			write.ImageJPEG(w, &img)
		} else if f == format.SVG {
			write.ImageSVG(w)
			draw.RandomGrid6X6SVG(w, colorMap[int(intID)][0], colorMap[int(intID)][1], size)

			draw.RandomGrid6X6SVG(w, c1, c2, size)
		}
	}
}
+68 −75
Original line number Diff line number Diff line
@@ -3,31 +3,27 @@ package squares
import (
	"crypto/md5"
	"fmt"
	tgColors "github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/format"
	"github.com/taironas/tinygraphs/misc"
	"github.com/taironas/tinygraphs/write"
	"image"
	"io"
	"log"
	"net/http"
	"strconv"
	"strings"

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

// Square is the handler for /squares/[A-Za-z0-9]+/?.
// 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) {

	if id, err := misc.PermalinkString(r, 2); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {

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

	e := `"` + key + `"`
	w.Header().Set("Etag", e)
@@ -60,20 +56,21 @@ func Square(w http.ResponseWriter, r *http.Request) {
		draw.SquaresSVG(w, key, bg, fg, size)
	}
}
}

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

	if colorId, err := misc.PermalinkID(r, 2); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		if id, err1 := misc.PermalinkString(r, 3); err1 == nil {
	id := route.Context.Get(r, "colorId")
	colorId, err := strconv.ParseInt(id, 0, 64)
	if err != nil {
		colorId = 0
	}

	key := route.Context.Get(r, "key")
	h := md5.New()
			io.WriteString(h, id)
			key := fmt.Sprintf("%x", h.Sum(nil)[:])
	io.WriteString(h, key)
	key = fmt.Sprintf("%x", h.Sum(nil)[:])
	strId := strconv.FormatInt(colorId, 10)

	e := `"` + key + `-` + strId + `"`
@@ -98,8 +95,4 @@ func Color(w http.ResponseWriter, r *http.Request) {
		write.ImageSVG(w)
		draw.SquaresSVG(w, key, colorMap[int(colorId)][0], colorMap[int(colorId)][1], size)
	}
		} else {
			log.Printf("error when extracting permalink string: %v", err)
		}
	}
}