package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
)
type WebhookPayload struct {
Event string `json:"event"`
Data struct {
JobID string `json:"job_id"`
BatchID string `json:"batch_id"`
Error string `json:"error,omitempty"`
} `json:"data"`
}
func handleWebhook(w http.ResponseWriter, r *http.Request) {
var payload WebhookPayload
if err := json.NewDecoder(r.Body).Decode(&payload); err != nil {
http.Error(w, "Bad request", http.StatusBadRequest)
return
}
switch payload.Event {
case "CREATED":
fmt.Printf("Job %s created\n", payload.Data.JobID)
case "STARTED":
fmt.Printf("Job %s started processing\n", payload.Data.JobID)
case "SUCCESS":
fmt.Printf("Job %s completed successfully\n", payload.Data.JobID)
// Make a request to the download API with the job_id
case "ERROR":
fmt.Printf("Job %s failed: %s\n", payload.Data.JobID, payload.Data.Error)
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("OK"))
}
func main() {
http.HandleFunc("/webhooks/ytrss", handleWebhook)
log.Println("Webhook server running on port 3000")
log.Fatal(http.ListenAndServe(":3000", nil))
}