Repository navigation
Expand file tree
/
Copy pathdevices.go
More file actions
79 lines (67 loc) · 2.19 KB
/
Copy pathdevices.go
File metadata and controls
79 lines (67 loc) · 2.19 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
package api
import (
"encoding/json"
"net/http"
"time"
"github.com/RealityLink-Tech/MoonHub/pkg/devices"
)
// PublicPairedDevice is the public representation of a paired device,
// with sensitive fields (Token, TokenExpiresAt) stripped.
type PublicPairedDevice struct {
ID string `json:"id"`
Name string `json:"name"`
IPAddress string `json:"ipAddress,omitempty"`
UserAgent string `json:"userAgent,omitempty"`
PairedAt time.Time `json:"pairedAt"`
LastSeenAt time.Time `json:"lastSeenAt"`
}
// toPublic maps a PairedDevice to its public-safe representation.
func toPublicPairedDevice(p *devices.PairedDevice) PublicPairedDevice {
return PublicPairedDevice{
ID: p.ID,
Name: p.Name,
IPAddress: p.IPAddress,
UserAgent: p.UserAgent,
PairedAt: p.PairedAt,
LastSeenAt: p.LastSeenAt,
}
}
// deviceStoreInterface defines the interface for device store operations.
// This allows for mocking in tests.
type deviceStoreInterface interface {
ListDevices() []*devices.PairedDevice
}
// DevicesHandler handles paired devices API requests.
type DevicesHandler struct {
deviceStore deviceStoreInterface
}
// NewDevicesHandler creates a new devices API handler.
func NewDevicesHandler(store *devices.DeviceStore) *DevicesHandler {
return &DevicesHandler{
deviceStore: store,
}
}
// RegisterRoutes registers devices routes with the mux.
func (h *DevicesHandler) RegisterRoutes(mux *http.ServeMux) {
mux.HandleFunc("GET /api/devices", h.handleListDevices)
}
// handleListDevices handles GET /api/devices - returns all paired devices.
func (h *DevicesHandler) handleListDevices(w http.ResponseWriter, r *http.Request) {
// Enforce LAN-only restriction
if !requireLANClient(w, r) {
return
}
// Get all paired devices from store and strip sensitive fields
storedDevices := h.deviceStore.ListDevices()
publicDevices := make([]PublicPairedDevice, len(storedDevices))
for i, d := range storedDevices {
publicDevices[i] = toPublicPairedDevice(d)
}
// Return successful response with device list
response := map[string]interface{}{
"success": true,
"data": publicDevices,
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}