-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
146 lines (121 loc) · 3.18 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package main
import (
"fmt"
"os"
"time"
"github.com/RemoteBuild/Remotebuild/constants"
"github.com/RemoteBuild/Remotebuild/models"
"github.com/RemoteBuild/Remotebuild/storage"
"github.com/JojiiOfficial/gaw"
log "github.com/sirupsen/logrus"
"gopkg.in/alecthomas/kingpin.v2"
_ "gorm.io/driver/postgres"
"gorm.io/gorm"
)
const version = "v0.0.1"
var (
app = kingpin.New("dmserver", "The data manager server")
appLogLevel = app.Flag("log-level", "Enable debug mode").HintOptions(constants.LogLevels...).Envar(getEnVar(EnVarLogLevel)).Short('l').Default(constants.LogLevels[2]).String()
appNoColor = app.Flag("no-color", "Disable colors").Envar(getEnVar(EnVarNoColor)).Bool()
appCfgFile = app.
Flag("config", "the configuration file for the server").
Envar(getEnVar(EnVarConfigFile)).
Short('c').String()
// Server commands
// Server start
serverCmd = app.Command("server", "Commands for the server")
serverCmdStart = serverCmd.Command("start", "Start the server")
// Config commands
// Config create
configCmd = app.Command("config", "Commands for the config file")
configCmdCreate = configCmd.Command("create", "Create config file")
configCmdCreateName = configCmdCreate.Arg("name", "Config filename").Default(models.GetDefaultConfig()).String()
)
var (
config *models.Config
db *gorm.DB
)
// Env vars
const (
// EnVarPrefix prefix of all used env vars
EnVarPrefix = "DM"
EnVarLogLevel = "LOG_LEVEL"
EnVarNoColor = "NO_COLOR"
EnVarConfigFile = "CONFIG"
)
// Return the variable using the server prefix
func getEnVar(name string) string {
return fmt.Sprintf("%s_%s", EnVarPrefix, name)
}
func main() {
// Set app attributes
app.HelpFlag.Short('h')
app.Version(version)
// parsing the args
parsed := kingpin.MustParse(app.Parse(os.Args[1:]))
setupLogger()
log.Infof("LogLevel: %s\n", *appLogLevel)
// set app logLevel
switch *appLogLevel {
case constants.LogLevels[0]:
// Debug
log.SetLevel(log.DebugLevel)
case constants.LogLevels[1]:
// Info
log.SetLevel(log.InfoLevel)
case constants.LogLevels[2]:
// Warning
log.SetLevel(log.WarnLevel)
case constants.LogLevels[3]:
// Error
log.SetLevel(log.ErrorLevel)
default:
fmt.Println("LogLevel not found!")
os.Exit(1)
return
}
if parsed != configCmdCreate.FullCommand() {
var shouldExit bool
config, shouldExit = models.InitConfig(*appCfgFile, false)
if shouldExit {
return
}
if !config.Check() {
log.Info("Exiting")
return
}
log.Debug("Connecting to db")
var err error
// Connect db
db, err = storage.ConnectToDatabase(config)
if err != nil {
log.Fatalln(err)
return
}
log.Debug("Successfully connected to DB")
}
// Init gaw random seed
gaw.Init()
switch parsed {
// Server --------------------
case serverCmdStart.FullCommand():
{
startAPI()
}
// Config --------------------
case configCmdCreate.FullCommand():
{
models.InitConfig(*configCmdCreateName, true)
}
}
}
func setupLogger() {
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: false,
TimestampFormat: time.Stamp,
FullTimestamp: true,
ForceColors: !*appNoColor,
DisableColors: *appNoColor,
})
}