Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
76 lines (65 loc) · 1.98 KB
/
Copy pathmain.go
File metadata and controls
76 lines (65 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Example demonstrates updating a referer rule for a CacheFly service.
//
// This example shows:
// - Client initialization with API token
// - Updating referer rule configuration
// - Modifying allowed domains and file extensions
// - Error handling and response formatting
//
// Usage:
//
// export CACHEFLY_API_TOKEN="your-token"
// go run main.go <service_id> <rule_id>
//
// Example:
//
// go run main.go srv_123456789 rule_987654321
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"github.com/cachefly/cachefly-sdk-go/pkg/cachefly"
api "github.com/cachefly/cachefly-sdk-go/pkg/cachefly/api/v2_6"
"github.com/joho/godotenv"
)
func main() {
// Load .env variables (optional)
if err := godotenv.Load(); err != nil {
log.Printf("⚠️ Warning: unable to load .env file: %v", err)
}
// Ensure API token is set
token := os.Getenv("CACHEFLY_API_TOKEN")
if token == "" {
log.Fatal("❌ CACHEFLY_API_TOKEN environment variable is required")
}
// Read service ID and rule ID arguments
if len(os.Args) < 3 {
log.Fatalf("⚠️ Usage: go run main.go <service_id> <rule_id>")
}
serviceID := os.Args[1]
ruleID := os.Args[2]
// Initialize CacheFly client
client := cachefly.NewClient(cachefly.WithToken(token))
// Prepare payload for updating the referer rule
payload := api.UpdateRefererRuleRequest{
Directory: "/images",
Extension: "png",
Exceptions: []string{"trusted.example.com"},
DefaultAction: "ALLOW", // or "DENY"
}
// Update the referer rule by ID
updatedRule, err := client.ServiceOptionsRefererRules.Update(context.Background(), serviceID, ruleID, payload)
if err != nil {
log.Fatalf("❌ Failed to update referer rule %s for service %s: %v", ruleID, serviceID, err)
}
// Pretty-print the updated rule
out, err := json.MarshalIndent(updatedRule, "", " ")
if err != nil {
log.Fatalf("❌ Error formatting updated referer rule JSON: %v", err)
}
fmt.Println("\n✅ Service referer rule updated successfully:")
fmt.Println(string(out))
}