-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
72 lines (57 loc) · 1.28 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
package main
import (
_ "csvtomysql/routers"
"fmt"
"log"
"time"
"github.com/astaxie/beego"
"github.com/beego/beego/v2/client/orm"
"github.com/go-sql-driver/mysql"
_ "github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
)
func init() {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
dbConfig := mysql.NewConfig()
dbConfig.User = "dbAdmin"
dbConfig.Passwd = "dbPassword"
dbConfig.Addr = "mysql:3306"
dbConfig.DBName = "PersonsDb"
dbConfig.Net = "tcp"
orm.RegisterDriver("mysql", orm.DRMySQL)
flag := false
// Checks if the Database is ready or not.
timeoutExceeded := time.After(5 * time.Minute)
for {
select {
case <-timeoutExceeded:
log.Printf("DB connection failed after %s timeout", 5*time.Minute)
break
case <-ticker.C:
err := orm.RegisterDataBase("default", "mysql", dbConfig.FormatDSN())
if err == nil {
flag = true
log.Println("DB Connection Made Successfully.")
break
} else {
log.Println("DB Connection failed. Retrying...")
}
log.Println(errors.Wrapf(err, "Failed to connect to DB. Retrying..."))
}
if flag {
break
}
}
}
func main() {
force := true
// Print log.
verbose := true
// Error.
err := orm.RunSyncdb("default", force, verbose)
if err != nil {
fmt.Println(err)
}
beego.Run()
}