initial
This commit is contained in:
33
webhook_handler.go
Normal file
33
webhook_handler.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package simplelog
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type WebhookHandler struct {
|
||||
webhookURL string
|
||||
}
|
||||
|
||||
func NewWebhookHandler(webhookURL string) (*WebhookHandler, error) {
|
||||
if _, err := url.ParseRequestURI(webhookURL); err != nil {
|
||||
return nil, fmt.Errorf("invalid webhook URL: %v", err)
|
||||
}
|
||||
return &WebhookHandler{webhookURL: webhookURL}, nil
|
||||
}
|
||||
|
||||
func (w *WebhookHandler) Log(event Event) error {
|
||||
jsonData, err := json.Marshal(event)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error marshaling event: %v", err)
|
||||
}
|
||||
response, err := http.Post(w.webhookURL, "application/json", bytes.NewBuffer(jsonData))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer response.Body.Close()
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user