Created
June 30, 2018 10:39
-
-
Save trusch/864c6f2262eb0649fe2409c420007e1d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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