Commit 76c828b4 authored by jonas's avatar jonas

Log events

parent dab814ba
Pipeline #721 passed with stage
in 21 seconds
package app
import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"
)
// EventType type
type EventType string
......@@ -24,3 +32,108 @@ var EventTypes = []EventType{
BlockedEventType,
SpamEventType,
}
// Event interface for all events
type Event interface {
Decode(r *http.Request) error
Encode() []byte
SetID(uint64)
String() string
}
// BaseEvent define an event occurred on mailjet side
type BaseEvent struct {
ID uint64 `json:"id"`
Event string `json:"event"` // the event type
Timestamp uint64 `json:"time"` // Unix timestamp of event
Email string `json:"email"` // email address of recipient triggering the event
MailjetMessageID uint64 `json:"MessageID"` // internal Mailjet message ID
MailjetCampaignID uint64 `json:"mj_campaign_id"` // internal Mailjet campaign ID associated to the message
MailjetContactID uint64 `json:"mj_contact_id"` // internal Mailjet contact ID
CustomCampaign string `json:"customcampaign"` // value of the X-Mailjet-Campaign header when provided
Payload string `json:"Payload"` // the custom ID, when provided at send time
CustomID string `json:"CustomID"` // the event payload, when provided at send time
}
// Decode mailjet event json body
func (e *BaseEvent) Decode(r *http.Request) error {
decoder := json.NewDecoder(r.Body)
return decoder.Decode(&e)
}
// Encode mailjet event json body
func (e *BaseEvent) Encode() []byte {
buf := bytes.NewBuffer(nil)
encoder := json.NewEncoder(buf)
if err := encoder.Encode(e); err != nil {
log.Fatalln(err)
}
return buf.Bytes()
}
// String convert an event to a string
func (e *BaseEvent) String() string {
return fmt.Sprintf("id=%d type=%s timestamp=%d", e.ID, e.Event, e.Timestamp)
}
// SetID set the internal event ID
func (e *BaseEvent) SetID(value uint64) {
e.ID = value
}
// SentEvent define a sent event occurred on mailjet side
type SentEvent struct {
BaseEvent
SMTPReply string `json:"smtp_reply"` // The raw SMTP response message
}
// OpenEvent define an open event occurred on mailjet side
type OpenEvent struct {
BaseEvent
IP string `json:"ip"` // IP address (can be IPv4 or IPv6) that triggered the event
Geo string `json:"geo"` // country code of IP address
UserAgent string `json:"agent"` // User-Agent
}
// ClickEvent define a click event occurred on mailjet side
type ClickEvent struct {
OpenEvent
URL string `json:"url"` // the link that was clicked
}
// UnsubEvent define a unsub event occurred on mailjet side
type UnsubEvent struct {
OpenEvent
MailjetListID string `json:"mj_list_id"` // internal Mailjet list ID associated to the contact
}
// BounceEvent define a bounce event occurred on mailjet side
type BounceEvent struct {
BaseEvent
Blocked bool `json:"blocked"` // true if this bounce leads to the recipient being blocked
HardBounce bool `json:"hard_bounce"` // true if error was permanent
// See https://dev.mailjet.com/email/guides/webhooks/#possible-values-for-errors
ErrorRelatedTo string `json:"error_related_to"`
Error string `json:"error"`
Comment string `json:"comment"` // the raw SMTP error code, including descriptions of the reason for the bounce
}
// BlockedEvent define a blocked event occurred on mailjet side
type BlockedEvent struct {
BaseEvent
// See https://dev.mailjet.com/email/guides/webhooks/#possible-values-for-errors
ErrorRelatedTo string `json:"error_related_to"`
Error string `json:"error"`
}
// SpamEvent define a spam event occurred on mailjet side
type SpamEvent struct {
BaseEvent
Source string `json:"source"` // indicates which feedback loop program reported this complaint
}
package app
import (
"bytes"
"encoding/json"
"log"
"net/http"
)
// Event interface for all events
type Event interface {
Decode(r *http.Request) error
Encode() []byte
SetID(uint64)
}
// BaseEvent define an event occurred on mailjet side
type BaseEvent struct {
ID uint64 `json:"id"`
Event string `json:"event"` // the event type
Timestamp uint64 `json:"time"` // Unix timestamp of event
Email string `json:"email"` // email address of recipient triggering the event
MailjetMessageID uint64 `json:"MessageID"` // internal Mailjet message ID
MailjetCampaignID uint64 `json:"mj_campaign_id"` // internal Mailjet campaign ID associated to the message
MailjetContactID uint64 `json:"mj_contact_id"` // internal Mailjet contact ID
CustomCampaign string `json:"customcampaign"` // value of the X-Mailjet-Campaign header when provided
Payload string `json:"Payload"` // the custom ID, when provided at send time
CustomID string `json:"CustomID"` // the event payload, when provided at send time
}
// Decode mailjet event json body
func (e *BaseEvent) Decode(r *http.Request) error {
decoder := json.NewDecoder(r.Body)
return decoder.Decode(&e)
}
// Encode mailjet event json body
func (e *BaseEvent) Encode() []byte {
buf := bytes.NewBuffer(nil)
encoder := json.NewEncoder(buf)
if err := encoder.Encode(e); err != nil {
log.Fatalln(err)
}
return buf.Bytes()
}
// SetID set the internal event ID
func (e *BaseEvent) SetID(value uint64) {
e.ID = value
}
// SentEvent define a sent event occurred on mailjet side
type SentEvent struct {
BaseEvent
SMTPReply string `json:"smtp_reply"` // The raw SMTP response message
}
// OpenEvent define an open event occurred on mailjet side
type OpenEvent struct {
BaseEvent
IP string `json:"ip"` // IP address (can be IPv4 or IPv6) that triggered the event
Geo string `json:"geo"` // country code of IP address
UserAgent string `json:"agent"` // User-Agent
}
// ClickEvent define a click event occurred on mailjet side
type ClickEvent struct {
OpenEvent
URL string `json:"url"` // the link that was clicked
}
// UnsubEvent define a unsub event occurred on mailjet side
type UnsubEvent struct {
OpenEvent
MailjetListID string `json:"mj_list_id"` // internal Mailjet list ID associated to the contact
}
// BounceEvent define a bounce event occurred on mailjet side
type BounceEvent struct {
BaseEvent
Blocked bool `json:"blocked"` // true if this bounce leads to the recipient being blocked
HardBounce bool `json:"hard_bounce"` // true if error was permanent
// See https://dev.mailjet.com/email/guides/webhooks/#possible-values-for-errors
ErrorRelatedTo string `json:"error_related_to"`
Error string `json:"error"`
Comment string `json:"comment"` // the raw SMTP error code, including descriptions of the reason for the bounce
}
// BlockedEvent define a blocked event occurred on mailjet side
type BlockedEvent struct {
BaseEvent
// See https://dev.mailjet.com/email/guides/webhooks/#possible-values-for-errors
ErrorRelatedTo string `json:"error_related_to"`
Error string `json:"error"`
}
// SpamEvent define a spam event occurred on mailjet side
type SpamEvent struct {
BaseEvent
Source string `json:"source"` // indicates which feedback loop program reported this complaint
}
......@@ -10,7 +10,7 @@ import (
func NewRouter(a *App) *mux.Router {
r := mux.NewRouter()
log.Println("creating router with base url '" + a.config.publicURL.Path + "'")
log.Println("creating router with public url '" + a.config.PublicURL + "'")
base := r.PathPrefix(a.config.publicURL.Path).Subrouter()
webhooks := base.PathPrefix("/webhooks").Subrouter()
......
......@@ -25,7 +25,7 @@ func (a *App) Serve() {
}
go func() {
log.Println("starting server at 'http://" + a.config.ListenAddr + "'")
log.Println("starting server")
if err := srv.ListenAndServe(); err != nil {
log.Println(err)
......
......@@ -49,6 +49,10 @@ func (a *App) SaveEvent(eventType EventType, event Event) error {
return err
}
go func(event Event) {
log.Println(event.String())
}(event)
return nil
})
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment