improving logging
This commit is contained in:
@@ -23,7 +23,9 @@ func main() {
|
||||
}
|
||||
|
||||
func notifyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("Incoming %s request from %s to %s", r.Method, r.RemoteAddr, r.URL.Path)
|
||||
if r.Method != http.MethodPost {
|
||||
log.Printf("Method not allowed: %s", r.Method)
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
w.Write([]byte("Method not allowed"))
|
||||
return
|
||||
@@ -31,18 +33,22 @@ func notifyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
var notif Notification
|
||||
if err := json.NewDecoder(r.Body).Decode(¬if); err != nil {
|
||||
log.Printf("Invalid payload: %v", err)
|
||||
w.WriteHeader(http.StatusBadRequest)
|
||||
w.Write([]byte("Invalid payload"))
|
||||
return
|
||||
}
|
||||
log.Printf("Received notification payload: Title='%s', Message='%s'", notif.Title, notif.Message)
|
||||
|
||||
// Call Discord notification function
|
||||
if err := sendDiscordNotification(notif.Title, notif.Message); err != nil {
|
||||
log.Printf("Failed to send Discord notification: %v", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("Failed to send Discord notification"))
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Notification sent successfully for Title='%s'", notif.Title)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte("Notification sent"))
|
||||
}
|
||||
@@ -50,6 +56,7 @@ func notifyHandler(w http.ResponseWriter, r *http.Request) {
|
||||
func sendDiscordNotification(title, message string) error {
|
||||
webhookURL := os.Getenv("WEBHOOK_URL")
|
||||
if webhookURL == "" {
|
||||
log.Printf("WEBHOOK_URL environment variable not set")
|
||||
return fmt.Errorf("WEBHOOK_URL environment variable not set")
|
||||
}
|
||||
|
||||
@@ -63,18 +70,23 @@ func sendDiscordNotification(title, message string) error {
|
||||
|
||||
jsonData, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
log.Printf("Failed to marshal Discord payload: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
log.Printf("Sending Discord notification: Title='%s', Message='%s'", title, message)
|
||||
resp, err := http.Post(webhookURL, "application/json", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
log.Printf("Error posting to Discord webhook: %v", err)
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
log.Printf("Discord webhook returned status: %s", resp.Status)
|
||||
return fmt.Errorf("Discord webhook returned status: %s", resp.Status)
|
||||
}
|
||||
|
||||
log.Printf("Discord notification sent successfully: Title='%s'", title)
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user