-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
70 lines (63 loc) · 1.51 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
package main
import (
"bytes"
"io/ioutil"
"log"
"net"
"net/http"
_ "net/http/pprof"
"github.com/gin-gonic/gin"
"github.com/spf13/viper"
"github.com/ritoon/api-vote/db"
"github.com/ritoon/api-vote/db/moke"
"github.com/ritoon/api-vote/db/postgres"
"github.com/ritoon/api-vote/db/sqlboiler"
"github.com/ritoon/api-vote/db/sqlite"
"github.com/ritoon/api-vote/service"
)
func main() {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
// config
viper.SetConfigType("yaml")
configFile, err := ioutil.ReadFile("./config.test.yaml")
if err != nil {
panic(err)
}
viper.ReadConfig(bytes.NewBuffer(configFile))
// init project
r := gin.Default()
var db db.DataManager
switch viper.GetString("env") {
case "test":
db = moke.New()
case "preprod":
db = sqlite.New("test.db")
case "prod":
db = postgres.New("localhost", "postgres")
//db = postgres.New(GetLocalIP(), "postgres")
case "boiler":
db = sqlboiler.New("localhost", "postgres")
//db = postgres.New(GetLocalIP(), "postgres")
}
v1 := r.Group("/v1")
service.Init(v1, db)
r.Run(":" + viper.GetString("port"))
}
// GetLocalIP returns the non loopback local IP of the host
func GetLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}