-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.go
50 lines (43 loc) · 1.25 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
package main
import (
"fmt"
"net/http"
"github.com/rs/cors"
"github.com/sirupsen/logrus"
"github.com/suyashkumar/auth"
"github.com/suyashkumar/conduit/config"
db2 "github.com/suyashkumar/conduit/db"
"github.com/suyashkumar/conduit/device"
"github.com/suyashkumar/conduit/log"
"github.com/suyashkumar/conduit/routes"
)
func main() {
log.Configure()
d := device.NewHandler()
db, err := db2.NewHandler(config.Get(config.DBConnString))
if err != nil {
logrus.WithError(err).WithField("DBConnString", config.Get(config.DBConnString)).Fatal("Could not connect to DB")
}
a, err := auth.NewAuthenticatorFromGORM(db.GetDB(), []byte(config.Get(config.SigningKey)))
if err != nil {
logrus.WithError(err).Fatal("Could not connect to or init database")
}
r := routes.Build(d, db, a)
handler := cors.Default().Handler(r)
p := fmt.Sprintf(":%s", config.Get(config.Port))
if config.Get(config.UseSSL) == "false" {
logrus.WithField("port", p).Info("Serving without SSL")
err := http.ListenAndServe(p, handler)
logrus.Fatal(err)
} else {
logrus.Info("Serving with SSL")
err := http.ListenAndServeTLS(
p,
config.Get(config.CertKey),
config.Get(config.PrivKey),
handler,
)
// TODO: reroute http requests to https
logrus.Fatal(err)
}
}