package webui import ( "encoding/json" "fmt" "net/http" "gitea.armuli.eu/museum-digital/museum-digital-webdav-uploader/src/configloader" ) type updateSettingsResponse struct { Success bool `json:"success"` ValidationError string `json:"error"` } // Provides the API for updating the general configuration. func serveApiUpdateSettings(w http.ResponseWriter, r *http.Request) { setHeadersForJson(w) // Parse POST data parseErr := r.ParseForm() if parseErr != nil { fmt.Println("Failed to load form data") return } // Load new config from a POST variable 'settings' that provides the full // settings as a JSON-encoded string. rawInputConfig := r.PostForm.Get("settings") var newConfig configloader.MDWebDavUploaderConfig unmarshalErr := json.Unmarshal([]byte(rawInputConfig), &newConfig) if unmarshalErr != nil { fmt.Fprint(w, "{\"success\": false, \"error\": \"Invalid input JSON\"}") return } // Attempt to store the new settings. Validation takes place while storing var output updateSettingsResponse validationErr := configloader.StoreConfigToFile(newConfig, "") if validationErr == nil { output = updateSettingsResponse{Success: true, ValidationError: ""} // Reload config config, setupRequired, _ = configloader.LoadFromFile("") } else { output = updateSettingsResponse{Success: false, ValidationError: validationErr.Error()} } // Format response in JSON, then print it outputJson, encodeErr := json.Marshal(output) if encodeErr != nil { panic("Failed to create JSON") } fmt.Fprint(w, string(outputJson)) }