-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
97 lines (83 loc) · 2.15 KB
/
main.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
package main
import (
"database/sql"
"log"
"net/http"
"os"
"github.com/etowett/returns/core"
"github.com/etowett/returns/utils"
_ "github.com/go-sql-driver/mysql"
"github.com/joho/godotenv"
)
func main() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file ", err)
}
f, err := os.OpenFile(os.Getenv("LOG_FILE"), os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
log.Fatal("Log file error: ", err)
}
defer f.Close()
log.SetFlags(log.LstdFlags | log.Lshortfile)
log.SetOutput(f)
utils.DBCon, err = sql.Open("mysql", os.Getenv("DB_USER")+":"+os.Getenv("DB_PASS")+"@tcp("+os.Getenv("DB_HOST")+":3306)/"+os.Getenv("DB_NAME")+"?charset=utf8")
if err != nil {
log.Fatal("db error: ", err)
}
defer utils.DBCon.Close()
// Test the connection to the database
err = utils.DBCon.Ping()
if err != nil {
log.Fatal("Error DB ping ", err)
}
initStuff()
// Route set up
http.HandleFunc("/at-dlrs", core.ATDlrPage)
http.HandleFunc("/rm-dlrs", core.RMDlrPage)
http.HandleFunc("/saf-dlrs", core.SafDlrPage)
http.HandleFunc("/saf-notify", core.SafNotifyPage)
http.HandleFunc("/cache-dlr", core.CacheDlrPage)
http.HandleFunc("/cache-bulk-dlr", core.CacheBulkDlrPage)
http.HandleFunc("/inbox", core.InboxPage)
http.HandleFunc("/optout", core.OptoutPage)
http.HandleFunc("/saf-inbox", core.SafInboxPage)
log.Fatal(http.ListenAndServe(":"+os.Getenv("PORT"), nil))
}
func initStuff() {
log.Println("Started callbacks on port", os.Getenv("PORT"))
// Listen for Dlrs
go core.ListenForDlrs()
// Listen for Inbox
go core.ListenForInbox()
// Listen for optout
go core.ListenForOptOut()
// Push Scheduled Dlrs to reqdy queue
go core.PushToQueue()
startQueueDlrWorkers()
startSMSPool()
}
func startSMSPool() {
for i := 1; i <= 10; i++ {
go func() {
for notif := range core.SMSNotifs {
err := notif.ProcessSMSNofit()
if err != nil {
log.Println("ProcessSMSNofit: ", err)
}
}
}()
}
}
func startQueueDlrWorkers() {
for i := 1; i <= 9; i++ {
go func() {
for dlr := range core.DLRReqChan {
err := core.QueueDlr(dlr)
if err != nil {
log.Println("QueueDlr: ", err)
}
}
}()
}
}