-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.go
83 lines (71 loc) · 1.8 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
75
76
77
78
79
80
81
82
83
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"github.com/xenowits/nakamoto-coefficient-calculator/core/chains"
"log"
"sort"
"sync"
"time"
)
type JsonResponse struct {
ChainName string `json:"chain_name"`
ChainToken string `json:"chain_token"`
NakaCoPrevVal int `json:"naka_co_prev_val"`
NakaCoCurrVal int `json:"naka_co_curr_val"`
Change int `json:"naka_co_change_val"`
}
func main() {
var mu sync.Mutex
chainState := chains.NewState()
// Run a goroutine which refreshes state after every interval.
ticker := time.NewTicker(6 * time.Hour)
quit := make(chan struct{})
defer close(quit)
go func(state chains.ChainState) {
for {
select {
case <-ticker.C:
log.Println("Ticker ticked")
newState := chains.RefreshChainState(chainState)
mu.Lock()
chainState = newState
mu.Unlock()
fmt.Println(chainState)
case <-quit:
ticker.Stop()
return
}
}
}(chainState)
// Run server.
gin.SetMode(gin.ReleaseMode)
r := gin.Default()
r.GET("/naka-coeffs", func(c *gin.Context) {
coefficients := getListOfCoefficients(chainState)
c.Header("Access-Control-Allow-Origin", "*")
c.JSON(200, gin.H{
"coefficients": coefficients,
})
})
r.Run(":8080") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
func getListOfCoefficients(state chains.ChainState) []JsonResponse {
var coeffs []JsonResponse
for token, chain := range state {
coeffs = append(coeffs, JsonResponse{
ChainName: token.ChainName(),
ChainToken: string(token),
NakaCoPrevVal: chain.PrevNCVal,
NakaCoCurrVal: chain.CurrNCVal,
Change: chain.CurrNCVal - chain.PrevNCVal,
})
}
sort.Slice(coeffs, func(i, j int) bool {
if coeffs[i].ChainToken < coeffs[j].ChainToken {
return true
}
return false
})
return coeffs
}