Commit b4f98965 authored by santiaago's avatar santiaago
Browse files

new organization with better packages

parent 6e398d7c
Loading
Loading
Loading
Loading
+17 −14
Original line number Diff line number Diff line
@@ -3,6 +3,9 @@ package main
import (
	"flag"
	"github.com/taironas/route"
	"github.com/taironas/tinygraphs/controllers/colors"
	"github.com/taironas/tinygraphs/controllers/gradient"
	"github.com/taironas/tinygraphs/controllers/grid"
	"log"
	"net/http"
	"os"
@@ -13,24 +16,24 @@ var root = flag.String("root", "app", "file system path")
func main() {
	r := new(route.Router)

	r.HandleFunc("/black/?", blackHandler)
	r.HandleFunc("/green/?", greenHandler)
	r.HandleFunc("/blue/?", blueHandler)
	r.HandleFunc("/red/?", redHandler)
	r.HandleFunc("/black/?", colors.Black)
	r.HandleFunc("/green/?", colors.Green)
	r.HandleFunc("/blue/?", colors.Blue)
	r.HandleFunc("/red/?", colors.Red)

	r.HandleFunc("/grid/?", grid6X6Handler)
	r.HandleFunc("/grid/[0-8]?", gridColorHandler)
	r.HandleFunc("/grid/?", grid.H6X6)
	r.HandleFunc("/grid/[0-8]?", grid.Color)

	r.HandleFunc("/grid/random/?", gridRandomHandler)
	r.HandleFunc("/grid/random/[0-8]/?", gridRandomColorHandler)
	r.HandleFunc("/grid/random/?", grid.Random)
	r.HandleFunc("/grid/random/[0-8]/?", grid.RandomColor)

	r.HandleFunc("/grid/random/symetric/?", gridRandomSymetricXHandler)
	r.HandleFunc("/grid/random/symetric/x/?", gridRandomSymetricXHandler)
	r.HandleFunc("/grid/random/symetric/x/[0-8]?", gridRandomSymetricXColorHandler)
	r.HandleFunc("/grid/random/symetric/y/?", gridRandomSymetricYHandler)
	r.HandleFunc("/grid/random/symetric/y/[0-8]?", gridRandomSymetricYColorHandler)
	r.HandleFunc("/grid/random/symetric/?", grid.RandomSymetricX)
	r.HandleFunc("/grid/random/symetric/x/?", grid.RandomSymetricX)
	r.HandleFunc("/grid/random/symetric/x/[0-8]?", grid.RandomSymetricXColor)
	r.HandleFunc("/grid/random/symetric/y/?", grid.RandomSymetricY)
	r.HandleFunc("/grid/random/symetric/y/[0-8]?", grid.RandomSymetricYColor)

	r.HandleFunc("/gradient/?", gradientHandler)
	r.HandleFunc("/gradient/?", gradient.Gradient)

	r.AddStaticResource(root)

+1 −1
Original line number Diff line number Diff line
package main
package colors

import (
	"image/color"
+10 −10
Original line number Diff line number Diff line
package main
package colors

import (
	"github.com/taironas/tinygraphs/write"
	"image"
	"image/color"
	"image/draw"
	"net/http"
)

func blackHandler(w http.ResponseWriter, r *http.Request) {
func Black(w http.ResponseWriter, r *http.Request) {
	m := image.NewRGBA(image.Rect(0, 0, 240, 240))
	black := color.RGBA{0, 0, 0, 255}
	draw.Draw(m, m.Bounds(), &image.Uniform{black}, image.ZP, draw.Src)

	var img image.Image = m
	writeImage(w, &img)
	write.Image(w, &img)
}

func greenHandler(w http.ResponseWriter, r *http.Request) {
	// prepare
func Green(w http.ResponseWriter, r *http.Request) {
	m := image.NewRGBA(image.Rect(0, 0, 240, 240))
	green := color.RGBA{0, 128, 0, 255}
	draw.Draw(m, m.Bounds(), &image.Uniform{green}, image.ZP, draw.Src)

	var img image.Image = m
	writeImage(w, &img)
	write.Image(w, &img)
}

func blueHandler(w http.ResponseWriter, r *http.Request) {
func Blue(w http.ResponseWriter, r *http.Request) {
	m := image.NewRGBA(image.Rect(0, 0, 240, 240))
	blue := color.RGBA{0, 0, 255, 255}
	draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)

	var img image.Image = m
	writeImage(w, &img)
	write.Image(w, &img)
}

func redHandler(w http.ResponseWriter, r *http.Request) {
func Red(w http.ResponseWriter, r *http.Request) {
	m := image.NewRGBA(image.Rect(0, 0, 240, 240))
	blue := color.RGBA{255, 0, 0, 255}
	draw.Draw(m, m.Bounds(), &image.Uniform{blue}, image.ZP, draw.Src)

	var img image.Image = m
	writeImageWithTemplate(w, &img)
	write.ImageWithTemplate(w, &img)
}
+17 −0
Original line number Diff line number Diff line
package gradient

import (
	"github.com/taironas/tinygraphs/draw"
	"github.com/taironas/tinygraphs/write"

	"image"
	"net/http"
)

// Gradient is the handler for /gradient/
func Gradient(w http.ResponseWriter, r *http.Request) {
	m := image.NewRGBA(image.Rect(0, 0, 240, 240))
	draw.Gradient(m)
	var img image.Image = m
	write.Image(w, &img)
}
+13 −17
Original line number Diff line number Diff line
package main
package grid

import (
	"github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw"
	"github.com/taironas/tinygraphs/misc"
	"github.com/taironas/tinygraphs/write"
	"image"
	"image/color"
	"log"
@@ -9,34 +13,26 @@ import (

// gridColorHandler is the handler for /grid/[0-8]
// build a 6x6 grid with alternate colors based on the number passed in the url
func gridColorHandler(w http.ResponseWriter, r *http.Request) {
	intID, err := PermalinkID(r, 2)
func Color(w http.ResponseWriter, r *http.Request) {
	intID, err := misc.PermalinkID(r, 2)
	if err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		m := image.NewRGBA(image.Rect(0, 0, 240, 240))
		colorMap := MapOfColorPatterns()
		drawGrid6X6(m, colorMap[int(intID)][0], colorMap[int(intID)][1])
		colorMap := colors.MapOfColorPatterns()
		draw.Grid6X6(m, colorMap[int(intID)][0], colorMap[int(intID)][1])
		var img image.Image = m
		writeImage(w, &img)
		write.Image(w, &img)
	}
}

// grid6X6Handler is the handler for /grid/
// build a 6x6 grid with alternate black and white colors.
func grid6X6Handler(w http.ResponseWriter, r *http.Request) {
func H6X6(w http.ResponseWriter, r *http.Request) {
	m := image.NewRGBA(image.Rect(0, 0, 240, 240))
	color1 := color.RGBA{uint8(255), uint8(255), 255, 255}
	color2 := color.RGBA{uint8(0), uint8(0), 0, 255}
	drawGrid6X6(m, color1, color2)
	draw.Grid6X6(m, color1, color2)
	var img image.Image = m
	writeImage(w, &img)
}

// gradientHandler is the handler for /gradient/
func gradientHandler(w http.ResponseWriter, r *http.Request) {
	m := image.NewRGBA(image.Rect(0, 0, 240, 240))
	drawGradient(m)
	var img image.Image = m
	writeImage(w, &img)
	write.Image(w, &img)
}
Loading