-
Notifications
You must be signed in to change notification settings - Fork 3
/
http_api.go
48 lines (39 loc) · 1.05 KB
/
http_api.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package plot
import (
"html/template"
"github.com/antongulenko/golib"
"github.com/gin-gonic/gin"
)
// Requirement: go get github.com/mjibson/esc
//go:generate esc -o static_files_generated.go -pkg plotHttp -prefix static/ static
func (p *HttpPlotter) serve() error {
engine := golib.NewGinEngine()
index := template.New("index")
indexStr, err := FSString(p.UseLocalStatic, "/index.html")
if err != nil {
return err
}
if _, err := index.Parse(indexStr); err != nil {
return err
}
engine.SetHTMLTemplate(index)
engine.GET("/", p.serveMain)
engine.GET("/metrics", p.serveListData)
engine.GET("/data", p.serveData)
engine.StaticFS("/static", FS(p.UseLocalStatic))
return engine.Run(p.Endpoint)
}
func (p *HttpPlotter) serveMain(c *gin.Context) {
c.HTML(200, "index", nil)
}
func (p *HttpPlotter) serveListData(c *gin.Context) {
c.JSON(200, p.metricNames())
}
func (p *HttpPlotter) serveData(c *gin.Context) {
name := c.Request.FormValue("metric")
if len(name) == 0 {
c.JSON(200, p.allMetricData())
} else {
c.JSON(200, p.metricData(name))
}
}