/
main.go
60 lines (50 loc) · 1.16 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
package main
import (
"os"
"os/signal"
"syscall"
_ "github.com/mattn/go-sqlite3"
"github.com/trtstm/budgetr/config"
"github.com/trtstm/budgetr/db"
"github.com/trtstm/budgetr/log"
)
func handleInterrupt(quit chan struct{}) {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
quit <- struct{}{}
}()
}
func main() {
if conf, err := config.NewConfigFromFile("./config.json"); err != nil {
log.Fatalf("Failed read configuration file: %v", err)
} else {
config.Config = conf
}
var logLevel log.Level
switch config.Config.LogLevel {
case "debug":
logLevel = log.DebugLevel
case "info":
logLevel = log.InfoLevel
case "warn":
logLevel = log.WarnLevel
case "error":
logLevel = log.ErrorLevel
default:
logLevel = log.InfoLevel
}
log.SetLevel(logLevel)
if err := db.SetupConnection(db.SQLITE, config.Config.Database); err != nil {
log.Fatalf("Failed to create connection to database: %v", err)
}
if err := db.SetupSchema(); err != nil {
log.Fatalf("Failed to initialize database schema: %v", err)
}
quit := make(chan struct{})
handleInterrupt(quit)
go startAPI()
<-quit
log.Info("Goodbye.")
}