-
Notifications
You must be signed in to change notification settings - Fork 0
/
dash-backend.go
147 lines (124 loc) · 3.45 KB
/
dash-backend.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
package main
import (
"context"
"fmt"
"github.com/spacemeshos/dash-backend/utils"
"github.com/spacemeshos/economics/vesting"
"io"
"math/rand"
"net/http"
"os"
"time"
"github.com/urfave/cli/v2"
"github.com/spacemeshos/go-spacemesh/log"
"github.com/spacemeshos/dash-backend/client"
"github.com/spacemeshos/dash-backend/history"
)
var (
version string
commit string
branch string
)
var (
listenStringFlag string
mongoDbUrlStringFlag string
mongoDbNameStringFlag string
)
var flags = []cli.Flag{
&cli.StringFlag{
Name: "listen",
Usage: "Dashboar API listen string in format <host>:<port>",
Required: false,
Destination: &listenStringFlag,
Value: ":8080",
},
&cli.StringFlag{
Name: "mongodb",
Usage: "Explorer MongoDB Uri string in format mongodb://<host>:<port>",
Required: false,
Destination: &mongoDbUrlStringFlag,
Value: "mongodb://localhost:27017",
},
&cli.StringFlag{
Name: "db",
Usage: "MongoDB Explorer database name string",
Required: false,
Destination: &mongoDbNameStringFlag,
Value: "explorer",
},
}
func main() {
app := cli.NewApp()
app.Name = "Spacemesh Dashboard API Server"
app.Version = fmt.Sprintf("%s, commit '%s', branch '%s'", version, commit, branch)
app.Flags = flags
app.Writer = os.Stderr
app.Action = func(ctx *cli.Context) error {
rand.Seed(time.Now().UTC().UnixNano())
env, ok := os.LookupEnv("SPACEMESH_API_LISTEN")
if ok {
listenStringFlag = env
}
env, ok = os.LookupEnv("SPACEMESH_MONGO_URI")
if ok {
mongoDbUrlStringFlag = env
}
env, ok = os.LookupEnv("SPACEMESH_MONGO_DB")
if ok {
mongoDbNameStringFlag = env
}
bus := client.NewBus()
go bus.Run()
history, err := history.NewHistory(nil, bus, mongoDbUrlStringFlag, mongoDbNameStringFlag)
if err != nil {
log.Err(fmt.Errorf("Create History service error: %v", err))
return err
}
go history.Run()
http.HandleFunc("/ws", func(w http.ResponseWriter, r *http.Request) {
client.ServeWs(bus, w, r)
})
http.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
w.Header().Set("Access-Control-Allow-Origin", "*")
networkInfo, err := history.GetStorage().GetNetworkInfo(context.Background())
if err == nil && networkInfo.IsSynced {
w.WriteHeader(http.StatusOK)
io.WriteString(w, "SYNCED")
} else {
w.WriteHeader(http.StatusTooEarly)
io.WriteString(w, "SYNCING")
}
})
http.HandleFunc("/circulating", func(w http.ResponseWriter, r *http.Request) {
networkInfo, err := history.GetStorage().GetNetworkInfo(context.Background())
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
accumulatedVest := vesting.AccumulatedVestAtLayer(networkInfo.TopLayer)
sum, err := history.GetStorage().GetRewardsTotalSum(context.Background())
if err != nil {
log.Warning("GetRewardsTotalSum error: %v", err)
w.WriteHeader(http.StatusInternalServerError)
return
}
var circulation = int64(accumulatedVest) + sum
c := utils.ParseSmidge(float64(circulation))
w.WriteHeader(http.StatusOK)
w.Write([]byte(c.Value))
})
err = http.ListenAndServe(listenStringFlag, nil)
if err != nil {
log.Err(fmt.Errorf("Create HTTP ssrver error: %v", err))
return err
}
log.Info("Server is shutdown")
return nil
}
if err := app.Run(os.Args); err != nil {
log.Info("%v", err)
os.Exit(1)
}
os.Exit(0)
}