-
Notifications
You must be signed in to change notification settings - Fork 88
/
main.go
182 lines (162 loc) · 5.63 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"os"
"strconv"
"github.com/heptiolabs/healthcheck"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/messagebird/sachet"
"github.com/messagebird/sachet/provider/aliyun"
"github.com/messagebird/sachet/provider/aspsms"
"github.com/messagebird/sachet/provider/cm"
"github.com/messagebird/sachet/provider/esendex"
"github.com/messagebird/sachet/provider/exotel"
"github.com/messagebird/sachet/provider/freemobile"
"github.com/messagebird/sachet/provider/ghasedak"
"github.com/messagebird/sachet/provider/infobip"
"github.com/messagebird/sachet/provider/kannel"
"github.com/messagebird/sachet/provider/kavenegar"
"github.com/messagebird/sachet/provider/mailruim"
"github.com/messagebird/sachet/provider/mediaburst"
"github.com/messagebird/sachet/provider/melipayamak"
"github.com/messagebird/sachet/provider/messagebird"
"github.com/messagebird/sachet/provider/nexmo"
"github.com/messagebird/sachet/provider/nowsms"
"github.com/messagebird/sachet/provider/otc"
"github.com/messagebird/sachet/provider/ovh"
"github.com/messagebird/sachet/provider/pushbullet"
"github.com/messagebird/sachet/provider/sap"
"github.com/messagebird/sachet/provider/sfr"
"github.com/messagebird/sachet/provider/sipgate"
"github.com/messagebird/sachet/provider/sms77"
"github.com/messagebird/sachet/provider/smsc"
"github.com/messagebird/sachet/provider/telegram"
"github.com/messagebird/sachet/provider/tencentcloud"
"github.com/messagebird/sachet/provider/textmagic"
"github.com/messagebird/sachet/provider/turbosms"
"github.com/messagebird/sachet/provider/twilio"
)
var (
listenAddress = flag.String("listen-address", ":9876", "The address to listen on for HTTP requests.")
configFile = flag.String("config", "config.yaml", "The configuration file")
)
func main() {
flag.Parse()
log.SetFlags(log.LstdFlags | log.Lshortfile)
if err := LoadConfig(*configFile); err != nil {
log.Fatalf("Error loading configuration: %s", err)
}
app := handlers{}
http.HandleFunc("/alert", app.Alert)
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/-/reload", app.Reload)
hc := healthcheck.NewMetricsHandler(prometheus.DefaultRegisterer, "sachet")
http.HandleFunc("/-/live", hc.LiveEndpoint)
http.HandleFunc("/-/ready", hc.ReadyEndpoint)
if os.Getenv("PORT") != "" {
*listenAddress = ":" + os.Getenv("PORT")
}
log.Printf("Listening on %s", *listenAddress)
log.Fatal(http.ListenAndServe(*listenAddress, nil))
}
// receiverConfByReceiver loops the receiver conf list and returns the first instance with that name.
func receiverConfByReceiver(name string) *ReceiverConf {
for i := range config.Receivers {
rc := &config.Receivers[i]
if rc.Name == name {
return rc
}
}
return nil
}
func providerByName(name string) (sachet.Provider, error) {
// TODO: use map of providers instead
switch name {
case "messagebird":
return messagebird.NewMessageBird(config.Providers.MessageBird), nil
case "nexmo":
return nexmo.NewNexmo(config.Providers.Nexmo)
case "twilio":
return twilio.NewTwilio(config.Providers.Twilio), nil
case "infobip":
return infobip.NewInfobip(config.Providers.Infobip), nil
case "kannel":
return kannel.NewKannel(config.Providers.Kannel), nil
case "kavenegar":
return kavenegar.NewKaveNegar(config.Providers.KaveNegar), nil
case "turbosms":
return turbosms.NewTurbosms(config.Providers.Turbosms), nil
case "smsc":
return smsc.NewSmsc(config.Providers.Smsc), nil
case "exotel":
return exotel.NewExotel(config.Providers.Exotel), nil
case "cm":
return cm.NewCM(config.Providers.CM), nil
case "telegram":
return telegram.NewTelegram(config.Providers.Telegram)
case "mailruim":
return mailruim.NewMailruIM(config.Providers.MailruIM)
case "otc":
return otc.NewOTC(config.Providers.OTC), nil
case "mediaburst":
return mediaburst.NewMediaBurst(config.Providers.MediaBurst), nil
case "freemobile":
return freemobile.NewFreeMobile(config.Providers.FreeMobile), nil
case "aspsms":
return aspsms.NewAspSms(config.Providers.AspSms), nil
case "sipgate":
return sipgate.NewSipgate(config.Providers.Sipgate), nil
case "pushbullet":
return pushbullet.NewPushbullet(config.Providers.Pushbullet), nil
case "nowsms":
return nowsms.NewNowSms(config.Providers.NowSms), nil
case "aliyun":
return aliyun.NewAliyun(config.Providers.Aliyun)
case "ovh":
return ovh.NewOvh(config.Providers.OVH)
case "tencentcloud":
return tencentcloud.NewTencentCloud(config.Providers.TencentCloud), nil
case "sap":
return sap.NewSap(config.Providers.Sap), nil
case "esendex":
return esendex.NewEsendex(config.Providers.Esendex), nil
case "sms77":
return sms77.NewSms77(config.Providers.Sms77), nil
case "ghasedak":
return ghasedak.NewGhasedak(config.Providers.Ghasedak), nil
case "sfr":
return sfr.NewSfr(config.Providers.Sfr), nil
case "textmagic":
return textmagic.NewTextMagic(config.Providers.TextMagic), nil
case "melipayamak":
return melipayamak.NewMelipayamak(config.Providers.Melipayamak), nil
}
return nil, fmt.Errorf("%s: Unknown provider", name)
}
func errorHandler(w http.ResponseWriter, status int, err error, provider string) {
w.WriteHeader(status)
data := struct {
Error bool
Status int
Message string
}{
true,
status,
err.Error(),
}
// respond json
body, err := json.Marshal(data)
if err != nil {
log.Fatalf("marshalling error: " + err.Error())
}
if _, err := w.Write(body); err != nil {
log.Fatalf("marshalling error: " + err.Error())
}
log.Println("error: " + string(body))
requestTotal.WithLabelValues(strconv.FormatInt(int64(status), 10), provider).Inc()
}