Commit
Author: Noam Preil [noam@pixelhero.dev]
Committer: Drew DeVault [sir@cmpwn.com] Wed, 21 Oct 2020 23:04:47 +0000
Hash: 2b9570f135c5dc38c9bdf43c35074c541fd62253
Timestamp: Wed, 21 Oct 2020 23:04:47 +0000 (4 years ago)

+22 -15 +/-1 browse
Add /chart.png path
1diff --git a/main.go b/main.go
2index 2bce2d7..0eab06f 100644
3--- a/main.go
4+++ b/main.go
5 @@ -126,19 +126,8 @@ func metricName(metric map[string]string) string {
6 return out + "{" + strings.Join(inner, ",") + "}"
7 }
8
9- func main() {
10- plotutil.DefaultDashes = [][]vg.Length{{}}
11-
12- if len(os.Args) < 2 {
13- fmt.Printf("Usage: %s server\n", os.Args[0])
14- os.Exit(1)
15- }
16- upstream = os.Args[1]
17- router := chi.NewRouter()
18- router.Use(middleware.RealIP)
19- router.Use(middleware.Logger)
20-
21- router.Get("/chart.svg", func(w http.ResponseWriter, r *http.Request) {
22+ func registerExtension(router chi.Router, extension string, mime string) {
23+ router.Get("/chart."+extension, func(w http.ResponseWriter, r *http.Request) {
24 args := r.URL.Query()
25 var query string
26 if q, ok := args["query"]; !ok {
27 @@ -261,17 +250,35 @@ func main() {
28 p.Add(plotters[i])
29 }
30
31- writer, err := p.WriterTo(width, height, "svg")
32+ writer, err := p.WriterTo(width, height, extension)
33 if err != nil {
34 w.WriteHeader(400)
35 w.Write([]byte(fmt.Sprintf("%v", err)))
36 return
37 }
38
39- w.Header().Add("Content-Type", "image/svg+xml")
40+ w.Header().Add("Content-Type", mime)
41 writer.WriteTo(w)
42 })
43
44+ }
45+
46+ func main() {
47+ plotutil.DefaultDashes = [][]vg.Length{{}}
48+
49+ if len(os.Args) < 2 {
50+ fmt.Printf("Usage: %s server\n", os.Args[0])
51+ os.Exit(1)
52+ }
53+ upstream = os.Args[1]
54+ router := chi.NewRouter()
55+
56+ router.Use(middleware.RealIP)
57+ router.Use(middleware.Logger)
58+
59+ registerExtension(router, "svg", "image/svg+xml")
60+ registerExtension(router, "png", "image/png")
61+
62 addr := ":8142"
63 if len(os.Args) > 2 {
64 addr = os.Args[2]