Source

simplefractal / fractal / fractal.go

package fractal

import (
	"http"
	"time"
	"log"
	"fmt"
)

func drawSVG(w http.ResponseWriter, req *http.Request) {
	tree, err := ParseTree(req.URL.Query())
	if err != nil {
		http.Error(w, err.String(), http.StatusBadRequest)
	}

	start := time.Nanoseconds()
	w.Header().Add("Content-Type", "image/svg+xml")
	tree.WriteSVG(w)
	end := time.Nanoseconds()

	log.Printf("%v in %fs", req.URL, float64(end-start)*1e-9)
}

func indexHTML(w http.ResponseWriter, req *http.Request) {
	fmt.Fprintln(w, "<html>")
	fmt.Fprintln(w, "<head>")
	fmt.Fprintln(w, "<title>Fractal</title>")
	fmt.Fprintln(w, "</head>")
	fmt.Fprintln(w, "<body>")
	fmt.Fprintln(w, "<img src=\"/draw.svg?r=0,0,0,128&e=0,0,0,128&s=-48,128,48,128&s=-32,0,32,0&s=-40,64,40,64\"/>")
	fmt.Fprintln(w, "<img src=\"/draw.svg?r=0,0,128,128&e=0,0,128,128&s=0,0,128,0&s=128,0,128,128&s=64,64,128,0&se=128,0,64,64\"/>")
	fmt.Fprintln(w, "<img src=\"/draw.svg?r=0,0,128,128&e=0,0,128,128&s=0,0,128,0&s=0,128,128,128\"/>")
	fmt.Fprintln(w, "<img src=\"/draw.svg?r=0,0,0,128&e=0,0,0,128&s=-48,128,48,128&s=-48,0,48,0\"/>")
	fmt.Fprintln(w, "<img src=\"/draw.svg?r=0,0,0,128&e=0,0,0,128&s=-16,48,64,0&s=32,128,-64,128\"/>")
	fmt.Fprintln(w, "<img src=\"/draw.svg?r=0,0,0,128&e=0,0,0,128&s=-48,176,+48,160&s=-48,-48,+48,-32\"/>")
	fmt.Fprintln(w, "<img src=\"/draw.svg?r=0,0,0,128&e=0,0,0,128&s=-48,160,+48,160&s=-48,-32,+48,-32\"/>")

	fmt.Fprintln(w, "</body>")
	fmt.Fprintln(w, "</html>")
}

func init() {
	http.HandleFunc("/draw.svg", drawSVG)
	http.HandleFunc("/index.html", indexHTML)
}