package app import ( "encoding/json" "fmt" "io" "net/http" "os" "strings" "time" ) // --------------------------------------------------------------------- // rollback / clear // --------------------------------------------------------------------- func handleRoutesClear(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } res := routesClear() writeJSON(w, http.StatusOK, res) } func handleRoutesCacheRestore(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } res := restoreRoutesFromCache() writeJSON(w, http.StatusOK, res) } func handleRoutesPrecheckDebug(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { http.Error(w, "method not allowed", http.StatusMethodNotAllowed) return } var body struct { RunNow bool `json:"run_now"` } runNow := true if r.Body != nil { defer r.Body.Close() if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err == nil { runNow = body.RunNow } } now := time.Now().UTC().Format(time.RFC3339) content := []byte("forced_at=" + now + "\n") if err := os.WriteFile(precheckForcePath, content, 0o644); err != nil { writeJSON(w, http.StatusOK, cmdResult{ OK: false, Message: fmt.Sprintf("precheck debug arm failed: %v", err), }) return } appendTraceLineTo(traceLogPath, "routes", fmt.Sprintf("debug precheck armed: %s", precheckForcePath)) if !runNow { writeJSON(w, http.StatusOK, cmdResult{ OK: true, Message: "precheck debug armed (run_now=false)", }) return } // Run restart asynchronously: this endpoint is debug/helper and should return // immediately, otherwise GUI client can hit read-timeout while systemctl works. go func() { restartRes := runRoutesServiceAction("restart") if restartRes.OK { appendTraceLineTo(traceLogPath, "routes", "debug precheck: routes restart completed") return } appendTraceLineTo(traceLogPath, "routes", "debug precheck: routes restart failed: "+strings.TrimSpace(restartRes.Message)) }() writeJSON(w, http.StatusOK, cmdResult{ OK: true, Message: "precheck debug armed + async routes restart requested", }) }