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"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
|
@ -36,12 +37,14 @@ func HandleComputerWebSocket(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
slog.Info("WebSocket connection established")
|
||||
service.ComputerData.Online = true
|
||||
service.ComputerData.UptimeStart = int(time.Now().Unix())
|
||||
|
||||
for {
|
||||
_, message, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
slog.Error("WebSocket connection closed by client", slog.Any("error", err))
|
||||
service.ComputerData.Online = false
|
||||
service.ComputerData.UptimeStart = 0
|
||||
break
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ type ComputerWebSocketMessage struct {
|
|||
|
||||
type ComputerData struct {
|
||||
Online bool `json:"online"`
|
||||
UptimeStart int `json:"uptimeStart"`
|
||||
Totals ComputerTotals `json:"totals"`
|
||||
Graph []ComputerGraphData `json:"graph"`
|
||||
}
|
||||
|
|
|
|||
|
|
@ -52,12 +52,17 @@ func NewRouter() {
|
|||
// Prometheus
|
||||
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) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(map[string]string{
|
||||
"name": "trafficlunar's 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) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue