-
Notifications
You must be signed in to change notification settings - Fork 17
/
head.go
106 lines (91 loc) · 2.41 KB
/
head.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
package handlers
import (
"net/http"
"sort"
"time"
"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/models/block"
"github.com/baking-bad/bcdhub/internal/models/types"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
"github.com/rs/zerolog/log"
)
// GetHead godoc
// @Summary Show indexer head
// @Description Get indexer head for each network
// @Tags head
// @ID get-indexer-head
// @Accept json
// @Produce json
// @Success 200 {array} Head
// @Failure 500 {object} Error
// @Router /v1/head [get]
func GetHead() gin.HandlerFunc {
return func(c *gin.Context) {
ctxs := c.MustGet("contexts").(config.Contexts)
body := make([]Head, 0)
for network, ctx := range ctxs {
block, err := ctx.Blocks.Last(c.Request.Context())
if err != nil {
if ctx.Storage.IsRecordNotFound(err) {
continue
}
handleError(c, ctx.Storage, err, 0)
return
}
head, err := getHead(ctx, network, block)
if err != nil {
log.Warn().Str("network", network.String()).Err(err).Msg("head api")
continue
}
body = append(body, head)
}
sort.Sort(HeadsByNetwork(body))
c.SecureJSON(http.StatusOK, body)
}
}
// GetHeadByNetwork godoc
// @Summary Show indexer head for the network
// @Description Get indexer head for the network
// @Tags head
// @ID get-indexer-head-by-network
// @Param network path string true "Network"
// @Accept json
// @Produce json
// @Success 200 {object} Head
// @Failure 500 {object} Error
// @Router /v1/head/{network} [get]
func GetHeadByNetwork() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.MustGet("context").(*config.Context)
block, err := ctx.Blocks.Last(c.Request.Context())
if handleError(c, ctx.Storage, err, 0) {
return
}
head, err := getHead(ctx, ctx.Network, block)
if handleError(c, ctx.Storage, err, 0) {
return
}
c.SecureJSON(http.StatusOK, head)
}
}
func getHead(ctx *config.Context, network types.Network, block block.Block) (Head, error) {
var found bool
for j := range ctx.Config.API.Networks {
if network.String() == ctx.Config.API.Networks[j] {
found = true
break
}
}
if !found {
return Head{}, errors.New("unknown network")
}
return Head{
network: network,
Network: network.String(),
Level: block.Level,
Timestamp: block.Timestamp.UTC(),
Protocol: block.Protocol.Hash,
Synced: !block.Timestamp.UTC().Add(2 * time.Minute).Before(time.Now().UTC()),
}, nil
}