-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail_send.go
119 lines (109 loc) · 2.93 KB
/
mail_send.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package vcago
import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
)
type MailSend struct {
URL string
Key string
Mode string
}
func NewMailSend() *MailSend {
return &MailSend{
URL: Settings.String("MAIL_URL", "w", "http://factory-api.localhost"),
Key: Settings.String("MAIL_API_KEY", "w", "secret"),
Mode: Settings.String("MAIL_MODE", "w", "local"),
}
}
func (i *MailSend) Send(mail *MailData) {
if i.Mode == "local" {
i.Print(mail)
} else if i.Mode == "nats" {
i.Nats(mail)
} else if i.Mode == "post" {
go i.Post(mail)
}
}
func (i *MailSend) Nats(mail *MailData) {
Nats.Publish("mail.send", mail)
}
func (i *MailSend) Print(mail *MailData) {
output, _ := json.MarshalIndent(mail, "", "\t")
log.Print(string(output))
}
func (i *MailSend) Subscribe() {
Nats.Subscribe("mail.send", func(m *MailData) { i.Post(m) })
}
func (i *MailSend) Post(mailData *MailData) {
var jsonData []byte
var err error
if jsonData, err = json.Marshal(mailData); err != nil {
log.Print(err)
return
}
request := new(http.Request)
request, err = http.NewRequest("POST", i.URL+"/mails/send", bytes.NewBuffer(jsonData))
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
request.Header.Set("Authorization", "Bearer "+i.Key)
client := &http.Client{}
response := new(http.Response)
response, err = client.Do(request)
if err != nil {
log.Print(NewIDjangoError(err, response.StatusCode, nil))
return
}
defer response.Body.Close()
if response.StatusCode != 200 {
var bodyBytes []byte
if bodyBytes, err = ioutil.ReadAll(response.Body); err != nil {
log.Print(NewIDjangoError(err, response.StatusCode, nil))
return
}
body := new(interface{})
if err = json.Unmarshal(bodyBytes, body); err != nil {
log.Print(NewIDjangoError(err, 500, string(bodyBytes)))
return
}
log.Print(NewIDjangoError(nil, response.StatusCode, body))
return
}
return
}
func (i *MailSend) PostCycularMail(data *CycularMail) {
var jsonData []byte
var err error
if jsonData, err = json.Marshal(data); err != nil {
log.Print(err)
return
}
request := new(http.Request)
request, err = http.NewRequest("POST", i.URL+"/mails/send/cycle", bytes.NewBuffer(jsonData))
request.Header.Set("Content-Type", "application/json; charset=UTF-8")
request.Header.Set("Authorization", "Bearer "+i.Key)
client := &http.Client{}
response := new(http.Response)
response, err = client.Do(request)
if err != nil {
log.Print(NewIDjangoError(err, response.StatusCode, nil))
return
}
defer response.Body.Close()
if response.StatusCode != 200 {
var bodyBytes []byte
if bodyBytes, err = ioutil.ReadAll(response.Body); err != nil {
log.Print(NewIDjangoError(err, response.StatusCode, nil))
return
}
body := new(interface{})
if err = json.Unmarshal(bodyBytes, body); err != nil {
log.Print(NewIDjangoError(err, 500, string(bodyBytes)))
return
}
log.Print(NewIDjangoError(nil, response.StatusCode, body))
return
}
return
}