Skip to content

Instantly share code, notes, and snippets.

@trusch
Created June 30, 2018 10:39
Show Gist options
  • Save trusch/864c6f2262eb0649fe2409c420007e1d to your computer and use it in GitHub Desktop.
Save trusch/864c6f2262eb0649fe2409c420007e1d to your computer and use it in GitHub Desktop.
package main
import (
"encoding/json"
"net/http"
"strconv"
"github.com/guptarohit/asciigraph"
)
// supports width and height parameters.
// example: curl -d '[1,2,3,2,1,2,3]' http://localhost:8080\?width=80\&height=20
func main() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
data := []float64{}
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
opts := []asciigraph.Option{}
if wStr := r.URL.Query().Get("width"); wStr != "" {
width, err := strconv.ParseInt(wStr, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
opts = append(opts, asciigraph.Width(int(width)))
}
if hStr := r.URL.Query().Get("height"); hStr != "" {
height, err := strconv.ParseInt(hStr, 10, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(err.Error()))
return
}
opts = append(opts, asciigraph.Height(int(height)))
}
plot := asciigraph.Plot(data, opts...)
w.Write([]byte(plot + "\n"))
})
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment