-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (60 loc) · 1.66 KB
/
main.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
package main
import (
"encoding/json"
"io"
"log"
"net/http"
"time"
env "github.com/caitlinelfring/go-env-default"
)
type BlockSnapshot struct {
Level int
Timestamp time.Time
}
type BlockHeader struct {
Level int `json:"level"`
Timestamp time.Time `json:"timestamp"`
}
var lastBlock BlockSnapshot = BlockSnapshot{Level: 0, Timestamp: time.Now()}
func main() {
addr := env.GetDefault("ADDR", ":31234")
minutes := env.GetIntDefault("MINUTES", 5)
tezosURI := env.GetDefault("TEZOS_URI", "https://mainnet.tezos.marigold.dev")
log.Printf("Listening on %s\n", addr)
// Health endpoint
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
level, err := request(tezosURI)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
// If the new level is greater than the last level
// or the timestamp is less than X minutes ago
// return 200
if level.Level > lastBlock.Level || level.Timestamp.Sub(time.Now()) <= (time.Duration(minutes)*time.Minute) {
lastBlock = *level
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusInternalServerError)
})
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatal(err)
}
}
func request(tezosURI string) (*BlockSnapshot, error) {
resp, err := http.Get(tezosURI + "/chains/main/blocks/head/header")
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
var block BlockHeader
json.Unmarshal(body, &block)
log.Printf("Level: %d, Timestamp: %s", block.Level, block.Timestamp)
result := &BlockSnapshot{Level: block.Level, Timestamp: block.Timestamp}
return result, nil
}