forked from open-falcon-archive/query
/
web.go
66 lines (56 loc) · 1.79 KB
/
web.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package ginHttp
import (
"time"
log "github.com/Sirupsen/logrus"
cmodel "github.com/Cepave/common/model"
"github.com/Cepave/query/g"
"github.com/Cepave/query/gin_http/computeFunc"
"github.com/Cepave/query/gin_http/openFalcon"
"github.com/gin-gonic/gin"
)
type QueryInput struct {
StartTs time.Time
EndTs time.Time
ComputeMethod string
Endpoint string
Counter string
}
//this function will generate query string obj for QueryRRDtool
func getq(q QueryInput) cmodel.GraphQueryParam {
request := cmodel.GraphQueryParam{
Start: q.StartTs.Unix(),
End: q.EndTs.Unix(),
ConsolFun: q.ComputeMethod,
Endpoint: q.Endpoint,
Counter: q.Counter,
}
return request
}
//accept cross domain request
func CORSMiddleware() gin.HandlerFunc {
return func(c *gin.Context) {
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
c.Writer.Header().Set("Access-Control-Allow-Headers", "Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization, accept, origin, Cache-Control, X-Requested-With")
c.Writer.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS, GET, PUT")
if c.Request.Method == "OPTIONS" {
c.AbortWithStatus(204)
return
}
c.Next()
}
}
func StartWeb() {
handler := gin.Default()
handler.Use(CORSMiddleware())
compute := handler.Group("/func")
conf := g.Config()
compute.GET("/compute", computeFunc.Compute)
compute.GET("/funcations", computeFunc.GetAvaibleFun)
compute.GET("/smapledata", computeFunc.GetTestData)
openfalcon := handler.Group("/owl")
openfalcon.GET("/endpoints", openFalcon.GetEndpoints)
openfalcon.GET("/queryrrd", openFalcon.QueryData)
log.Println("open gin port on:", conf.GinHttp.Listen)
handler.Run(conf.GinHttp.Listen)
}