-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
executable file
·135 lines (99 loc) · 3.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
package main
import (
"api-gateway/config"
"api-gateway/handlers/routes"
"api-gateway/healthutility"
"api-gateway/constants"
"api-gateway/logoutput"
"api-gateway/messagequeue"
"api-gateway/redisutility"
"fmt"
"github.com/tkanos/gonfig"
"log"
"os"
"path"
"path/filepath"
"runtime"
"strings"
"time"
)
func main() {
fmt.Println("--------API GATEWAY init--------")
configuration := config.Configuration{}
err := gonfig.GetConf(getConfigFileName(), &configuration)
if err != nil {
fmt.Println(err)
os.Exit(500)
}
t := time.Now()
var sb strings.Builder
sb.WriteString(configuration.Log_file_path)
sb.WriteString("api_log_")
sb.WriteString(t.Format("2006-01-02"))
sb.WriteString(".txt")
logoutput.SetLogFileLocation(sb.String())
// https://blog.kowalczyk.info/article/1Ll7/rotate-log-files-daily-in-go.html
// https://stackoverflow.com/questions/28796021/how-can-i-log-in-golang-to-a-file-with-log-rotation
// ^ maybe use lumberjack package or log rotation
f, err := os.OpenFile(sb.String(), os.O_RDWR | os.O_CREATE | os.O_APPEND, 0644)
if err != nil {
log.Fatalf("Error opening log file: %v", err)
}
//defer f.Close()
//log.SetOutput(f)
logoutput.Set(f)
defer logoutput.Close() // call from main only
log.Println("--------API GATEWAY init--------")
log.Printf("Application refresh, setting log file to : %s", logoutput.GetLogFileLocation())
health := healthutility.Health{
ServiceName: "api-gateway",
Status: healthutility.SERVICE_STATUS_UP,
}
monitor := healthutility.Monitor{
&health,
}
health.ServiceStarted()
//
////http://localhost:8085/health/
//
go monitor.Listen(configuration.HealthCheckPoint)
redisutility.SetConfiguration(configuration)
messagequeue.SetConfiguration(configuration)
go messagequeue.Subscribe(constants.BETGAMES_REQUEST_SUBSCRIPTION_BET_PAYIN_TOPIC,
"betgames-api-response-subscription-payin")
setupConsumers()
time.Sleep(5 * time.Second)
routes.SetupRoutes(configuration, &health)
}
func setupConsumers() {
go messagequeue.Subscribe(constants.BETGAMES_RESPONSE_REFRESH_TOKEN_TOPIC,
constants.BETGAMES_QUEUE_FOR_SUBSCRIBER_REFRESH_TOKEN)
go messagequeue.Subscribe(constants.BETGAMES_RESPONSE_NEW_TOKEN_TOPIC,
constants.BETGAMES_QUEUE_FOR_SUBSCRIBER_NEW_TOKEN)
go messagequeue.Subscribe(constants.BETGAMES_RESPONSE_GET_ACCOUNT_DETAILS_TOPIC,
constants.BETGAMES_QUEUE_FOR_SUBSCRIBER_ACCOUNT_DETAILS)
go messagequeue.Subscribe(constants.BETGAMES_RESPONSE_GET_BALANCE_TOPIC,
constants.BETGAMES_QUEUE_FOR_SUBSCRIBER_GET_BALANCE)
go messagequeue.Subscribe(constants.BETGAMES_RESPONSE_TRANSACTION_BET_PAYIN_TOPIC,
constants.BETGAMES_QUEUE_FOR_SUBSCRIBER_TRANSACTION_PAYIN)
go messagequeue.Subscribe(constants.BETGAMES_RESPONSE_SUBSCRIPTION_BET_PAYIN_TOPIC,
constants.BETGAMES_QUEUE_FOR_SUBSCRIBER_SUBSCRIPTION_PAYIN)
go messagequeue.Subscribe(constants.BETGAMES_RESPONSE_TRANSACTION_BET_PAYOUT_TOPIC,
constants.BETGAMES_QUEUE_FOR_SUBSCRIBER_TRANSACTION_PAYOUT)
go messagequeue.Subscribe(constants.BETGAMES_RESPONSE_COMBINATION_BET_PAYIN_TOPIC,
constants.BETGAMES_QUEUE_FOR_SUBSCRIBER_COMBINATION_PAYIN)
go messagequeue.Subscribe(constants.BETGAMES_RESPONSE_COMBINATION_BET_PAYOUT_TOPIC,
constants.BETGAMES_QUEUE_FOR_SUBSCRIBER_COMBINATION_PAYOUT)
go messagequeue.Subscribe(constants.BETGAMES_RESPONSE_GENERAL_ERROR_TOPIC,
constants.BETGAMES_QUEUE_FOR_SUBSCRIBER_GENERAL_ERROR)
}
func getConfigFileName() string {
env := os.Getenv("ENV")
if len(env) == 0 {
env = "production"
}
filename := []string{"config/", env, ".json"}
_, dirname, _, _ := runtime.Caller(0)
filePath := path.Join(filepath.Dir(dirname), strings.Join(filename, ""))
return filePath
}