-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (156 loc) · 4.46 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"fmt"
"os"
"time"
log "github.com/sirupsen/logrus"
dbhelper "github.com/JojiiOfficial/GoDBHelper"
"github.com/JojiiOfficial/WhShareServer/constants"
"github.com/JojiiOfficial/WhShareServer/models"
"github.com/JojiiOfficial/WhShareServer/services"
"github.com/JojiiOfficial/WhShareServer/storage"
"gopkg.in/alecthomas/kingpin.v2"
_ "github.com/go-sql-driver/mysql"
)
const version = "0.23.7a"
var (
app = kingpin.New("server", "A Rest 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()
appClean = app.Flag("cleanup", "Cleanup database items").Envar(getEnVar(EnVarClean)).Bool()
appAutoClean = app.Flag("autoclean", "Automatically cleanup database items").Envar(getEnVar(EnVarAutoClean)).Bool()
appYes = app.Flag("yes", "Skips confirmations").Short('y').Envar(getEnVar(EnVarYes)).Bool()
appCfgFile = app.
Flag("config", "the configuration file for the subscriber").
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()
benchCmd = app.Command("bench", "benchmark the server")
)
var (
config *models.ConfigStruct
db *dbhelper.DBhelper
isDebug bool = false
)
//Env vars
const (
//EnVarPrefix prefix of all used env vars
EnVarPrefix = "S"
EnVarLogLevel = "LOG_LEVEL"
EnVarNoColor = "NO_COLOR"
EnVarYes = "SKIP_CONFIRM"
EnVarConfigFile = "CONFIG"
EnVarClean = "CLEAN"
EnVarAutoClean = "AUTOCLEAN"
)
//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:]))
log.SetOutput(os.Stdout)
log.SetFormatter(&log.TextFormatter{
DisableTimestamp: false,
TimestampFormat: time.Stamp,
FullTimestamp: true,
ForceColors: !*appNoColor,
DisableColors: *appNoColor,
})
log.Infof("LogLevel: %s\n", *appLogLevel)
//set app logLevel
switch *appLogLevel {
case constants.LogLevels[0]:
//Debug
log.SetLevel(log.DebugLevel)
isDebug = true
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
}
var err error
db, err = storage.ConnectDB(config, isDebug, *appNoColor)
if err != nil {
log.Fatalln(err.Error())
return
}
}
//Clean only if
if *appClean {
cleanUp(db, config)
return
}
switch parsed {
//Server --------------------
case serverCmdStart.FullCommand():
{
startAPI()
}
case benchCmd.FullCommand():
{
//TODO enchant the bench cmd
start := time.Now()
sessions, err := models.GetAllSessionTokens(db)
fmt.Printf("Getting all %d sessions took %s\n", len(sessions), time.Now().Sub(start).String())
if err != nil {
LogError(err)
return
}
fmt.Println("Get all users having a session:")
for _, session := range sessions {
models.GetUserBySession(db, session)
fmt.Println(time.Now().Sub(start).String())
}
}
//Config --------------------
case configCmdCreate.FullCommand():
{
//whsub config create
models.InitConfig(*configCmdCreateName, true)
}
}
}
func cleanUp(db *dbhelper.DBhelper, config *models.ConfigStruct) {
log.Info("Cleaning up")
//Create new cleanup service
cleanupService := services.NewCleanupService(db, config)
//Call cleaner
err := <-cleanupService.Tick()
if err != nil {
log.Fatalln(err)
} else {
log.Info("Cleaning up successfully")
}
}