-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
68 lines (59 loc) · 1.69 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
/*
The main function to start the application.
At first, it initializes all setting include logger, maps, singleflight, database.
Then it starts the service by the framework, gin-gonic/gin.
*/
package main
import (
"advertisement_api/api/routes"
"advertisement_api/internal/repository/redis"
"advertisement_api/internal/repository/sql"
"advertisement_api/utils"
"github.com/gin-gonic/gin"
)
func main() {
// initialize the logger.
utils.InitLogger()
// initialize the maps include gender-map, country-map and platform-map.
utils.InitMaps()
// initialize the singleflight instance
utils.InitSingleFlight()
// initialize the sql-db connection. return and log error when connect fail.
if err := utils.InitDB(); err != nil {
utils.LogFatal(err.Error())
return
}
// close sql-db connection when application turn off. log error when close fail.
defer func() {
err := utils.CloseDB()
if err != nil {
utils.LogFatal(err.Error())
}
}()
// initialize the redis connection. return and log error when connect fail.
if err := utils.InitRedis(); err != nil {
utils.LogFatal(err.Error())
return
}
// close redis connection when application turn off. log error when close fail.
defer func() {
err := utils.CloseRedis()
if err != nil {
utils.LogFatal(err.Error())
}
}()
// set mode to the release mode.
gin.SetMode(gin.ReleaseMode)
// new a sql repository
sqlRepository := sql.NewRepository()
// new a redis repository
redisRepository := redis.NewRepository()
// initialize all the routes in the server.
r := routes.InitRoutes(sqlRepository, redisRepository)
// start the service. log error when start fail.
err := r.Run()
if err != nil {
utils.LogFatal(err.Error())
return
}
}