-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
120 lines (99 loc) · 2.63 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
118
119
120
package main
import (
"fmt"
"os"
"runtime"
"github.com/gin-gonic/gin"
"github.com/sheey11/chocolate/common"
"github.com/sheey11/chocolate/log"
"github.com/sheey11/chocolate/middleware"
"github.com/sheey11/chocolate/models"
"github.com/sheey11/chocolate/routes"
"github.com/sirupsen/logrus"
flag "github.com/spf13/pflag"
)
var (
BUILDVERSION = "v0.0.0"
GOVERSION = runtime.Version()
)
var flags struct {
Verbose *bool
Addr *string
Config *string
Migrate *bool
Serve *bool
Version *bool
Help *bool
}
func checkArgs() {
flags.Migrate = flag.BoolP("migrate", "m", false, "perform database initilization")
flags.Serve = flag.BoolP("serve", "s", false, "start the server")
flags.Version = flag.BoolP("version", "V", false, "print chocolate version")
flags.Verbose = flag.BoolP("verbose", "v", false, "more verbose")
flags.Addr = flag.StringP("listen", "l", "0.0.0.0:4090", "specify listening address")
flags.Config = flag.StringP("config", "c", "config.yaml", "the config file path")
flags.Help = flag.BoolP("help", "h", false, "show help infomation")
flag.Parse()
}
var engine *gin.Engine
func startServer(addr string, connStr string) {
err := models.Connect(connStr)
if err != nil {
logrus.WithError(err).Fatal("connect database failed")
}
engine = gin.New()
engine.Use(gin.Recovery())
engine.Use(middleware.Cors())
engine.Use(middleware.Log())
routes.Mount(engine)
logrus.Infof("server is running at %v", addr)
err = engine.Run(addr)
if err != nil {
logrus.WithError(err).Fatalf("server start failed.")
}
}
func main() {
defer func() {
err := recover()
if err != nil {
logrus.Fatal(err)
}
}()
checkArgs()
if *flags.Help {
fmt.Fprintf(os.Stderr, "Usage of chocolate:\n")
fmt.Fprintf(os.Stderr, " chocolate [-s] [-m] [-l addr]\n")
fmt.Fprintf(os.Stderr, "Arguments:\n")
flag.PrintDefaults()
return
}
if *flags.Version {
fmt.Fprintf(os.Stderr, "chocolate %s runtime %s", BUILDVERSION, GOVERSION)
return
}
log.SetupLogging(*flags.Verbose)
if *flags.Verbose {
common.DEBUG = true
logrus.SetLevel(logrus.DebugLevel)
} else {
common.DEBUG = false
logrus.SetLevel(logrus.InfoLevel)
}
logrus.Info(fmt.Sprintf("BUILD: %s, GOVERSION: %s", BUILDVERSION, GOVERSION))
if !*flags.Migrate && !*flags.Serve {
logrus.Error("No action specified, exiting")
return
}
common.LoadConfig(*flags.Config)
connStr := common.GetConnStrFromEnv()
if *flags.Migrate {
err := models.Migrate(connStr)
if err != nil {
logrus.WithField("err", err).Fatalf("error on migrating database")
}
logrus.Infof("Migration complete")
}
if *flags.Serve {
startServer(*flags.Addr, connStr)
}
}