-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
312 lines (270 loc) · 8.7 KB
/
utils.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"strings"
"time"
kiteconnect "github.com/zerodhatech/gokiteconnect"
gomail "gopkg.in/gomail.v2"
)
const letterBytes = "abcdefghijklmnopqrstuvwxyz0123456789"
func logger(str interface{}) {
if test {
fmt.Println(str)
}
}
func requiredFiledsCheck(body map[string]string, required []string) string {
for _, field := range required {
if len(body[field]) == 0 {
return field
}
}
return ""
}
func getTickers() []map[string]string {
tickers := []map[string]string{}
value := getValueRedis("tickers")
if len(value) > 0 {
err := json.Unmarshal([]byte(value), &tickers)
if err != nil {
fmt.Println(err)
} else {
fmt.Println("from cache")
return tickers
}
}
kiteclient := kiteconnect.New(apiKey)
instruments, err := kiteclient.GetInstrumentsByExchange("NSE")
if err != nil {
fmt.Println("getTickers", err)
return []map[string]string{}
}
for _, instrument := range instruments {
tickers = append(tickers, map[string]string{
"i": strconv.Itoa(instrument.InstrumentToken),
"e": strconv.Itoa(instrument.ExchangeToken),
"t": instrument.Tradingsymbol,
"n": instrument.Name,
"ex": "", // since these are stocks and no expiry
"s": strconv.FormatFloat(instrument.StrikePrice, 'f', 2, 64),
"ti": strconv.FormatFloat(instrument.TickSize, 'f', 2, 64),
"l": strconv.FormatFloat(instrument.LotSize, 'f', 2, 64),
"in": instrument.InstrumentType,
"se": instrument.Segment,
"exc": instrument.Exchange,
})
}
body, err := json.Marshal(tickers)
if err != nil {
fmt.Println(err)
} else {
setValueRedisWithExpiration("tickers", string(body), 15*time.Minute)
}
return tickers
}
func sqlErrorCheck(code uint16) string {
if code == 1054 { // Error 1054: Unknown column
return statusCodeBadRequest
} else if code == 1062 { // Error 1062: Duplicate entry
return statusCodeDuplicateEntry
}
return statusCodeServerError
}
func setMeta(status string, msg string, msgType string) map[string]string {
if len(msg) == 0 {
if status == statusCodeBadRequest {
msg = "Bad Request Body"
} else if status == statusCodeServerError {
msg = "Internal Server Error"
}
}
return map[string]string{
"status": status,
"message": msg,
"message_type": msgType, // 1 : dialog or 2 : toast if msg
}
}
func getHTTPStatusCode(code string) int {
switch code {
case statusCodeOk:
return http.StatusOK
case statusCodeCreated:
return http.StatusCreated
case statusCodeBadRequest:
return http.StatusBadRequest
case statusCodeServerError:
return http.StatusInternalServerError
}
return http.StatusOK
}
// SetReponseStatus .
func SetReponseStatus(w http.ResponseWriter, r *http.Request, status string, msg string, msgType string, response map[string]interface{}) {
w.Header().Set("Status", status)
response["meta"] = setMeta(status, msg, msgType)
w.WriteHeader(getHTTPStatusCode(response["meta"].(map[string]string)["status"]))
json.NewEncoder(w).Encode(response)
}
func checkAppUpdate(r *http.Request) (map[string]string, bool) {
if strings.EqualFold(r.Header.Get("apikey"), androidLive) || strings.EqualFold(r.Header.Get("apikey"), androidTest) {
appversion, _ := strconv.ParseFloat(r.Header.Get("appversion"), 64)
if appversion < androidForceVersionCode {
return setMeta(statusCodeOk, "App update required", appUpdateRequired), true
} else if appversion < androidVersionCode {
return setMeta(statusCodeOk, "App update available", appUpdateAvailable), true
}
} else if strings.EqualFold(r.Header.Get("apikey"), iOSLive) || strings.EqualFold(r.Header.Get("apikey"), iOSTest) {
appversion, _ := strconv.ParseFloat(r.Header.Get("appversion"), 64)
if appversion < iOSForceVersionCode {
return setMeta(statusCodeOk, "App update required", appUpdateRequired), true
} else if appversion < iOSVersionCode {
return setMeta(statusCodeOk, "App update available", appUpdateAvailable), true
}
}
return map[string]string{}, false
}
func checkHeaders(h http.HandlerFunc) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
var response = make(map[string]interface{})
if len(r.Header.Get("apikey")) == 0 || len(r.Header.Get("appversion")) == 0 {
SetReponseStatus(w, r, statusCodeBadRequest, "apikey, appversion required", "", response)
return
} else if len(apikeys[r.Header.Get("apikey")]) == 0 {
SetReponseStatus(w, r, statusCodeBadRequest, "Unauthorized request. Not valid apikey", "", response)
return
}
if migrate { // statusCodeBadRequest because app will hit again if 500
SetReponseStatus(w, r, statusCodeBadRequest, "Server is busy. Please try after some time", dialogType, response)
return
}
h.ServeHTTP(w, r)
})
}
// RandStringBytes .
func RandStringBytes(n int) string {
b := make([]byte, n)
for i := range b {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}
func checkUserID(userID string) bool {
var status string
db.QueryRow("select status from " + accountTable + " where user_id = '" + userID + "' and status = 1").Scan(&status)
return len(status) > 0
}
func sendNotifications(userID, heading, content string) {
data := map[string]interface{}{}
data["app_id"] = onesignalAppID
data["headings"] = map[string]string{"en": heading}
data["contents"] = map[string]string{"en": content}
data["filters"] = []map[string]string{
map[string]string{
"field": "tag",
"key": "user_id",
"relation": "=",
"value": userID,
},
}
byteData, _ := json.Marshal(data)
req, _ := http.NewRequest("POST", "https://onesignal.com/api/v1/notifications", bytes.NewBuffer(byteData))
req.Header.Add("Authorization", "Basic NjVmMjBhZTItNTZjMS00MzUyLTg5MTgtMjZhNmI3ZDk2NDg1")
req.Header.Add("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("sendNotifications", userID, err)
return
}
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Println("sendNotifications", userID, err)
return
}
}
func isMarketOpen() bool {
now := time.Now().UTC()
var (
openTime string
closeTime string
holiday string
)
db.QueryRow("select open, close, holiday from "+timingTable+" where day = '"+strconv.Itoa(int(now.Weekday()))+"'").Scan(&openTime, &closeTime, &holiday)
if len(openTime) == 0 {
return false
}
if strings.EqualFold(holiday, "1") {
return false
}
openArr := strings.Split(openTime, ":")
closeArr := strings.Split(closeTime, ":")
hour, _ := strconv.Atoi(openArr[0])
min, _ := strconv.Atoi(openArr[1])
open := time.Date(now.Year(), now.Month(), now.Day(), hour, min, 0, 0, time.UTC).Add(-330 * time.Minute)
if time.Now().UTC().Before(open) {
return false
}
hour, _ = strconv.Atoi(closeArr[0])
min, _ = strconv.Atoi(closeArr[1])
close := time.Date(now.Year(), now.Month(), now.Day(), hour, min, 0, 0, time.UTC).Add(-330 * time.Minute)
if time.Now().UTC().After(close) {
return false
}
return true
}
func setValueRedis(key, value string) {
redisClient.Set(ctx, key, value, 0)
}
func setValueRedisWithExpiration(key, value string, d time.Duration) {
redisClient.Set(ctx, key, value, d)
}
func setValuesRedis(keyValues map[string]string) {
redisClient.MSet(ctx, keyValues, 0)
}
func getValueRedis(key string) string {
val, _ := redisClient.Get(ctx, key).Result()
return val
}
func getValuesRedis(keys []string) []interface{} {
val, _ := redisClient.MGet(ctx, keys...).Result()
return val
}
func msg91(to, otp string) {
url := "https://api.msg91.com/api/v2/sendsms"
payload := strings.NewReader("{ \"sender\": \"ASAIRA\", \"route\": \"4\", \"country\": \"91\", \"sms\": [ { \"message\": \"" + strings.ReplaceAll(otpMessage, "##OTP##", otp) + "\", \"to\": [ \"" + to + "\"] } ] }")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("authkey", "331064AVVjQRraN7jt5ed65c82P1")
req.Header.Add("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Println("SendOTP", err)
return
}
defer res.Body.Close()
ioutil.ReadAll(res.Body)
}
// defer measureTime("expensivePrint")()
// To log code latency
func measureTime(funcName string) func() {
start := time.Now()
return func() {
fmt.Printf("Time taken by %s function is %v \n", funcName, time.Since(start))
}
}
func mail(to, title, body string) {
fmt.Println("mail", to, title, body)
m := gomail.NewMessage()
m.SetHeader("From", "[email protected]")
m.SetHeader("To", "[email protected]")
m.SetHeader("Bcc", to)
m.SetHeader("Subject", title)
m.SetBody("text/html", body)
d := gomail.NewDialer("smtp.gmail.com", 587, "[email protected]", "Gmail9848$")
if err := d.DialAndSend(m); err != nil {
fmt.Println("mail", to, err)
}
}