-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnagsms-receiver.go
182 lines (164 loc) · 4.27 KB
/
nagsms-receiver.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 (
"github.com/AgileBits/go-redis-queue/redisqueue"
"github.com/BurntSushi/toml"
"github.com/garyburd/redigo/redis"
log "github.com/sirupsen/logrus"
"math/rand"
"net/http"
"os"
"time"
)
var (
conf tomlConfig
// RedisPool creates new pool for redis
RedisPool *redis.Pool
ll map[string]log.Level
)
const (
letterBytes = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-"
letterIdxBits = 6 // 6 bits to represent a letter index
letterIdxMask = 1<<letterIdxBits - 1 // All 1-bits, as many as letterIdxBits
letterIdxMax = 63 / letterIdxBits // # of letter indices fitting in 63 bits
)
type oracleConf struct {
OracleHost string
OraclePort string
OracleUser string
OraclePassword string
OracleSid string
OracleMaxOpenCon int
OracleMaxIdleCon int
}
type redisConf struct {
RedisHost string
RedisPort string
RedisPassword string
RedisQueue string
RedisMaxActiveCon int
RedisMaxIdleCon int
}
type loggingConf struct {
LogPath string
LogLevel string
}
type appConf struct {
AppListenAddr string
AppListenPort string
AppHandlerURI string
}
type tomlConfig struct {
OracleConf oracleConf `toml:"oracle"`
RedisConf redisConf `toml:"redis"`
LoggingConf loggingConf `toml:"logging"`
AppConf appConf `toml:"app"`
}
func loadConf(filename string) (tomlConfig, error) {
if _, err := toml.DecodeFile(filename, &conf); err != nil {
return conf, err
}
return conf, nil
}
func init() {
var err error
conf, err = loadConf("config.toml")
if err != nil {
log.Fatal(err)
}
ll = map[string]log.Level{
"debug": log.DebugLevel,
"info": log.InfoLevel,
"warning": log.WarnLevel,
"error": log.ErrorLevel,
"fatal": log.FatalLevel,
}
filename := conf.LoggingConf.LogPath
Formatter := new(log.JSONFormatter)
log.SetFormatter(Formatter)
log.SetLevel(ll[conf.LoggingConf.LogLevel])
f, err := os.OpenFile(filename, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
if err != nil {
log.SetOutput(os.Stdout)
log.Error(err)
} else {
log.SetOutput(f)
}
RedisPool = redisCon(conf.RedisConf.RedisHost+":"+conf.RedisConf.RedisPort, conf.RedisConf.RedisPassword, conf.RedisConf.RedisMaxActiveCon, conf.RedisConf.RedisMaxIdleCon)
}
func randString(n int) string {
var src = rand.NewSource(time.Now().UnixNano())
b := make([]byte, n)
// A src.Int63() generates 63 random bits, enough for letterIdxMax characters!
for i, cache, remain := n-1, src.Int63(), letterIdxMax; i >= 0; {
if remain == 0 {
cache, remain = src.Int63(), letterIdxMax
}
if idx := int(cache & letterIdxMask); idx < len(letterBytes) {
b[i] = letterBytes[idx]
i--
}
cache >>= letterIdxBits
remain--
}
return string(b)
}
func redisCon(rhost, password string, maxActiveCon, maxIdleCon int) *redis.Pool {
return &redis.Pool{
MaxActive: maxActiveCon,
MaxIdle: maxIdleCon,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", rhost)
if err != nil {
log.Error(err)
} else {
log.Debug("Redis connection established")
}
if password != "" {
if _, err := c.Do("AUTH", password); err != nil {
log.Error(err)
c.Close()
return nil, err
}
} else {
log.Debug("Redis password accepted")
}
return c, err
},
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
}
func putQueue(rhost, qname, data string) {
c := RedisPool.Get()
defer c.Close()
q := redisqueue.New(qname, c)
rnd := randString(12)
stringdata := rnd + " " + data
_, err := q.Push(stringdata)
if err != nil {
log.Error("Cannot put data to queue: ", err)
} else {
log.Debug("Data was added: ", stringdata)
}
log.Debug("RND: ", rnd)
}
func handler(w http.ResponseWriter, r *http.Request) {
tel, ok := r.URL.Query()["tel"]
if !ok || len(tel) < 1 {
log.Errorf("Url Param %s is missing", "tel")
return
}
msg, ok := r.URL.Query()["msg"]
if !ok || len(msg) < 1 {
log.Errorf("Url Param %s is missing", "msg")
return
}
data := tel[0] + " " + msg[0]
go putQueue(conf.RedisConf.RedisHost+":"+conf.RedisConf.RedisPort, conf.RedisConf.RedisQueue, data)
}
func main() {
http.HandleFunc(conf.AppConf.AppHandlerURI, handler)
http.ListenAndServe(conf.AppConf.AppListenAddr+":"+conf.AppConf.AppListenPort, nil)
}