Example #1
0
func webservice(listen string) {
	http.HandleFunc("/terminal", func(w http.ResponseWriter, r *http.Request) {
		input, err := ioutil.ReadAll(r.Body)
		check("could not read from HTTP stream", err)
		w.Write(wrapPreview(terminal.Render(input)))
	})

	log.Printf("Listening on %s", listen)
	log.Fatal(http.ListenAndServe(listen, nil))
}
Example #2
0
func stdin() {
	var input []byte
	var err error
	if len(flag.Arg(0)) > 0 {
		input, err = ioutil.ReadFile(flag.Arg(0))
		check(fmt.Sprintf("could not read %s", flag.Arg(0)), err)
	} else {
		input, err = ioutil.ReadAll(os.Stdin)
		check("could not read stdin", err)
	}
	fmt.Printf("%s", wrapPreview(terminal.Render(input)))
}
Example #3
0
func handler(w http.ResponseWriter, r *http.Request) {
	// extract query from url
	u, err := url.Parse(r.URL.String())
	if err != nil {
		fmt.Fprintf(w, "URL error: %v:\n", err)
		return
	}

	// parse query
	m, err := url.ParseQuery(u.RawQuery)
	if err != nil {
		fmt.Fprintf(w, "URL query error: %v:\n", err)
		return
	}

	// check if no arguments
	if len(m) == 0 {
		http.Redirect(w, r, "?size=5&balls=4&star=true&emoji=false&color=false&presents=true&garlands=5", http.StatusFound)
	}

	// unmarshal arguments
	opts := Options{}
	decoder := schema.NewDecoder()
	err = decoder.Decode(&opts, m)
	if err != nil {
		fmt.Fprintf(w, "Parameters error: %v:\n", err)
		return
	}

	// check size argument
	if opts.Size > 10 {
		fmt.Fprintf(w, "Max size is: 10\n")
		return
	}
	if opts.Size < 0 {
		fmt.Fprintf(w, "Min size is: 0\n")
		return
	}

	if opts.Balls < 0 {
		fmt.Fprintf(w, "Min value for balls is 0\n")
		return
	}
	if opts.Balls > 100 {
		fmt.Fprintf(w, "Max value for balls is 100\n")
		return
	}
	if opts.Garlands < 0 {
		fmt.Fprintf(w, "Min value for garlands is 0\n")
		return
	}
	if opts.Garlands > 50 {
		fmt.Fprintf(w, "Max value for garlands is 50\n")
		return
	}

	sapin := sapin.NewSapin(opts.Size)
	if opts.Star {
		sapin.AddStar()
	}
	sapin.AddBalls(opts.Balls)
	sapin.AddGarlands(opts.Garlands)
	if opts.Emoji {
		sapin.Emojize()
	}
	if opts.Color {
		w.Header().Set("Content-Type", "text/html")
		sapin.Colorize()
		if opts.Presents {
			sapin.AddPresents()
		}
		coloredOutput := string(terminal.Render([]byte(sapin.String())))
		html := strings.Replace(htmlTemplate, "CONTENT", coloredOutput, 1)
		fmt.Fprint(w, html)
	} else {
		w.Header().Set("Content-Type", "text/plain; charset=utf-8")
		if opts.Presents {
			sapin.AddPresents()
		}
		fmt.Fprintf(w, "%s\n", sapin)
	}
}