Skip to content

Commit

Permalink
Merge pull request #4 from harsimranmaan/cachefix
Browse files Browse the repository at this point in the history
Create cache directory if it doesn't exist
  • Loading branch information
SimonWaldherr committed Oct 10, 2017
2 parents 274b474 + 10b585d commit f0ce476
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions demo/webexample.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package main

import (
bbmandelbrot "simonwaldherr.de/go/bbmandelbrotGo"
"encoding/base64"
"fmt"
"image/png"
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit f0ce476

Please sign in to comment.