forked from fagongzi/manba
/
api_analysis.go
86 lines (69 loc) · 1.7 KB
/
api_analysis.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package server
import (
"encoding/json"
"io"
"net/http"
"strconv"
"github.com/fagongzi/gateway/pkg/model"
"github.com/labstack/echo"
)
// AnalysisPoint analysis point
type AnalysisPoint struct {
ProxyAddr string `json:"proxyAddr,omitempty"`
ServerAddr string `json:"serverAddr,omitempty"`
Secs int `json:"secs,omitempty"`
}
func unMarshalAnalysisPointFromReader(r io.Reader) (*AnalysisPoint, error) {
v := &AnalysisPoint{}
decoder := json.NewDecoder(r)
err := decoder.Decode(v)
return v, err
}
func (server *AdminServer) getAnalysis() echo.HandlerFunc {
return func(c echo.Context) error {
var errstr string
code := CodeSuccess
proxyAddr := c.Param("proxy")
serverAddr := c.Param("server")
secs, err := strconv.Atoi(c.Param("secs"))
if nil != err {
return c.JSON(http.StatusOK, &Result{
Code: code,
Error: errstr,
})
}
registor, _ := server.store.(model.Register)
data, err := registor.GetAnalysisPoint(proxyAddr, serverAddr, secs)
if err != nil {
errstr = err.Error()
code = CodeError
}
return c.JSON(http.StatusOK, &Result{
Code: code,
Error: errstr,
Value: data,
})
}
}
func (server *AdminServer) newAnalysis() echo.HandlerFunc {
return func(c echo.Context) error {
var errstr string
code := CodeSuccess
point, err := unMarshalAnalysisPointFromReader(c.Request().Body())
if nil != err {
errstr = err.Error()
code = CodeError
} else {
registor, _ := server.store.(model.Register)
err := registor.AddAnalysisPoint(point.ProxyAddr, point.ServerAddr, point.Secs)
if nil != err {
errstr = err.Error()
code = CodeError
}
}
return c.JSON(http.StatusOK, &Result{
Code: code,
Error: errstr,
})
}
}