58 lines
1.5 KiB
Go
58 lines
1.5 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"github.com/local-mcp/local-mcp-go/internal/events"
|
|
)
|
|
|
|
func handleGetConfig(stores Stores) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
cfg, err := stores.Settings.Get()
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, cfg)
|
|
}
|
|
}
|
|
|
|
func handleUpdateConfig(stores Stores, broker *events.Broker) http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
// Decode partial patch
|
|
var patch struct {
|
|
DefaultWaitSeconds *int `json:"default_wait_seconds"`
|
|
AgentStaleAfterSeconds *int `json:"agent_stale_after_seconds"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&patch); err != nil {
|
|
writeError(w, http.StatusBadRequest, "Invalid JSON")
|
|
return
|
|
}
|
|
|
|
// Get current settings
|
|
current, err := stores.Settings.Get()
|
|
if err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
// Apply patches
|
|
if patch.DefaultWaitSeconds != nil {
|
|
current.DefaultWaitSeconds = *patch.DefaultWaitSeconds
|
|
}
|
|
if patch.AgentStaleAfterSeconds != nil {
|
|
current.AgentStaleAfterSeconds = *patch.AgentStaleAfterSeconds
|
|
}
|
|
|
|
if err := stores.Settings.Update(current); err != nil {
|
|
writeError(w, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
broker.Broadcast("config.updated", map[string]any{"config": current})
|
|
writeJSON(w, http.StatusOK, current)
|
|
}
|
|
}
|
|
|