-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (67 loc) · 1.93 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
package main
import (
"fmt"
"log"
"os"
"syscall"
"time"
"github.com/fvbock/endless"
"github.com/gin-gonic/contrib/ginrus"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/williambao/metatable/config"
"github.com/williambao/metatable/model"
"github.com/williambao/metatable/router"
)
const Version = "0.1.0"
func main() {
// 加载配置文件
cfg, err := config.GetConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err.Error()))
}
// logrus.SetFormatter(&logrus.JSONFormatter{})
if cfg.IsProduction {
gin.SetMode(gin.ReleaseMode)
logrus.SetLevel(logrus.WarnLevel)
logFile, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
panic(err.Error())
}
logrus.SetOutput(logFile)
} else {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(os.Stdout)
}
// 数据库连接
if err := model.OpenDB(cfg); err != nil {
panic(fmt.Errorf("Fatal error connect to database : %s", err.Error()))
}
// 同步数据库结构
if err := model.Migrate(); err != nil {
panic(fmt.Errorf("Fatal error migrate tables to database : %s", err.Error()))
}
// if err := model.EnableCache(); err != nil {
// panic(fmt.Errorf("Fatal error open database cache: %s", err.Error()))
// }
// // 缓存
// _, err := cache.NewCache("memory", `{"interval":60}`)
// if err != nil {
// panic(fmt.Errorf("无法初始化缓存: %s", err.Error()))
// }
handler := router.Load(
ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true),
router.SetConfig(cfg),
)
listen := fmt.Sprintf("%s:%d", cfg.Server.Host, cfg.Server.Port)
server := endless.NewServer(listen, handler)
server.BeforeBegin = func(add string) {
logrus.Debugf("Actual pid is %d", syscall.Getpid())
}
fmt.Printf("start app on: http://localhost:%d\n", cfg.Server.Port)
if err := server.ListenAndServe(); err != nil {
panic(err)
}
log.Printf("Server on %s stopped\n", listen)
os.Exit(0)
}