-
Notifications
You must be signed in to change notification settings - Fork 21
/
api.go
157 lines (128 loc) · 3.37 KB
/
api.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
148
149
150
151
152
153
154
155
156
157
package main
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"github.com/gorilla/mux"
)
type apiServer struct {
db *database
conf *config
}
func (as *apiServer) revenueHandler(w http.ResponseWriter, r *http.Request) {
var raw []byte
header := w.Header()
header.Set("Content-Type", "application/json")
header.Set("Access-Control-Allow-Origin", "*")
table := as.db.getLastDayRevenue()
raw, _ = json.Marshal(table)
_, _ = w.Write(raw)
}
func (as *apiServer) sharesHandler(w http.ResponseWriter, r *http.Request) {
var raw []byte
header := w.Header()
header.Set("Content-Type", "application/json")
header.Set("Access-Control-Allow-Origin", "*")
table := as.db.getShares()
raw, _ = json.Marshal(table)
_, _ = w.Write(raw)
}
func (as *apiServer) poolHandler(w http.ResponseWriter, r *http.Request) {
//var blockBatch []string
header := w.Header()
header.Set("Content-Type", "application/json")
header.Set("Access-Control-Allow-Origin", "*")
//blockBatch = as.db.getAllBlockHashes()
req, _ := http.NewRequest("GET", "http://"+as.conf.Node.Address+":"+strconv.Itoa(as.conf.Node.APIPort)+"/v1/status", nil)
req.SetBasicAuth(as.conf.Node.AuthUser, as.conf.Node.AuthPass)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
log.Error(err)
return
}
dec := json.NewDecoder(res.Body)
var nodeStatus interface{}
_ = dec.Decode(&nodeStatus)
table := map[string]interface{}{
"node_status": nodeStatus,
//"mined_blocks": blockBatch,
}
raw, err := json.Marshal(table)
if err != nil {
log.Error(err)
return
}
_, _ = w.Write(raw)
}
type registerPaymentMethodForm struct {
Pass string `json:"pass"`
PaymentMethod string `json:"pm"`
}
func (as *apiServer) minerHandler(w http.ResponseWriter, r *http.Request) {
var raw []byte
header := w.Header()
header.Set("Content-Type", "application/json")
header.Set("Access-Control-Allow-Origin", "*")
vars := mux.Vars(r)
login := vars["miner_login"]
switch r.Method {
case "POST":
rawBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Error(err)
return
}
var form registerPaymentMethodForm
err = json.Unmarshal(rawBody, &form)
if err != nil {
log.Error(err)
return
}
if as.db.verifyMiner(login, form.Pass) == correctPassword {
as.db.updatePayment(login, form.PaymentMethod)
raw = []byte("{'status':'ok'}")
} else {
raw = []byte("{'status':'failed'}")
}
break
default: // GET
var err error
m := as.db.getMinerStatus(login)
raw, err = json.Marshal(m)
if err != nil {
log.Error(err)
return
}
}
_, _ = w.Write(raw)
}
func (as *apiServer) blocksHandler(w http.ResponseWriter, r *http.Request) {
var raw []byte
header := w.Header()
header.Set("Content-Type", "application/json")
header.Set("Access-Control-Allow-Origin", "*")
blocks := as.db.getAllMinedBlockHashes()
raw, err := json.Marshal(blocks)
if err != nil {
log.Error(err)
return
}
_, _ = w.Write(raw)
}
func initAPIServer(db *database, conf *config) {
as := &apiServer{
db: db,
conf: conf,
}
r := mux.NewRouter()
r.HandleFunc("/pool", as.poolHandler)
r.HandleFunc("/miner/{miner_login}", as.minerHandler)
r.HandleFunc("/revenue", as.revenueHandler)
r.HandleFunc("/shares", as.sharesHandler)
r.HandleFunc("/blocks", as.blocksHandler)
http.Handle("/", r)
go log.Fatal(
http.ListenAndServe(conf.APIServer.Address+":"+strconv.Itoa(conf.APIServer.Port), nil))
}