-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
nodes_controller.go
68 lines (55 loc) · 1.56 KB
/
nodes_controller.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
package web
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/manyminds/api2go/jsonapi"
"github.com/smartcontractkit/chainlink-relay/pkg/types"
"github.com/smartcontractkit/chainlink/v2/core/chains"
"github.com/smartcontractkit/chainlink/v2/core/logger/audit"
)
type NodesController interface {
// Index lists nodes, and optionally filters by chain id.
Index(c *gin.Context, size, page, offset int)
}
type nodesController[R jsonapi.EntityNamer] struct {
nodeSet chains.Nodes
errNotEnabled error
newResource func(status types.NodeStatus) R
auditLogger audit.AuditLogger
}
func newNodesController[R jsonapi.EntityNamer](
nodeSet chains.Nodes,
errNotEnabled error,
newResource func(status types.NodeStatus) R,
auditLogger audit.AuditLogger,
) NodesController {
return &nodesController[R]{
nodeSet: nodeSet,
errNotEnabled: errNotEnabled,
newResource: newResource,
auditLogger: auditLogger,
}
}
func (n *nodesController[R]) Index(c *gin.Context, size, page, offset int) {
if n.nodeSet == nil {
jsonAPIError(c, http.StatusBadRequest, n.errNotEnabled)
return
}
id := c.Param("ID")
var nodes []types.NodeStatus
var count int
var err error
if id == "" {
// fetch all nodes
nodes, count, err = n.nodeSet.NodeStatuses(c, offset, size)
} else {
// fetch nodes for chain ID
nodes, count, err = n.nodeSet.NodeStatuses(c, offset, size, id)
}
var resources []R
for _, node := range nodes {
res := n.newResource(node)
resources = append(resources, res)
}
paginatedResponse(c, "node", size, page, resources, count, err)
}