Created
March 18, 2024 18:15
-
-
Save Schachte/4e696fdf234da44a0293ef50d44163cd to your computer and use it in GitHub Desktop.
Testing Cloudflare webhook callback server using Tunnels & Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"encoding/json" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"os" | |
"strconv" | |
) | |
var ( | |
apiToken = os.Getenv("STREAM_API_KEY") | |
accountID = os.Getenv("STREAM_ACCOUNT") | |
webhookURL = os.Getenv("TUNNEL_URL") | |
cloudflareAPI = "https://api.cloudflare.com/client/v4/accounts" | |
) | |
type VideoResponse struct { | |
UID string `json:"uid"` | |
ReadyToStream bool `json:"readyToStream"` | |
Status struct { | |
PctComplete string `json:"pctComplete"` | |
} `json:"status"` | |
} | |
type WebhookResponse struct { | |
Result struct { | |
NotificationURL string `json:"notificationUrl"` | |
Modified string `json:"modified"` | |
Secret string `json:"secret"` | |
} `json:"result"` | |
Success bool `json:"success"` | |
Errors []interface{} `json:"errors"` | |
Messages []interface{} `json:"messages"` | |
} | |
func main() { | |
url := fmt.Sprintf("%s/%s/stream/webhook", cloudflareAPI, accountID) | |
data := fmt.Sprintf(`{"notificationUrl":"%s"}`, webhookURL) | |
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer([]byte(data))) | |
if err != nil { | |
log.Fatalf("Error creating request: %v", err) | |
} | |
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", apiToken)) | |
req.Header.Set("Content-Type", "application/json") | |
client := &http.Client{} | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Fatalf("Request error: %v", err) | |
} | |
defer resp.Body.Close() | |
if resp.StatusCode != 200 { | |
log.Fatalf("Unexpected status code: %d", resp.StatusCode) | |
} | |
body, err := io.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatalf("Error reading response body: %v", err) | |
} | |
var webhookResponse WebhookResponse | |
err = json.Unmarshal(body, &webhookResponse) | |
if err != nil { | |
log.Fatalf("Error unmarshalling response: %v", err) | |
} | |
if webhookResponse.Success { | |
fmt.Printf("Webhook created successfully: %s\n", webhookResponse.Result.NotificationURL) | |
} else { | |
fmt.Println("Failed to create webhook") | |
} | |
http.HandleFunc("/", handleRequest) | |
log.Fatal(http.ListenAndServe(":9081", nil)) | |
} | |
func handleRequest(w http.ResponseWriter, r *http.Request) { | |
var videoData VideoResponse | |
err := json.NewDecoder(r.Body).Decode(&videoData) | |
if err != nil { | |
fmt.Print(err) | |
http.Error(w, "Error decoding JSON", http.StatusBadRequest) | |
return | |
} | |
defer r.Body.Close() | |
// Access the fields from the struct | |
fmt.Println("UID:", videoData.UID) | |
fmt.Println("ReadyToStream:", videoData.ReadyToStream) | |
fmt.Println("PctComplete:", videoData.Status.PctComplete) | |
w.WriteHeader(http.StatusOK) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment