-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebserver.go
222 lines (183 loc) · 5.59 KB
/
webserver.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package main
import (
"fmt"
"github.com/gorilla/mux"
"html/template"
"net/http"
"path/filepath"
"strconv"
)
//Precache all templates in folder templates at start
var templates = template.Must(template.ParseFiles(filepath.Join("templates", "miners.html"),
filepath.Join("templates", "index.html"),
filepath.Join("templates", "miner.html")))
//Starts the webserver
func webServerMain(port int) {
r := mux.NewRouter()
r.HandleFunc("/", HomeHandler)
r.HandleFunc("/miner/{key:[a-zA-Z0-9]+}", MinerHandler)
r.HandleFunc("/miner/{key:[a-zA-Z0-9]+}/onoff", EnableDisableHandler)
r.HandleFunc("/miner/{key:[a-zA-Z0-9]+}/gpu", GPUHandler)
r.HandleFunc("/miners", MinersHandler)
r.PathPrefix("/").Handler(http.FileServer(http.Dir("./web-root/")))
http.Handle("/", r)
http.ListenAndServe(":"+strconv.Itoa(port), nil)
}
//Request handler for a single miner information
func MinerHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
key := vars["key"]
//Check if a miner with that name exists
if _, exists := miners[key]; !exists {
http.Error(w, "404 - No miner with that name", http.StatusNotFound)
return
}
miner := MinerWrapper{}
miner.Name = key
//Get the array that hold the information about the devs
miners[key].DevsWrap.Mu.RLock()
miner.Devs = miners[key].DevsWrap.Devs
miners[key].DevsWrap.Mu.RUnlock()
//fmt.Printf("Onoff: %s\n", miner.Devs.Devs[0].OnOff)
err := templates.ExecuteTemplate(w, "miner.html", miner)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func EnableDisableHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
key := vars["key"]
//Parse the values
statusNumber, err := strconv.Atoi(r.FormValue("status"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
deviceNumber, err := strconv.Atoi(r.FormValue("device"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
enableDisable(statusNumber, deviceNumber, key)
//And before we redirect we update the devs information
//But dont to the threshold check
UpdateDevs(key, false)
http.Redirect(w, r, "/miner/"+key, http.StatusFound)
}
//Request handler for a creatin summary for all miners
func MinersHandler(w http.ResponseWriter, r *http.Request) {
//Generate the correct structure for the template
tempMiners := createMinersTemplate()
err := templates.ExecuteTemplate(w, "miners.html", tempMiners)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func createMinersTemplate() MinersTemplate {
var rows []MinerRow
for _, value := range miners {
var minerStructTemp = *value
//Lock it
minerStructTemp.SumWrap.Mu.RLock()
//Add it
rows = append(rows, minerStructTemp.SumWrap.SummaryRow)
//Unlock it
minerStructTemp.SumWrap.Mu.RUnlock()
}
return MinersTemplate{rows}
}
//Default handler
func HomeHandler(w http.ResponseWriter, r *http.Request) {
hw := HomeWrapper{}
//Calculate the total hashrate
for _, value := range miners {
var minerStructTemp = *value
hw.TotalMHS += minerStructTemp.SumWrap.SummaryRow.MHSAv
}
err := templates.ExecuteTemplate(w, "index.html", hw)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func GPUHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
key := vars["key"]
//Parse the values
deviceNumber, err := strconv.Atoi(r.FormValue("device"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
gpuClock, err := strconv.Atoi(r.FormValue("GPUClock"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//Check so it's in the right interval
if gpuClock < 150 || gpuClock > 1200 {
http.Error(w, "GPUClock must be between 150-1200", http.StatusInternalServerError)
}
gpuMemory, err := strconv.Atoi(r.FormValue("MemoryClock"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//Check so it's in the right interval
if gpuMemory < 75 || gpuMemory > 1575 {
http.Error(w, "GPUClock must be between 150-1200", http.StatusInternalServerError)
}
/*
* Note:
* Possible bugg there. It might lose precision
* with the convert! Looking into this later
*/
vddc, err := strconv.ParseFloat(r.FormValue("Voltage"), 128)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//Check so it's in the right interval
if vddc < 0.800 || vddc > 1.250 {
http.Error(w, "GPUClock must be between 150-1200", http.StatusInternalServerError)
}
intensity, err := strconv.Atoi(r.FormValue("Intensity"))
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
//Check so it's in the right interval
if intensity < -10 || intensity > 20 {
http.Error(w, "GPUClock must be between 150-1200", http.StatusInternalServerError)
}
config := r.FormValue("Config")
fmt.Println(gpuClock, deviceNumber, gpuMemory, vddc, intensity)
setGPUEngine(gpuClock, deviceNumber, key)
setGPUMemory(gpuMemory, deviceNumber, key)
setVDDC(vddc, deviceNumber, key)
setIntensity(intensity, deviceNumber, key)
//Check if we shold write config file
if config == "on" {
writeConfig(key)
}
//And before we redirect we update the devs information
//But dont to the threshold check
UpdateDevs(key, false)
http.Redirect(w, r, "/miner/"+key, http.StatusFound)
}
type MinerWrapper struct {
Name string
Devs DevsResponse
}
type HomeWrapper struct {
TotalMHS float64
}
type MinersTemplate struct {
Rows []MinerRow
}
type MinerRow struct {
Name string
Accepted int
Rejected int
MHSAv float64
BestShare int
}