-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
63 lines (53 loc) · 1.74 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
package main
import (
"github.com/jinzhu/gorm"
flag "github.com/ogier/pflag"
"github.com/sirupsen/logrus"
"github.com/jclebreton/opensirene/api/router"
"github.com/jclebreton/opensirene/conf"
"github.com/jclebreton/opensirene/database"
"github.com/jclebreton/opensirene/logic"
)
// This variable is overridden at compile time when using script/build.sh
var version = "dev"
var buildDate = "no build date"
func main() {
var err error
var config string
var fullImport bool
// Configuration
flag.StringVarP(&config, "config", "c", "conf.yml", "Path to the configuration file")
flag.BoolVarP(&fullImport, "drop", "", false, "Truncate database and run a full import")
flag.Parse()
if err = conf.Load(config); err != nil {
logrus.WithError(err).Fatal("Couldn't parse configuration")
}
// Init PGX database client
var pgxClient *database.PgxClient
if pgxClient, err = database.NewImportClient(); err != nil {
logrus.WithError(err).Fatal("Couldn't initialize PGX client")
}
defer pgxClient.Conn.Close()
// Full import
if fullImport {
if err = logic.ResetDatabase(pgxClient); err != nil {
logrus.WithError(err).Fatal("Couldn't reset database")
}
logrus.Info("Database has been reset to trigger automatic update")
}
// Start automatic updates
crontab := &logic.Crontab{PgxClient: pgxClient, Config: conf.C.Crontab}
go crontab.Start()
// Start API
var gormClient *gorm.DB
if gormClient, err = database.NewGORMClient(); err != nil {
logrus.WithError(err).Fatal("Couldn't initialize GORM")
}
defer func() {
err = gormClient.Close()
logrus.WithError(err).Fatal("Couldn't close GORM")
}()
if err = router.SetupAndRun(gormClient, version, buildDate); err != nil {
logrus.WithError(err).Fatal("Could not setup and run API")
}
}