-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (94 loc) · 3.08 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
package main
import (
"log"
"runtime"
"fmt"
"github.com/spf13/viper"
"app/controllers"
"app/routes"
"app/commons/email"
"app/commons/mysql"
"app/commons/mongodb"
"app/commons/server"
)
func init() {
// Verbose logging with file name and line number
log.SetFlags(log.Lshortfile)
// Use all CPU cores
runtime.GOMAXPROCS(runtime.NumCPU())
}
func main() {
// Load configuration file
fmt.Println("\n==========================")
fmt.Println("Loading configuration...")
// Load configuration file
viper.SetConfigName("config")
viper.AddConfigPath("config")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
// Load email service
ConfigureEmail()
// Load MySQL service
ConfigureMySQL()
// Load controller routes
controllers.Load()
// Load MongoDB service
ConfigureMongoDB()
// Load Server
ConfigureServer()
}
// Configure email
func ConfigureEmail() {
c := email.SMTPInfo{
Host : viper.GetString("email.host"),
Port : viper.GetInt("email.port"),
FromName : viper.GetString("email.from_name"),
FromAddress : viper.GetString("email.from_address"),
Encryption : viper.GetString("email.encryption"),
Username : viper.GetString("email.username"),
Password : viper.GetString("email.Password"),
}
email.Configure(c)
fmt.Println("Email configured...")
}
// Configure MySQl
func ConfigureMySQL() {
c := mysql.MySQLInfo{
Username : viper.GetString("database.mysql.username"),
Password : viper.GetString("database.mysql.password"),
Name : viper.GetString("database.mysql.database"),
Hostname : viper.GetString("database.mysql.host"),
Port : viper.GetInt("database.mysql.port"),
Parameter : viper.GetString("database.mysql.parameter"),
}
mysql.Connect(c)
fmt.Println("MySQL configured...")
}
// Configure MongoDB
func ConfigureMongoDB() {
c := mongodb.DialInfo{
Addrs : viper.GetString("database.mongodb.host") + ":" + viper.GetString("database.mongodb.port"),
Database : viper.GetString("database.mongodb.database"),
Username : viper.GetString("database.mongodb.username"),
Password : viper.GetString("database.mongodb.password"),
}
mongodb.Connect(c)
fmt.Println("MongoDB configured...")
}
// Configure Server
func ConfigureServer() {
c := server.Server{
Hostname : viper.GetString("server.host"),
UseHTTP : viper.GetBool("server.use_http"),
UseHTTPS : viper.GetBool("server.use_https"),
HTTPPort : viper.GetInt("server.http_port"),
HTTPSPort : viper.GetInt("server.https_port"),
CertFile : viper.GetString("server.cert_file"),
KeyFile : viper.GetString("server.key_file"),
}
fmt.Println("Server configured ...")
fmt.Println("==========================")
server.Run(routes.LoadHTTP(), routes.LoadHTTPS(), c)
}