-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
executable file
·128 lines (107 loc) · 2.88 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
package main
import (
"flag"
"os"
"os/signal"
"syscall"
log "github.com/sirupsen/logrus"
"github.com/gost/server/configuration"
"github.com/gost/server/database/postgis"
"github.com/gost/server/http"
gostLog "github.com/gost/server/log"
"github.com/gost/server/mqtt"
"github.com/gost/server/sensorthings/api"
"github.com/gost/server/sensorthings/models"
)
var (
stAPI models.API
gostServer http.Server
mqttClient models.MQTTClient
file *os.File
logger *log.Logger
mainLogger *log.Entry
conf configuration.Config
cfgFlag = flag.String("config", "config.yaml", "path of the config file")
installFlag = flag.String("install", "", "path to the database creation file")
)
func initialize() {
flag.Parse()
cfg := *cfgFlag
var err error
conf, err = configuration.GetConfig(cfg)
if err != nil {
log.Fatal("config read error: ", err)
return
}
configuration.SetEnvironmentVariables(&conf)
logger, err := gostLog.InitializeLogger(file, conf.Logger.FileName, &log.TextFormatter{FullTimestamp: true}, conf.Logger.Verbose)
if err != nil {
log.Println("Error initializing logger, defaulting to stdout. Error: " + err.Error())
}
// Setting default fields for main logger
mainLogger = logger.WithFields(log.Fields{"package": "main"})
}
func main() {
initialize()
stop := make(chan os.Signal, 2)
signal.Notify(stop, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
go func() {
<-stop
// mainLogger.Info("GOST stopped gracefully")
cleanup()
os.Exit(1)
}()
mainLogger.Info("Starting GOST")
database := postgis.NewDatabase(
conf.Database.Host,
conf.Database.Port,
conf.Database.User,
conf.Database.Password,
conf.Database.Database,
conf.Database.Schema,
conf.Database.SSL,
conf.Database.MaxIdleConns,
conf.Database.MaxOpenConns,
conf.Server.MaxEntityResponse)
go database.Start()
// if install is supplied create database and close, if not start server
sqlFile := *installFlag
if len(sqlFile) != 0 {
createDatabase(database, sqlFile)
} else {
mqttClient = mqtt.CreateMQTTClient(conf.MQTT)
stAPI = api.NewAPI(database, conf, mqttClient)
if conf.MQTT.Enabled {
mqttClient.Start(&stAPI)
}
createAndStartServer(&stAPI)
}
}
func createDatabase(db models.Database, sqlFile string) {
mainLogger.Info("CREATING DATABASE")
err := db.CreateSchema(sqlFile)
if err != nil {
mainLogger.Fatal(err)
}
mainLogger.Info("Database created successfully, you can start your server now")
}
// createAndStartServer creates the GOST HTTPServer and starts it
func createAndStartServer(api *models.API) {
a := *api
a.Start()
config := a.GetConfig()
gostServer = http.CreateServer(
config.Server.Host,
config.Server.Port,
api,
config.Server.HTTPS,
config.Server.HTTPSCert,
config.Server.HTTPSKey)
gostServer.Start()
}
func cleanup() {
if gostServer != nil {
gostServer.Stop()
}
gostLog.CleanUp()
}