package cloudflare import ( "encoding/json" "fmt" "io" "log" "net/http" "strings" "git.hatecomputers.club/hatecomputers/hatecomputers.club/database" ) type CloudflareDNSResponse struct { Result database.DNSRecord `json:"result"` } func CreateDNSRecord(zoneId string, apiToken string, record *database.DNSRecord) (string, error) { url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records", zoneId) reqBody := fmt.Sprintf(`{"type":"%s","name":"%s","content":"%s","ttl":%d,"proxied":false}`, record.Type, record.Name, record.Content, record.TTL) log.Println(reqBody) payload := strings.NewReader(reqBody) req, _ := http.NewRequest("POST", url, payload) req.Header.Add("Authorization", "Bearer "+apiToken) req.Header.Add("Content-Type", "application/json") res, err := http.DefaultClient.Do(req) if err != nil { return "", err } defer res.Body.Close() body, _ := io.ReadAll(res.Body) if res.StatusCode != 200 { return "", fmt.Errorf("error creating dns record: %s", body) } var response CloudflareDNSResponse err = json.Unmarshal(body, &response) if err != nil { return "", err } result := &response.Result return result.ID, nil } func DeleteDNSRecord(zoneId string, apiToken string, id string) error { url := fmt.Sprintf("https://api.cloudflare.com/client/v4/zones/%s/dns_records/%s", zoneId, id) req, _ := http.NewRequest("DELETE", url, nil) req.Header.Add("Authorization", "Bearer "+apiToken) res, err := http.DefaultClient.Do(req) if err != nil { return err } defer res.Body.Close() body, _ := io.ReadAll(res.Body) if res.StatusCode != 200 { return fmt.Errorf("error deleting dns record: %s", body) } return nil }