Commit 0d0a8da5 authored by Pasan Mallawaarachchi's avatar Pasan Mallawaarachchi 💻

read logs & status

parent d5461c49
......@@ -9,6 +9,7 @@ curl:
delete_outbox_mail: http://root:root@couchdb_emanger:5984/outbox/
add_sent_mail: http://root:root@couchdb_emanger:5984/sentitems/
add_mail_log: http://root:root@couchdb_emanger:5984/mail_logs/
read_log: http://root:root@couchdb_emanger:5984/read_logs/
protocols:
method: http://
requests:
......
......@@ -41,6 +41,7 @@ type Config struct {
DeleteOutboxMail string `yaml:"delete_outbox_mail"`
AddSentMail string `yaml:"add_sent_mail"`
AddMailLog string `yaml:"add_mail_log"`
ReadLog string `yaml:"read_log"`
} `yaml:"curl"`
Protocols struct {
Request string `yaml:"method"`
......@@ -163,12 +164,12 @@ func main() {
if respErr != nil {
alert := " Error in getting email from the OUTBOX DB \n" + respErr.Error()
alertAdmin(alert)
fmt.Printf("Error : %s", reqErr.Error())
//fmt.Printf("Error : %s", respErr.Error())
//panic(respErr)
}
defer resp.Body.Close()
respbody, _ := ioutil.ReadAll(resp.Body)
//fmt.Println(string(respbody))
fmt.Printf(" %s | ", resp.Status)
/**
* struct the response of the of the cURL request
......@@ -181,6 +182,17 @@ func main() {
alertAdmin(alert)
}
if respErr != nil {
alert := " Error in getting email from the OUTBOX DB \n" + respErr.Error()
alertAdmin(alert)
fmt.Printf("Error : %s", reqErr.Error())
logOutbox(resp.Status, reqErr.Error(), newDoc.Rows[0].ID)
//panic(respErr)
}
defer resp.Body.Close()
//fmt.Printf("Status - %d", resp.StatusCode)
// logOutbox()
/**
* if: the "OUTBOX" DB has new records, it returns the number of Rows
* so there are new documents the value is always greaterthan 0
......@@ -400,7 +412,7 @@ func worker(i int) {
if err != nil {
// log.Fatal(err)
fmt.Printf("MailgunResp err : %s", err.Error())
alertAdmin(" MAilgun Error \n" + err.Error())
alertAdmin(" Mailgun Error \n" + err.Error())
}
if len(filename) > 0 {
os.Remove(filename)
......@@ -457,8 +469,8 @@ func sendMessage(mg mailgun.Mailgun, sender, subject, body, recipient, cc string
resp, id, err := mg.Send(ctx, message)
if err != nil {
//log.Fatal(err)
fmt.Printf("MAilgun Error : %s", err.Error())
alertAdmin(" MAilgun Error \n" + err.Error())
fmt.Printf("Mailgun Error : %s", err.Error())
alertAdmin(" Mailgun Error \n" + err.Error())
}
//fmt.Println(resp)
// get configurations
......@@ -585,3 +597,35 @@ func sentmail(subject, sender, recipient, cc, bcc, attachment, body, mailID stri
defer logResp.Body.Close()
ioutil.ReadAll(logResp.Body)
}
func logOutbox(status, error, id string) {
// get configurations
var config Config
source, sourceErr := ioutil.ReadFile("config.yaml")
if sourceErr != nil {
fmt.Printf("Logmail sourceErr : %s", sourceErr.Error())
// panic(sourceErr)
}
configErr := yaml.Unmarshal(source, &config)
if configErr != nil {
fmt.Printf("Logmail configErr : %s", configErr.Error())
// panic(configErr)
}
sentBody := strings.NewReader(`{"status":"` + status +
`","error":"` + error +
`","id":"` + id + `"}`)
logReq, logBodyerr := http.NewRequest(config.Requests.Put, config.Curl.ReadLog+id, sentBody)
if logBodyerr != nil {
alert := " Error requesting log mail \n" + logBodyerr.Error()
alertAdmin(alert)
}
logReq.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson)
logResp, respErr := http.DefaultClient.Do(logReq)
if respErr != nil {
alert := " Error adding data to LOGS DB \n" + respErr.Error()
alertAdmin(alert)
}
defer logResp.Body.Close()
ioutil.ReadAll(logResp.Body)
}
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