Beispiel #1
0
func main() {
	runtime.GOMAXPROCS(runtime.NumCPU())

	cmdargs := cmdline.Cmdline()
	if cmdargs.Err != nil {
		fmt.Fprintf(os.Stderr, "Failed to parse commandline: %s", cmdargs.Err)
		os.Exit(1)
	}

	if cmdargs.Filepath != "" {
		// algo is already validated by cmdline.Cmdline
		img := img.BuildImg(&cmdargs.ImgParams)
		if err := cmdline.WriteImg(img, cmdargs.Filepath); err != nil {
			fmt.Fprintf(os.Stderr, "Failed to write Image to file: %s", err)
			os.Exit(1)
		}
		os.Exit(0)
	}

	if cmdargs.Ssocket != "" {
		http.HandleFunc("/", web.ServeHTTP)
		// Forever
		err := http.ListenAndServe(cmdargs.Ssocket, nil)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Failed to start http server: %s", err)
			os.Exit(1)
		}
	}

	// Never reached, cmdline.Cmdline() makes sure that either cmdargs.Filepath
	// or cmdargs.Ssocket evaluates to true.
}
Beispiel #2
0
// ServeHTTP responds with a PNG image of desired fractal to the client.
func ServeHTTP(rW http.ResponseWriter, r *http.Request) {
	var err error

	imgP, err := getHTTPReqData(r)
	if err != nil {
		rW.Header().Set("Content-Type", "text/plain")
		msg := fmt.Sprintf("getHTTPReqData failed: %v", err)
		if _, err = io.WriteString(rW, msg); err != nil {
			msg = msg + ", and io.WriteString failed as well (???)"
			fmt.Fprintf(os.Stderr, msg)
			os.Exit(1)
		}
		return
	}
	rW.Header().Set("Content-Type", "img/png")
	imgRes := img.BuildImg(imgP)

	err = png.Encode(rW, imgRes) // NOTE: ignoring errors
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error while sending image to client: %v\n", err)
	}
}