-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
509 lines (434 loc) · 14.5 KB
/
Copy pathapi.go
File metadata and controls
509 lines (434 loc) · 14.5 KB
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"log"
"net/http"
"strings"
"sync"
"time"
"github.com/google/uuid"
)
// API response types
type APIResponse struct {
Success bool `json:"success"`
Data interface{} `json:"data,omitempty"`
Error string `json:"error,omitempty"`
}
// RegisterAPIHandlers sets up all API routes
func RegisterAPIHandlers(mux *http.ServeMux, state *AppState, statusCache *StatusCache, upsCache *UPSStatusCache) {
// Device endpoints
mux.HandleFunc("GET /api/devices", handleAPIGetDevices(state))
mux.HandleFunc("POST /api/devices", handleAPIAddDevice(state))
mux.HandleFunc("PUT /api/devices/{id}", handleAPIUpdateDevice(state))
mux.HandleFunc("DELETE /api/devices/{id}", handleAPIDeleteDevice(state))
mux.HandleFunc("POST /api/devices/{id}/wake", handleAPIWakeDevice(state))
mux.HandleFunc("GET /api/devices/status", handleAPIDevicesStatus(statusCache))
// UPS endpoints
mux.HandleFunc("GET /api/ups", handleAPIGetUPS(state))
mux.HandleFunc("POST /api/ups", handleAPIAddUPS(state, upsCache))
mux.HandleFunc("PUT /api/ups/{id}", handleAPIUpdateUPS(state, upsCache))
mux.HandleFunc("DELETE /api/ups/{id}", handleAPIDeleteUPS(state, upsCache))
mux.HandleFunc("GET /api/ups/status", handleAPIUPSStatus(upsCache))
// Config endpoints
mux.HandleFunc("GET /api/config/export", handleAPIExport(state))
mux.HandleFunc("POST /api/config/import", handleAPIImport(state))
// Health check
mux.HandleFunc("GET /api/health", handleAPIHealth())
// Version
mux.HandleFunc("GET /api/version", handleAPIVersion())
mux.HandleFunc("GET /api/version/latest", handleAPILatestRelease(newLatestReleaseCache(time.Hour)))
}
// latestReleaseCache memoizes the GitHub /releases/latest response so that
// browser-side polls don't burn the unauthenticated GitHub rate limit
// (60/h/IP) — multiple tabs or NAT'd users would otherwise silently exhaust it.
//
// On a failed fetch it backs off for errTTL before contacting GitHub again and
// keeps serving the last good value (stale) in the meantime. Without this, a
// GitHub outage or a 403 rate-limit response would turn every poll into yet
// another GitHub call — amplifying the very rate-limiting the cache prevents.
type latestReleaseCache struct {
mu sync.RWMutex
version string
url string
ok bool // a successful fetch has happened at least once
fetchedAt time.Time // time of last successful fetch
lastErrAt time.Time // time of last failed fetch
ttl time.Duration // freshness window for a successful value
errTTL time.Duration // backoff window after a failed fetch
}
func newLatestReleaseCache(ttl time.Duration) *latestReleaseCache {
return &latestReleaseCache{ttl: ttl, errTTL: 5 * time.Minute}
}
type releaseSnapshot struct {
version string
url string
fresh bool // a successful value within ttl — serve it directly
stale bool // an older value exists, worth serving while GitHub is down
backoff bool // a fetch failed recently — don't contact GitHub yet
}
func (c *latestReleaseCache) snapshot() releaseSnapshot {
c.mu.RLock()
defer c.mu.RUnlock()
s := releaseSnapshot{version: c.version, url: c.url}
if c.ok {
s.stale = true
s.fresh = time.Since(c.fetchedAt) <= c.ttl
}
if !c.lastErrAt.IsZero() && time.Since(c.lastErrAt) < c.errTTL {
s.backoff = true
}
return s
}
func (c *latestReleaseCache) setSuccess(version, url string) {
c.mu.Lock()
defer c.mu.Unlock()
c.version, c.url, c.ok, c.fetchedAt = version, url, true, time.Now()
c.lastErrAt = time.Time{} // a fresh success clears the backoff window
}
func (c *latestReleaseCache) setError() {
c.mu.Lock()
defer c.mu.Unlock()
c.lastErrAt = time.Now()
}
func handleAPILatestRelease(cache *latestReleaseCache) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
snap := cache.snapshot()
if snap.fresh {
writeSuccess(w, map[string]string{"version": snap.version, "url": snap.url})
return
}
// A recent fetch failed: serve the stale value if we have one, otherwise
// fail fast — either way, don't contact GitHub again until errTTL passes.
if snap.backoff {
if snap.stale {
writeSuccess(w, map[string]string{"version": snap.version, "url": snap.url})
return
}
writeError(w, http.StatusServiceUnavailable, "Latest release temporarily unavailable")
return
}
// serveFailure records the failure (starting the backoff window) and
// serves the stale value if available, else the given error.
serveFailure := func(status int, msg string) {
cache.setError()
if snap.stale {
writeSuccess(w, map[string]string{"version": snap.version, "url": snap.url})
return
}
writeError(w, status, msg)
}
ctx, cancel := context.WithTimeout(r.Context(), 10*time.Second)
defer cancel()
const url = "https://api.github.com/repos/aloks98/wolnut/releases/latest"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
// Building the request never touches GitHub, so this isn't a reason
// to start the backoff window.
writeError(w, http.StatusInternalServerError, "Failed to build request")
return
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("User-Agent", "wol-nut/"+version)
resp, err := http.DefaultClient.Do(req)
if err != nil {
serveFailure(http.StatusBadGateway, "Failed to reach GitHub: "+err.Error())
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
serveFailure(http.StatusBadGateway, fmt.Sprintf("GitHub responded with %d", resp.StatusCode))
return
}
var rel struct {
TagName string `json:"tag_name"`
HTMLURL string `json:"html_url"`
Name string `json:"name"`
}
if err := json.NewDecoder(resp.Body).Decode(&rel); err != nil {
serveFailure(http.StatusBadGateway, "Failed to parse GitHub response")
return
}
ver := strings.TrimPrefix(rel.TagName, "v")
if ver == "" {
ver = rel.Name
}
cache.setSuccess(ver, rel.HTMLURL)
writeSuccess(w, map[string]string{"version": ver, "url": rel.HTMLURL})
}
}
func writeJSON(w http.ResponseWriter, status int, data interface{}) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(status)
if err := json.NewEncoder(w).Encode(data); err != nil {
log.Printf("writeJSON: encode error: %v", err)
}
}
func writeSuccess(w http.ResponseWriter, data interface{}) {
writeJSON(w, http.StatusOK, APIResponse{Success: true, Data: data})
}
func writeError(w http.ResponseWriter, status int, message string) {
writeJSON(w, status, APIResponse{Success: false, Error: message})
}
// Device handlers
func handleAPIGetDevices(state *AppState) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
devices := state.GetDevices()
writeSuccess(w, devices)
}
}
func handleAPIAddDevice(state *AppState) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var device Device
if err := json.NewDecoder(r.Body).Decode(&device); err != nil {
writeError(w, http.StatusBadRequest, "Invalid JSON body")
return
}
if device.Name == "" || device.MAC == "" {
writeError(w, http.StatusBadRequest, "Name and MAC are required")
return
}
device.MAC = NormalizeMAC(device.MAC)
if !ValidateMAC(device.MAC) {
writeError(w, http.StatusBadRequest, "Invalid MAC address format")
return
}
device.ID = uuid.New().String()
if err := state.AddDevice(device); err != nil {
writeError(w, http.StatusInternalServerError, "Failed to save device")
return
}
writeSuccess(w, device)
}
}
func handleAPIUpdateDevice(state *AppState) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if id == "" {
writeError(w, http.StatusBadRequest, "Device ID required")
return
}
var device Device
if err := json.NewDecoder(r.Body).Decode(&device); err != nil {
writeError(w, http.StatusBadRequest, "Invalid JSON body")
return
}
if device.Name == "" || device.MAC == "" {
writeError(w, http.StatusBadRequest, "Name and MAC are required")
return
}
device.ID = id
device.MAC = NormalizeMAC(device.MAC)
if !ValidateMAC(device.MAC) {
writeError(w, http.StatusBadRequest, "Invalid MAC address format")
return
}
if err := state.UpdateDevice(device); err != nil {
if errors.Is(err, ErrNotFound) {
writeError(w, http.StatusNotFound, "Device not found")
return
}
writeError(w, http.StatusInternalServerError, "Failed to update device")
return
}
writeSuccess(w, device)
}
}
func handleAPIDeleteDevice(state *AppState) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if id == "" {
writeError(w, http.StatusBadRequest, "Device ID required")
return
}
if err := state.DeleteDevice(id); err != nil {
if errors.Is(err, ErrNotFound) {
writeError(w, http.StatusNotFound, "Device not found")
return
}
writeError(w, http.StatusInternalServerError, "Failed to delete device")
return
}
writeSuccess(w, nil)
}
}
func handleAPIWakeDevice(state *AppState) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if id == "" {
writeError(w, http.StatusBadRequest, "Device ID required")
return
}
device, ok := state.GetDevice(id)
if !ok {
writeError(w, http.StatusNotFound, "Device not found")
return
}
if err := SendWakeOnLAN(device.MAC, device.IP); err != nil {
writeError(w, http.StatusInternalServerError, "Failed to send wake packet: "+err.Error())
return
}
writeSuccess(w, map[string]string{"message": "Wake packet sent to " + device.Name})
}
}
func handleAPIDevicesStatus(statusCache *StatusCache) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
statuses := statusCache.GetDevicesStatus()
writeSuccess(w, statuses)
}
}
// UPS handlers
func handleAPIGetUPS(state *AppState) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
upsList := state.GetUPSList()
writeSuccess(w, upsList)
}
}
func handleAPIAddUPS(state *AppState, cache *UPSStatusCache) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
var ups UPSEntry
if err := json.NewDecoder(r.Body).Decode(&ups); err != nil {
writeError(w, http.StatusBadRequest, "Invalid JSON body")
return
}
if ups.Name == "" || ups.Host == "" || ups.UPSName == "" {
writeError(w, http.StatusBadRequest, "Name, Host, and UPSName are required")
return
}
if !ValidateUPSHost(ups.Host) {
writeError(w, http.StatusBadRequest, "Invalid Host: expected 'hostname' or 'hostname:port'")
return
}
if !ValidateUPSName(ups.UPSName) {
writeError(w, http.StatusBadRequest, "Invalid UPSName: alphanumerics, dot, dash, underscore only")
return
}
ups.ID = uuid.New().String()
if err := state.AddUPS(ups); err != nil {
writeError(w, http.StatusInternalServerError, "Failed to save UPS")
return
}
go cache.Refresh()
writeSuccess(w, ups)
}
}
func handleAPIUpdateUPS(state *AppState, cache *UPSStatusCache) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if id == "" {
writeError(w, http.StatusBadRequest, "UPS ID required")
return
}
var ups UPSEntry
if err := json.NewDecoder(r.Body).Decode(&ups); err != nil {
writeError(w, http.StatusBadRequest, "Invalid JSON body")
return
}
if ups.Name == "" || ups.Host == "" || ups.UPSName == "" {
writeError(w, http.StatusBadRequest, "Name, Host, and UPSName are required")
return
}
if !ValidateUPSHost(ups.Host) {
writeError(w, http.StatusBadRequest, "Invalid Host: expected 'hostname' or 'hostname:port'")
return
}
if !ValidateUPSName(ups.UPSName) {
writeError(w, http.StatusBadRequest, "Invalid UPSName: alphanumerics, dot, dash, underscore only")
return
}
ups.ID = id
if err := state.UpdateUPS(ups); err != nil {
if errors.Is(err, ErrNotFound) {
writeError(w, http.StatusNotFound, "UPS not found")
return
}
writeError(w, http.StatusInternalServerError, "Failed to update UPS")
return
}
go cache.Refresh()
writeSuccess(w, ups)
}
}
func handleAPIDeleteUPS(state *AppState, cache *UPSStatusCache) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
if id == "" {
writeError(w, http.StatusBadRequest, "UPS ID required")
return
}
if err := state.DeleteUPS(id); err != nil {
if errors.Is(err, ErrNotFound) {
writeError(w, http.StatusNotFound, "UPS not found")
return
}
writeError(w, http.StatusInternalServerError, "Failed to delete UPS")
return
}
go cache.Refresh()
writeSuccess(w, nil)
}
}
func handleAPIUPSStatus(cache *UPSStatusCache) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
writeSuccess(w, cache.Get())
}
}
// Config handlers
func handleAPIExport(state *AppState) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
data, err := state.GetRawData()
if err != nil {
writeError(w, http.StatusInternalServerError, "Failed to serialize data: "+err.Error())
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Disposition", "attachment; filename=wol-nut-backup.json")
if _, err := w.Write(data); err != nil {
log.Printf("export: write failed: %v", err)
}
}
}
const maxImportSize = 1 << 20 // 1 MiB — plenty for a JSON config
func handleAPIImport(state *AppState) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
r.Body = http.MaxBytesReader(w, r.Body, maxImportSize)
file, _, err := r.FormFile("file")
if err != nil {
writeError(w, http.StatusBadRequest, "No file uploaded or file too large")
return
}
defer file.Close()
data, err := io.ReadAll(io.LimitReader(file, maxImportSize+1))
if err != nil {
writeError(w, http.StatusBadRequest, "Failed to read file")
return
}
if int64(len(data)) > maxImportSize {
writeError(w, http.StatusRequestEntityTooLarge, "File exceeds 1 MiB limit")
return
}
if err := state.ImportData(data); err != nil {
writeError(w, http.StatusBadRequest, "Invalid backup file: "+err.Error())
return
}
writeSuccess(w, map[string]string{"message": "Configuration imported successfully"})
}
}
// Health check
func handleAPIHealth() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]string{"status": "ok"})
}
}
// Version
func handleAPIVersion() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
writeSuccess(w, map[string]string{
"version": version,
"commit": commit,
})
}
}