-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (102 loc) · 2.87 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
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"dota_league/db"
"dota_league/delivery"
"dota_league/handler"
"dota_league/repository"
"dota_league/worker"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/spf13/viper"
)
// ENVIRONMENT - Global variable that stores current envorinment
var ENVIRONMENT = "development"
// GetConfigStr add current env to viper config string query
func GetConfigStr(key string) string {
key = fmt.Sprintf("%s.%s", ENVIRONMENT, key)
return viper.GetString(key)
}
// GetConfigInt add current env to viper config int query
func GetConfigInt(key string) int {
key = fmt.Sprintf("%s.%s", ENVIRONMENT, key)
return viper.GetInt(key)
}
func init() {
//set default values
viper.SetDefault("cors.origin", "*")
//read the cofig file
viper.SetConfigFile(`config.json`)
err := viper.ReadInConfig()
if err != nil {
panic(err)
}
}
func dbConnection() db.Interface {
log.Printf("cs: %+v, %+v, %+v, %+v",
GetConfigStr(`database.url`),
GetConfigStr(`database.user`),
GetConfigStr(`database.pass`),
GetConfigStr(`database.name`))
db, err := db.Connect(context.Background(),
GetConfigStr(`database.url`),
GetConfigStr(`database.user`),
GetConfigStr(`database.pass`),
GetConfigStr(`database.name`))
if err != nil {
log.Fatal(err)
os.Exit(1)
}
return db
}
func main() {
prodFlag := flag.Bool("production", false, "run in production mode")
flag.Parse()
if *prodFlag {
ENVIRONMENT = "production"
}
log.Printf("Current Environment: %s", ENVIRONMENT)
db := dbConnection()
leagueRepository := repository.NewLeagueRepository(&db)
leagueDetailsRepository := repository.NewLeagueDetailsRepository(&db)
gameRepository := repository.NewGameRepository(&db)
playerRepository := repository.NewPlayerRepository(&db)
teamRepository := repository.NewTeamRepository(&db)
teamRosterRepository := repository.NewTeamRosterRepository(&db)
_ = worker.NewDataLoader(
&leagueRepository,
&leagueDetailsRepository,
&gameRepository,
&playerRepository,
&teamRepository,
&teamRosterRepository,
)
//----------------
//START WEB SERVER
//----------------
// Echo instance
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
e.Use(middleware.GzipWithConfig(middleware.GzipConfig{
Level: 5,
}))
e.Use(IPRateLimitWithConfig(GetConfigInt(`throttling.per_min`), GetConfigInt(`throttling.burst`)))
//CORS
e.Use(middleware.CORSWithConfig(middleware.CORSConfig{
AllowOrigins: []string{GetConfigStr(`cors.origin`)},
AllowMethods: []string{echo.GET, echo.HEAD},
}))
// Routes
e.Static("/", "./public")
leaguesHandler := handler.NewLeaguesHandler(&leagueDetailsRepository)
gamesHandler := handler.NewGameHandler(&gameRepository)
delivery.NewLeaguesDelivery(e, &leaguesHandler, &gamesHandler)
// Start server
e.Logger.Fatal(e.Start(GetConfigStr(`server.address`)))
}