Commit 0953dc47 authored by Pasan Mallawaarachchi's avatar Pasan Mallawaarachchi 💻

outbox read log

parent 86ca1c10
...@@ -9,7 +9,7 @@ curl: ...@@ -9,7 +9,7 @@ curl:
delete_outbox_mail: http://root:root@couchdb_emanger:5984/outbox/ delete_outbox_mail: http://root:root@couchdb_emanger:5984/outbox/
add_sent_mail: http://root:root@couchdb_emanger:5984/sentitems/ add_sent_mail: http://root:root@couchdb_emanger:5984/sentitems/
add_mail_log: http://root:root@couchdb_emanger:5984/mail_logs/ add_mail_log: http://root:root@couchdb_emanger:5984/mail_logs/
read_log: http://root:root@couchdb_emanger:5984/read_logs/ read_logs: http://root:root@couchdb_emanger:5984/read_logs
protocols: protocols:
method: http:// method: http://
requests: requests:
......
...@@ -41,7 +41,7 @@ type Config struct { ...@@ -41,7 +41,7 @@ type Config struct {
DeleteOutboxMail string `yaml:"delete_outbox_mail"` DeleteOutboxMail string `yaml:"delete_outbox_mail"`
AddSentMail string `yaml:"add_sent_mail"` AddSentMail string `yaml:"add_sent_mail"`
AddMailLog string `yaml:"add_mail_log"` AddMailLog string `yaml:"add_mail_log"`
ReadLog string `yaml:"read_log"` ReadLog string `yaml:"read_logs"`
} `yaml:"curl"` } `yaml:"curl"`
Protocols struct { Protocols struct {
Request string `yaml:"method"` Request string `yaml:"method"`
...@@ -107,6 +107,7 @@ var mailData chan string ...@@ -107,6 +107,7 @@ var mailData chan string
var outboxReadTime chan string var outboxReadTime chan string
var outboxDeleteTime chan string var outboxDeleteTime chan string
var filename = "" var filename = ""
var loc, _ = time.LoadLocation("Asia/Colombo")
func main() { func main() {
// get configurations // get configurations
...@@ -182,9 +183,10 @@ func main() { ...@@ -182,9 +183,10 @@ func main() {
alertAdmin(alert) alertAdmin(alert)
} }
fmt.Println(string(respbody)) // fmt.Println(string(respbody))
fmt.Printf(" %s, %d | ", resp.Status, newDoc.TotalRows) fmt.Printf(" %s, %s | ", resp.Status, strconv.Itoa(newDoc.TotalRows))
logOutbox(resp.Status, strconv.Itoa(newDoc.TotalRows), time.Now().In(loc).String() /*string(respbody)*/)
//fmt.Printf("%s, %s, %s", resp.Status, strconv.Itoa(newDoc.TotalRows), string(respbody))
/** /**
* if: the "OUTBOX" DB has new records, it returns the number of Rows * if: the "OUTBOX" DB has new records, it returns the number of Rows
* so there are new documents the value is always greaterthan 0 * so there are new documents the value is always greaterthan 0
...@@ -205,9 +207,9 @@ func main() { ...@@ -205,9 +207,9 @@ func main() {
* Now we get the documents' email data from the "OUTBOX" DB * Now we get the documents' email data from the "OUTBOX" DB
* cURL reuest to get the document * cURL reuest to get the document
*/ */
req, reqErr := http.NewRequest(config.Requests.Get, config.Curl.GetOutboxMail+newMailID, nil) reqNew, reqErr := http.NewRequest(config.Requests.Get, config.Curl.GetOutboxMail+newMailID, nil)
req.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson) reqNew.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson)
resp, respErr := http.DefaultClient.Do(req) respNew, respErr := http.DefaultClient.Do(reqNew)
if reqErr != nil { if reqErr != nil {
alert := " Error requesting the OUTBOX mail data \n" + reqErr.Error() alert := " Error requesting the OUTBOX mail data \n" + reqErr.Error()
alertAdmin(alert) alertAdmin(alert)
...@@ -216,10 +218,10 @@ func main() { ...@@ -216,10 +218,10 @@ func main() {
alert := " Error getting the OUTBOX mail data \n" + respErr.Error() alert := " Error getting the OUTBOX mail data \n" + respErr.Error()
alertAdmin(alert) alertAdmin(alert)
} }
defer resp.Body.Close() defer respNew.Body.Close()
respbody, _ := ioutil.ReadAll(resp.Body) respbodyNew, _ := ioutil.ReadAll(respNew.Body)
newMail := string(respbody) newMail := string(respbodyNew)
readTimeOutbox := time.Now().String() readTimeOutbox := time.Now().In(loc).String()
// mailReadTime := mailReadTime.String() // mailReadTime := mailReadTime.String()
/** /**
...@@ -256,7 +258,7 @@ func main() { ...@@ -256,7 +258,7 @@ func main() {
} }
defer delResp.Body.Close() defer delResp.Body.Close()
ioutil.ReadAll(delResp.Body) ioutil.ReadAll(delResp.Body)
deleteTimeOutbox := time.Now().String() deleteTimeOutbox := time.Now().In(loc).String()
outboxDeleteTime <- (deleteTimeOutbox) outboxDeleteTime <- (deleteTimeOutbox)
} else { } else {
...@@ -266,7 +268,6 @@ func main() { ...@@ -266,7 +268,6 @@ func main() {
outboxDeleteTime <- "" outboxDeleteTime <- ""
} }
} }
} }
...@@ -414,7 +415,7 @@ func worker(i int) { ...@@ -414,7 +415,7 @@ func worker(i int) {
fmt.Printf("Email sent ID: %s Resp: %s\n", id, MailgunResp) fmt.Printf("Email sent ID: %s Resp: %s\n", id, MailgunResp)
} }
//sendMessage(mg, sender, subject, body, recipient, cc) //sendMessage(mg, sender, subject, body, recipient, cc)
mailSentTimeMailgun := time.Now().String() mailSentTimeMailgun := time.Now().In(loc).String()
/** /**
* add the email as new document in "SENT ITEMS" DB * add the email as new document in "SENT ITEMS" DB
*/ */
...@@ -434,7 +435,7 @@ func worker(i int) { ...@@ -434,7 +435,7 @@ func worker(i int) {
defer resp.Body.Close() defer resp.Body.Close()
ioutil.ReadAll(resp.Body) ioutil.ReadAll(resp.Body)
*/ */
mailAddedSentitems := time.Now().String() mailAddedSentitems := time.Now().In(loc).String()
sentmail(subject, sender, recipient, cc, bcc, attachment, body, mail.ID) sentmail(subject, sender, recipient, cc, bcc, attachment, body, mail.ID)
logmail(subject, sender, recipient, cc, bcc, attachment, readTimeOutbox, mailSentTimeMailgun, id, MailgunResp, mailAddedSentitems, deleteTimeOutbox, mail.ID) logmail(subject, sender, recipient, cc, bcc, attachment, readTimeOutbox, mailSentTimeMailgun, id, MailgunResp, mailAddedSentitems, deleteTimeOutbox, mail.ID)
...@@ -590,34 +591,36 @@ func sentmail(subject, sender, recipient, cc, bcc, attachment, body, mailID stri ...@@ -590,34 +591,36 @@ func sentmail(subject, sender, recipient, cc, bcc, attachment, body, mailID stri
ioutil.ReadAll(logResp.Body) ioutil.ReadAll(logResp.Body)
} }
func logOutbox(status, error, id string) { func logOutbox(status, totalRows, time /*respBody*/ string) {
// get configurations // get configurations
var config Config var config Config
source, sourceErr := ioutil.ReadFile("config.yaml") source, sourceErr := ioutil.ReadFile("config.yaml")
if sourceErr != nil { if sourceErr != nil {
fmt.Printf("Logmail sourceErr : %s", sourceErr.Error()) fmt.Printf("Sentmail sourceErr : %s", sourceErr.Error())
// panic(sourceErr) // panic(sourceErr)
} }
configErr := yaml.Unmarshal(source, &config) configErr := yaml.Unmarshal(source, &config)
if configErr != nil { if configErr != nil {
fmt.Printf("Logmail configErr : %s", configErr.Error()) fmt.Printf("Sentmail configErr : %s", configErr.Error())
// panic(configErr) // panic(configErr)
} }
sentBody := strings.NewReader(`{"status":"` + status + sentBody := strings.NewReader(`{"status":"` + status +
`","error":"` + error + `","row_count":"` + totalRows +
`","id":"` + id + `"}`) `","date_time":"` + time + `"}`)
logReq, logBodyerr := http.NewRequest(config.Requests.Put, config.Curl.ReadLog+id, sentBody) logReq, logBodyerr := http.NewRequest(config.Requests.Post, config.Curl.ReadLog, sentBody)
if logBodyerr != nil { if logBodyerr != nil {
alert := " Error requesting log mail \n" + logBodyerr.Error() alert := " Error requesting sent mail \n" + logBodyerr.Error()
alertAdmin(alert) alertAdmin(alert)
} }
logReq.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson) logReq.Header.Set(config.Requests.HeaderType, config.Requests.HeaderJson)
logResp, respErr := http.DefaultClient.Do(logReq) logResp, respErr := http.DefaultClient.Do(logReq)
if respErr != nil { if respErr != nil {
alert := " Error adding data to LOGS DB \n" + respErr.Error() alert := " Error adding data to SENT ITEMS DB \n" + respErr.Error()
alertAdmin(alert) alertAdmin(alert)
} }
// aa, _ := ioutil.ReadAll(logResp.Body)
// fmt.Println(aa)
defer logResp.Body.Close() defer logResp.Body.Close()
ioutil.ReadAll(logResp.Body) 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