forked from multiversx/mx-chain-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.go
153 lines (130 loc) · 3.54 KB
/
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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package api
import (
"bytes"
"fmt"
"net/http"
"reflect"
"github.com/ElrondNetwork/elrond-go/api/address"
"github.com/ElrondNetwork/elrond-go/api/middleware"
"github.com/ElrondNetwork/elrond-go/api/node"
"github.com/ElrondNetwork/elrond-go/api/transaction"
"github.com/ElrondNetwork/elrond-go/api/vmValues"
"github.com/gin-contrib/cors"
"github.com/gin-contrib/pprof"
"github.com/gin-gonic/gin"
"github.com/gin-gonic/gin/binding"
"github.com/gin-gonic/gin/json"
"github.com/prometheus/client_golang/prometheus/promhttp"
"gopkg.in/go-playground/validator.v8"
)
type validatorInput struct {
Name string
Validator validator.Func
}
type prometheus struct {
NodePort string
NetworkID string
}
// MainApiHandler interface defines methods that can be used from `elrondFacade` context variable
type MainApiHandler interface {
RestApiPort() string
RestAPIServerDebugMode() bool
PprofEnabled() bool
PrometheusMonitoring() bool
PrometheusJoinURL() string
PrometheusNetworkID() string
IsInterfaceNil() bool
}
// Start will boot up the api and appropriate routes, handlers and validators
func Start(elrondFacade MainApiHandler) error {
var ws *gin.Engine
if elrondFacade.RestAPIServerDebugMode() {
ws = gin.Default()
} else {
ws = gin.New()
ws.Use(gin.Recovery())
gin.SetMode(gin.ReleaseMode)
}
ws.Use(cors.Default())
err := registerValidators()
if err != nil {
return err
}
registerRoutes(ws, elrondFacade)
if elrondFacade.PrometheusMonitoring() {
err = joinMonitoringSystem(elrondFacade)
if err != nil {
return err
}
}
return ws.Run(fmt.Sprintf(":%s", elrondFacade.RestApiPort()))
}
func joinMonitoringSystem(elrondFacade MainApiHandler) error {
prometheusJoinUrl := elrondFacade.PrometheusJoinURL()
structToSend := prometheus{
NodePort: elrondFacade.RestApiPort(),
NetworkID: elrondFacade.PrometheusNetworkID(),
}
jsonValue, err := json.Marshal(structToSend)
if err != nil {
return err
}
req, err := http.NewRequest("POST", prometheusJoinUrl, bytes.NewBuffer(jsonValue))
if err != nil {
return err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
err = resp.Body.Close()
return err
}
func registerRoutes(ws *gin.Engine, elrondFacade middleware.ElrondHandler) {
nodeRoutes := ws.Group("/node")
nodeRoutes.Use(middleware.WithElrondFacade(elrondFacade))
node.Routes(nodeRoutes)
addressRoutes := ws.Group("/address")
addressRoutes.Use(middleware.WithElrondFacade(elrondFacade))
address.Routes(addressRoutes)
txRoutes := ws.Group("/transaction")
txRoutes.Use(middleware.WithElrondFacade(elrondFacade))
transaction.Routes(txRoutes)
vmValuesRoutes := ws.Group("/vm-values")
vmValuesRoutes.Use(middleware.WithElrondFacade(elrondFacade))
vmValues.Routes(vmValuesRoutes)
apiHandler, ok := elrondFacade.(MainApiHandler)
if ok && apiHandler.PrometheusMonitoring() {
nodeRoutes.GET("/metrics", gin.WrapH(promhttp.Handler()))
}
if apiHandler.PprofEnabled() {
pprof.Register(ws)
}
}
func registerValidators() error {
validators := []validatorInput{
{Name: "skValidator", Validator: skValidator},
}
for _, validatorFunc := range validators {
if v, ok := binding.Validator.Engine().(*validator.Validate); ok {
err := v.RegisterValidation(validatorFunc.Name, validatorFunc.Validator)
if err != nil {
return err
}
}
}
return nil
}
// skValidator validates a secret key from user input for correctness
func skValidator(
_ *validator.Validate,
_ reflect.Value,
_ reflect.Value,
_ reflect.Value,
_ reflect.Type,
_ reflect.Kind,
_ string,
) bool {
return true
}