From 10b585d824e91b1720f9e79af665c29a6f67e88a Mon Sep 17 00:00:00 2001 From: Harsimran Singh Maan Date: Mon, 9 Oct 2017 18:23:37 -0700 Subject: [PATCH] Create cache directory if it doesn't exist --- demo/webexample.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/demo/webexample.go b/demo/webexample.go index 9aaefa2..b8660c7 100755 --- a/demo/webexample.go +++ b/demo/webexample.go @@ -3,7 +3,6 @@ package main import ( - bbmandelbrot "simonwaldherr.de/go/bbmandelbrotGo" "encoding/base64" "fmt" "image/png" @@ -13,10 +12,15 @@ import ( "net/url" "os" "runtime" + bbmandelbrot "simonwaldherr.de/go/bbmandelbrotGo" "strconv" "strings" ) +const ( + cachePath = "cache" +) + func dataURI(fileName, contentType string) string { data, err := ioutil.ReadFile(fileName) if err != nil { @@ -66,7 +70,7 @@ func handler(w http.ResponseWriter, r *http.Request) { for y = 0; y < 4; y++ { for x = 0; x < 4; x++ { - fname = fmt.Sprintf("cache/%vx%v_%v_%v_%v_%v_mandelbrot.png", width, height, cx1+160*x, cx1+160*(x+1), cy1+160*y, cy1+160*(y+1)) + fname = fmt.Sprintf("%s/%vx%v_%v_%v_%v_%v_mandelbrot.png", cachePath, width, height, cx1+160*x, cx1+160*(x+1), cy1+160*y, cy1+160*(y+1)) if _, err := os.Stat(fname); err != nil { fmt.Println("generating ", fname) img, _ := bbmandelbrot.Mandelbrot(width, height, cx1+160*x, cx1+160*(x+1), cy1+160*y, cy1+160*(y+1), csr, csg, csb) @@ -95,6 +99,10 @@ func handler(w http.ResponseWriter, r *http.Request) { } func main() { + if _, err := os.Stat(cachePath); os.IsNotExist(err) { + os.Mkdir(cachePath, os.ModePerm) + } + var port string = ":8080" runtime.GOMAXPROCS(runtime.NumCPU()) http.HandleFunc("/", handler)