-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
101 lines (87 loc) · 2.32 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
package main
import (
"fmt"
"os"
"time"
"github.com/gin-gonic/gin"
"github.com/qshuai/coindis/controllers"
"github.com/qshuai/coindis/models"
"github.com/qshuai/coindis/routers"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)
func main() {
viper.AddConfigPath("./")
viper.SetConfigType("yaml")
viper.SetConfigName("app")
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("read configuration file error: %s\n", err)
os.Exit(1)
}
level, err := logrus.ParseLevel(viper.GetString("log.level"))
if err != nil {
fmt.Printf("log.level configuration malformated: %s\n", err)
os.Exit(1)
}
logrus.SetLevel(level)
_, err = os.Stat("./logs/")
if os.IsNotExist(err) {
err = os.MkdirAll("./logs", 0766)
if err != nil {
fmt.Printf("create log directory error: %s\n", err)
os.Exit(1)
}
}
logrus.SetFormatter(&logrus.TextFormatter{FullTimestamp: true, TimestampFormat: "2006-01-02 15:04:05", DisableTimestamp: false})
file, err := os.OpenFile("./logs/"+viper.GetString("log.filename")+".log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, os.FileMode(0644))
if err != nil {
fmt.Printf("open the specifed log file failed: %s\n", err)
os.Exit(1)
}
logrus.SetOutput(file)
// sync database state
err = models.SyncDataSource()
if err != nil {
fmt.Printf("sync database failed: %s\n", err)
os.Exit(1)
}
// init balance
err = controllers.UpdateBalance()
if err != nil {
fmt.Printf("initial account balance error: %s\n", err)
os.Exit(1)
}
go func() {
ticker := time.NewTicker(time.Minute * 30)
defer func() {
ticker.Stop()
logrus.Error("the goroutine holds the ticker to update balance existed")
}()
for _ = range ticker.C {
err = controllers.UpdateBalance()
if err != nil {
logrus.Errorf("update account balance failed, please check: %s\n", err)
}
}
}()
controllers.InitCache()
go func() {
ticker := time.NewTicker(time.Minute * 30)
defer func() {
ticker.Stop()
logrus.Error("the goroutine holds the ticker to cache clean existed")
}()
for _ = range ticker.C {
controllers.CleanCache()
}
}()
logrus.Info("the coindis program started!")
r := gin.Default()
r.LoadHTMLGlob("views/*")
r.Static("/static", "./static")
routers.RegisterApi(r)
err = r.Run(":" + viper.GetString("app.port"))
if err != nil {
fmt.Printf("HTTP server runs error: %s\n", err)
}
}