Files
Gilles Soulier 262413e2e3 feat(server): handlers REST + transport MQTT
Ajout des handlers HTTP (agents, métriques historique, config agent/serveur, icônes upload/get) et du client MQTT serveur avec subscribe automatique et PushConfig.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-05-22 12:10:34 +02:00

46 lines
945 B
Go

package handlers
import (
"encoding/json"
"net/http"
"strconv"
"strings"
"time"
"github.com/user/nanometrics/server/db"
)
func MetricsHistoryHandler(database *db.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
parts := strings.Split(strings.Trim(r.URL.Path, "/"), "/")
if len(parts) < 4 {
http.Error(w, "invalid path", 400)
return
}
agentID := parts[2]
now := time.Now().Unix()
from := now - 3600
to := now
if v := r.URL.Query().Get("from"); v != "" {
if n, err := strconv.ParseInt(v, 10, 64); err == nil {
from = n
}
}
if v := r.URL.Query().Get("to"); v != "" {
if n, err := strconv.ParseInt(v, 10, 64); err == nil {
to = n
}
}
history, err := database.GetMetricsHistory(agentID, from, to)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(history)
}
}