feat: computer data uptime
also fix last commit
This commit is contained in:
parent
02740548de
commit
84dfe05de8
3 changed files with 13 additions and 4 deletions
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
)
|
)
|
||||||
|
|
@ -36,12 +37,14 @@ func HandleComputerWebSocket(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
slog.Info("WebSocket connection established")
|
slog.Info("WebSocket connection established")
|
||||||
service.ComputerData.Online = true
|
service.ComputerData.Online = true
|
||||||
|
service.ComputerData.UptimeStart = int(time.Now().Unix())
|
||||||
|
|
||||||
for {
|
for {
|
||||||
_, message, err := conn.ReadMessage()
|
_, message, err := conn.ReadMessage()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
slog.Error("WebSocket connection closed by client", slog.Any("error", err))
|
slog.Error("WebSocket connection closed by client", slog.Any("error", err))
|
||||||
service.ComputerData.Online = false
|
service.ComputerData.Online = false
|
||||||
|
service.ComputerData.UptimeStart = 0
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,7 @@ type ComputerWebSocketMessage struct {
|
||||||
|
|
||||||
type ComputerData struct {
|
type ComputerData struct {
|
||||||
Online bool `json:"online"`
|
Online bool `json:"online"`
|
||||||
|
UptimeStart int `json:"uptimeStart"`
|
||||||
Totals ComputerTotals `json:"totals"`
|
Totals ComputerTotals `json:"totals"`
|
||||||
Graph []ComputerGraphData `json:"graph"`
|
Graph []ComputerGraphData `json:"graph"`
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,12 +52,17 @@ func NewRouter() {
|
||||||
// Prometheus
|
// Prometheus
|
||||||
r.Handle("/metrics", promhttp.Handler())
|
r.Handle("/metrics", promhttp.Handler())
|
||||||
|
|
||||||
|
var commit = "unknown"
|
||||||
|
if os.Getenv("SOURCE_COMMIT") != "" {
|
||||||
|
commit = os.Getenv("SOURCE_COMMIT")[:7] // shorten to 7 characters
|
||||||
|
}
|
||||||
|
|
||||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(map[string]string{
|
json.NewEncoder(w).Encode(map[string]string{
|
||||||
"name": "trafficlunar's api",
|
"name": "trafficlunar's api",
|
||||||
"url": "https://github.com/trafficlunar/api",
|
"url": "https://github.com/trafficlunar/api",
|
||||||
"commit": os.Getenv("SOURCE_COMMIT")[:7], // shorten to 7 characters
|
"commit": commit,
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
r.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
r.Get("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue