Files
local-mcp/go-server/internal/api/status.go
2026-03-27 18:32:25 +08:00

78 lines
1.9 KiB
Go

package api
import (
"net/http"
"time"
"github.com/local-mcp/local-mcp-go/internal/config"
"github.com/local-mcp/local-mcp-go/internal/models"
)
func handleHealth() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
writeJSON(w, http.StatusOK, map[string]any{
"status": "ok",
"server_time": time.Now().UTC().Format(time.RFC3339Nano),
})
}
}
func handleStatus(stores Stores) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
counts, err := stores.Instructions.Counts()
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
cfg, err := stores.Settings.Get()
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
latest, err := stores.Agents.Latest()
if err != nil {
writeError(w, http.StatusInternalServerError, err.Error())
return
}
agent := map[string]any{
"connected": false,
"last_seen_at": nil,
"last_fetch_at": nil,
"agent_id": nil,
}
if latest != nil {
connected := time.Since(latest.LastSeenAt).Seconds() <= float64(config.AgentStaleAfterSeconds)
agent = map[string]any{
"connected": connected,
"last_seen_at": latest.LastSeenAt.Format(time.RFC3339Nano),
"last_fetch_at": latest.LastFetchAt.Format(time.RFC3339Nano),
"agent_id": latest.AgentID,
}
}
resp := map[string]any{
"server": map[string]any{
"status": "up",
"started_at": serverStartTime.Format(time.RFC3339Nano),
"type": "go",
"version": config.AppVersion,
},
"agent": agent,
"queue": map[string]any{
"pending_count": counts.PendingCount,
"consumed_count": counts.ConsumedCount,
},
"settings": models.Settings{
DefaultWaitSeconds: cfg.DefaultWaitSeconds,
DefaultEmptyResponse: cfg.DefaultEmptyResponse,
},
}
writeJSON(w, http.StatusOK, resp)
}
}