|
| 1 | +package handlers |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "net/http" |
| 8 | + "os" |
| 9 | + |
| 10 | + yaml "gopkg.in/yaml.v3" |
| 11 | +) |
| 12 | + |
| 13 | +// ConfigHandler reads and serves the config.yaml file as JSON |
| 14 | +func ConfigHandler(configPath string) http.HandlerFunc { |
| 15 | + return func(w http.ResponseWriter, r *http.Request) { |
| 16 | + // Only allow GET requests |
| 17 | + if r.Method != http.MethodGet { |
| 18 | + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 19 | + return |
| 20 | + } |
| 21 | + |
| 22 | + // Read the config file |
| 23 | + data, err := os.ReadFile(configPath) |
| 24 | + if err != nil { |
| 25 | + log.Printf("Error reading config file: %v", err) |
| 26 | + http.Error(w, fmt.Sprintf("Failed to read config file: %v", err), http.StatusInternalServerError) |
| 27 | + return |
| 28 | + } |
| 29 | + |
| 30 | + // Parse YAML |
| 31 | + var config interface{} |
| 32 | + if err := yaml.Unmarshal(data, &config); err != nil { |
| 33 | + log.Printf("Error parsing config YAML: %v", err) |
| 34 | + http.Error(w, fmt.Sprintf("Failed to parse config: %v", err), http.StatusInternalServerError) |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + // Convert to JSON and send response |
| 39 | + w.Header().Set("Content-Type", "application/json") |
| 40 | + if err := json.NewEncoder(w).Encode(config); err != nil { |
| 41 | + log.Printf("Error encoding config to JSON: %v", err) |
| 42 | + http.Error(w, fmt.Sprintf("Failed to encode config: %v", err), http.StatusInternalServerError) |
| 43 | + return |
| 44 | + } |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +// UpdateConfigHandler updates the config.yaml file |
| 49 | +func UpdateConfigHandler(configPath string) http.HandlerFunc { |
| 50 | + return func(w http.ResponseWriter, r *http.Request) { |
| 51 | + // Only allow POST/PUT requests |
| 52 | + if r.Method != http.MethodPost && r.Method != http.MethodPut { |
| 53 | + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
| 54 | + return |
| 55 | + } |
| 56 | + |
| 57 | + // Read the request body |
| 58 | + var configData map[string]interface{} |
| 59 | + if err := json.NewDecoder(r.Body).Decode(&configData); err != nil { |
| 60 | + log.Printf("Error decoding request body: %v", err) |
| 61 | + http.Error(w, fmt.Sprintf("Invalid request body: %v", err), http.StatusBadRequest) |
| 62 | + return |
| 63 | + } |
| 64 | + |
| 65 | + // Convert to YAML |
| 66 | + yamlData, err := yaml.Marshal(configData) |
| 67 | + if err != nil { |
| 68 | + log.Printf("Error marshaling config to YAML: %v", err) |
| 69 | + http.Error(w, fmt.Sprintf("Failed to convert config to YAML: %v", err), http.StatusInternalServerError) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + // Write to file |
| 74 | + if err := os.WriteFile(configPath, yamlData, 0644); err != nil { |
| 75 | + log.Printf("Error writing config file: %v", err) |
| 76 | + http.Error(w, fmt.Sprintf("Failed to write config file: %v", err), http.StatusInternalServerError) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + log.Printf("Configuration updated successfully") |
| 81 | + w.Header().Set("Content-Type", "application/json") |
| 82 | + w.WriteHeader(http.StatusOK) |
| 83 | + json.NewEncoder(w).Encode(map[string]string{"status": "success", "message": "Configuration updated successfully"}) |
| 84 | + } |
| 85 | +} |
0 commit comments