-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (134 loc) · 4.8 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
package main
import (
"encoding/json"
"github.com/WalletService/cache"
config "github.com/WalletService/config"
"github.com/WalletService/controller"
"github.com/WalletService/db"
_ "github.com/WalletService/docs" // This line is necessary for go-swagger to find docs!
router "github.com/WalletService/http"
"github.com/WalletService/repository"
"github.com/WalletService/scheduler"
"github.com/WalletService/service"
"github.com/jinzhu/gorm"
"log"
"net/http"
"os"
)
var (
c config.Config
httpRouter router.IRouter
gormDb db.IDatabaseEngine
gDb *gorm.DB
)
// Cron
var (
cronCache cache.ICronCache
reportCron scheduler.IReportCron
)
// User
var (
userRepository repository.IUserRepository
userService service.IUserService
userController controller.IUserController
)
// Wallet
var (
walletCache cache.IWalletCache
walletRepository repository.IWalletRepository
walletService service.IWalletService
walletController controller.IWalletController
)
// Transaction
var (
transactionIdempotentCache cache.ITransactionIdempotentCache
transactionRepository repository.ITransactionRepository
transactionService service.ITransactionService
transactionController controller.ITransactionController
)
func main() {
initConfig()
httpRouter = router.NewMuxRouter()
httpRouter.ADDVERSION("/api/v1")
gormDb = db.NewGormDatabase()
gDb = gormDb.GetDatabase(c.Database)
gormDb.RunMigration()
initCachingLayer()
initUserServiceContainer()
initWalletServiceContainer()
initTransactionServiceContainer()
initCron()
httpRouter.SERVE(c.App.Port)
}
func initConfig() {
file, err := os.Open("./config.json")
if err != nil {
log.Printf("No ./config.json file found!! Terminating the server, error: %s\n", err.Error())
panic("No config file found! Error : " + err.Error())
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&c)
if err != nil {
log.Printf("Error occurred while decoding json to config model, error: %s\n", err.Error())
panic(err.Error())
}
}
func initCachingLayer() {
cacheEngine := cache.NewCache(c.Cache, c.Cache.Wallet.Db)
cacheEngine.GetCacheClient()
if err := cacheEngine.CheckConnection(); err != nil {
panic(err)
}
walletCache = cache.NewWalletCache(cacheEngine, c.Cache.Wallet) // 0 means no expiry date
cacheEngine2 := cache.NewCache(c.Cache, c.Cache.Idempotent.Db)
cacheEngine2.GetCacheClient()
if err := cacheEngine2.CheckConnection(); err != nil {
panic(err)
}
transactionIdempotentCache = cache.NewTransactionIdempotentCache(cacheEngine2, c.Cache.Idempotent)
cacheEngine3 := cache.NewCache(c.Cache, c.Cache.CronLock.Db)
cacheEngine3.GetCacheClient()
if err := cacheEngine3.CheckConnection(); err != nil {
panic(err)
}
cronCache = cache.NewCronCache(cacheEngine3, c.Cache.CronLock)
}
func initUserServiceContainer() {
userRepository = repository.NewUserRepository(gDb)
userService = service.NewUserService(userRepository)
userController = controller.NewUserController(userService)
httpRouter.GET("/user/{id}", userController.GetUser)
httpRouter.GET("/user", userController.GetUsers)
httpRouter.POST("/user", userController.PostUser)
httpRouter.PUT("/user/{id}", userController.PutUser)
httpRouter.DELETE("/user/{id}", userController.DeleteUser)
}
func initWalletServiceContainer() {
walletRepository = repository.NewWalletRepository(gDb)
walletService = service.NewWalletService(walletRepository, userService, walletCache)
walletController = controller.NewWalletController(walletService)
httpRouter.GET("/user/{id}/wallet", func(w http.ResponseWriter, r *http.Request) {
walletController.GetWallet(w,r,true)
})
httpRouter.GET("/wallet/{id}", func(w http.ResponseWriter, r *http.Request) {
walletController.GetWallet(w,r,false)
})
httpRouter.POST("/user/{id}/wallet", walletController.PostWallet)
httpRouter.POST("/wallet/{id}/block", walletController.BlockWallet)
httpRouter.POST("/wallet/{id}/unblock", walletController.UnBlockWallet)
}
func initTransactionServiceContainer() {
transactionRepository = repository.NewTransactionRepository(gDb)
transactionService = service.NewTransactionService(transactionRepository, walletService)
transactionController = controller.NewTransactionController(transactionService, transactionIdempotentCache)
httpRouter.GET("/transaction", transactionController.GetTransactions)
httpRouter.GET("/transaction/active", transactionController.GetActiveTransactions)
httpRouter.GET("/transaction/{id}", transactionController.GetTransaction)
httpRouter.GET("/wallet/{id}/transaction", transactionController.GetTransactionsByWalletId)
httpRouter.POST("/wallet/{id}/transaction", transactionController.PostTransaction)
httpRouter.PUT("/transaction/active", transactionController.UpdateActiveTransactions)
}
func initCron() {
reportCron = scheduler.NewReportCron(transactionService, cronCache)
reportCron.StartReportCron()
}