-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
164 lines (129 loc) · 4.17 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
package main
import (
"context"
"database/sql"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"time"
"github.com/go-redis/redis/v8"
_ "github.com/go-sql-driver/mysql" // for mysql
"github.com/gorilla/mux"
)
var db *sql.DB
var err error
var ctx = context.Background()
var redisClient *redis.Client
// HealthCheck .
func HealthCheck(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode("ok")
}
func connectDatabase() {
db, err = sql.Open("mysql", dbConfig)
if err != nil {
log.Fatal(err)
}
db.SetMaxOpenConns(connectionPool)
db.SetMaxIdleConns(connectionPool)
db.SetConnMaxLifetime(time.Hour)
redisClient = redis.NewClient(&redis.Options{
Addr: "127.0.0.1:6379",
Password: "", // no password set
DB: 0, // use default DB
})
ctx = redisClient.Context()
pong, err := redisClient.Ping(ctx).Result()
fmt.Println(pong, err)
}
func inits() {
rand.Seed(time.Now().UnixNano())
connectDatabase()
}
func main() {
mumbai, _ = time.LoadLocation("Asia/Kolkata")
if len(os.Getenv("dbConfig")) > 0 {
dbConfig = os.Getenv("dbConfig")
}
if len(os.Getenv("connectionPool")) > 0 {
connectionPool, _ = strconv.Atoi(os.Getenv("connectionPool"))
}
if len(os.Getenv("test")) > 0 {
test, _ = strconv.ParseBool(os.Getenv("test"))
}
if len(os.Getenv("migrate")) > 0 {
migrate, _ = strconv.ParseBool(os.Getenv("migrate"))
}
test = false
// defer profile.Start().Stop()
// defer profile.Start(profile.MemProfile).Stop()
// go tool pprof --pdf main /var/folders/k6/0m4k5qg110jgfzpfdrdjv1dr0000gn/T/profile113169278/cpu.pprof > pprofs/file17.pdf
inits()
defer db.Close()
defer redisClient.Close()
router := mux.NewRouter()
if len(getValueRedis("accessToken")) > 0 {
accessToken = getValueRedis("accessToken")
}
loadAlerts()
go connectToKite()
go alertPassed()
go sendingAlertNotifications()
// cron
router.Path("/dailycron").HandlerFunc(checkHeaders(DailyCron)).Methods("GET")
router.Path("/account").Queries(
"user_id", "{user_id}",
).HandlerFunc(checkHeaders(AccountGet)).Methods("GET")
router.Path("/account").HandlerFunc(checkHeaders(AccountAdd)).Methods("POST")
router.Path("/account").Queries(
"user_id", "{user_id}",
).HandlerFunc(checkHeaders(AccountUpdate)).Methods("PUT")
router.Path("/alert").Queries(
"user_id", "{user_id}",
).HandlerFunc(checkHeaders(AlertGet)).Methods("GET")
router.Path("/alert").HandlerFunc(checkHeaders(AlertAdd)).Methods("POST")
router.Path("/amount").Queries(
"user_id", "{user_id}",
).HandlerFunc(checkHeaders(AmountGet)).Methods("GET")
router.Path("/buysell").HandlerFunc(checkHeaders(BuySellAdd)).Methods("POST")
router.Path("/timing").HandlerFunc(TimingGet).Methods("GET")
router.Path("/order").Queries(
"user_id", "{user_id}",
).HandlerFunc(checkHeaders(OrderGet)).Methods("GET")
router.Path("/position").Queries(
"user_id", "{user_id}",
).HandlerFunc(checkHeaders(PositionGet)).Methods("GET")
router.Path("/ticker").HandlerFunc(checkHeaders(TickerGet)).Methods("GET")
router.Path("/realtime").Queries(
"user_id", "{user_id}",
"tickers", "{tickers}",
).HandlerFunc(checkHeaders(wsHandler)).Methods("GET")
router.Path("/login").HandlerFunc(Login).Methods("GET")
router.Path("/token").HandlerFunc(Token).Methods("GET")
router.Path("/token").Queries(
"token", "{token}",
).HandlerFunc(TokenUpdate).Methods("PUT")
router.Path("/sendotp").HandlerFunc(checkHeaders(SendOTP)).Methods("POST")
router.Path("/verifyotp").HandlerFunc(checkHeaders(VerifyOTP)).Methods("POST")
router.Path("/sendemailotp").HandlerFunc(checkHeaders(SendEmailOTP)).Methods("POST")
router.Path("/verifyemailotp").HandlerFunc(checkHeaders(VerifyEmailOTP)).Methods("POST")
router.Path("/").HandlerFunc(HealthCheck).Methods("GET")
fmt.Println(http.ListenAndServe(":5000", &WithCORS{router}))
}
// WithCORS .
type WithCORS struct {
r *mux.Router
}
func (s *WithCORS) ServeHTTP(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Access-Control-Allow-Origin", "*")
res.Header().Set("Access-Control-Allow-Methods", "*")
res.Header().Set("Access-Control-Allow-Headers", "*")
// Stop here for a Preflighted OPTIONS request.
if req.Method == "OPTIONS" {
return
}
s.r.ServeHTTP(res, req)
}