-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
138 lines (115 loc) · 3.49 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
package main
import (
"context"
"flag"
"net/http"
"os"
"os/signal"
"github.com/gorilla/mux"
"github.com/jhuebert/levely/config"
"github.com/jhuebert/levely/controller"
"github.com/jhuebert/levely/repository"
"github.com/jhuebert/levely/service"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"gobot.io/x/gobot/drivers/i2c"
"gobot.io/x/gobot/platforms/raspi"
)
func main() {
var configPath string
var dbPath string
flag.StringVar(&configPath, "c", "", "path to the config file")
flag.StringVar(&dbPath, "d", "levely.db", "path to the database file")
flag.Parse()
logFormatter := new(logrus.TextFormatter)
logFormatter.TimestampFormat = "2006-01-02 15:04:05"
logFormatter.FullTimestamp = true
logrus.SetFormatter(logFormatter)
if configPath != "" {
logrus.Infof("reading config file: %v", configPath)
viper.SetConfigFile(configPath)
if err := viper.ReadInConfig(); err != nil {
logrus.Errorf("error reading config: %v", err)
flag.Usage()
return
}
level, err := logrus.ParseLevel(viper.GetString(config.LogLevel))
if err != nil {
logrus.Warnf("Invalid input log level \"%v\". Can be one of trace, debug, info, warn, error, fatal, panic. Setting log level to info", viper.GetString(config.LogLevel))
level = logrus.InfoLevel
}
logrus.SetLevel(level)
}
// Start a new router
router := mux.NewRouter()
router.Use(loggingMiddleware)
r, err := repository.New(dbPath)
if err != nil {
logrus.Errorf("could not open database: %v", err)
return
}
//TODO Migrate DB
d := getDriver()
s := service.New(r, d)
rc, err := controller.New(s)
if err != nil {
logrus.Errorf("error setting up controller: %v", err)
return
}
rc.RegisterRoutes(router)
// Define the REST server
srv := &http.Server{
Handler: router,
Addr: viper.GetString(config.ServerAddress),
WriteTimeout: viper.GetDuration(config.ServerWriteTimeout),
ReadTimeout: viper.GetDuration(config.ServerReadTimeout),
IdleTimeout: viper.GetDuration(config.ServerIdleTimeout),
}
// Run the server in a goroutine so that it doesn't block
go func() {
logrus.Info("starting server")
if err := srv.ListenAndServe(); err != nil {
logrus.Info(err)
}
}()
// Accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
// Block until we receive the signal
<-c
// Create a deadline to wait for
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration(config.ServerStopTimeout))
defer cancel()
// Wait for connections or force shutdown if the timeout expires
err = srv.Shutdown(ctx)
if err != nil {
logrus.Error(err)
}
logrus.Infof("closing database connection")
r.Close()
logrus.Info("shutting down")
os.Exit(0)
}
func getDriver() *i2c.MPU6050Driver {
logrus.Info("setting up accelerometer driver")
adaptor := raspi.NewAdaptor()
err := adaptor.Connect()
if err != nil {
logrus.Errorf("could not connect to Raspberry Pi I2C bus: %v", err)
return nil
}
d := i2c.NewMPU6050Driver(adaptor, i2c.WithBus(viper.GetInt(config.DeviceI2CBus)), i2c.WithAddress(viper.GetInt(config.AccelerometerI2CAddress)))
err = d.Start()
if err != nil {
logrus.Errorf("could not communicate with accelerometer: %v", err)
return nil
}
return d
}
func loggingMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
logrus.Debugf("%v %v", r.RequestURI, r.Method)
next.ServeHTTP(w, r)
})
}