-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
86 lines (67 loc) · 2.75 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
package main
import (
"fmt"
"github.com/cecchisandrone/smarthome-server/influxdb"
"os"
"github.com/cecchisandrone/smarthome-server/config"
"github.com/cecchisandrone/smarthome-server/controller"
"github.com/cecchisandrone/smarthome-server/service"
"github.com/cecchisandrone/smarthome-server/authentication"
"github.com/cecchisandrone/smarthome-server/persistence"
"github.com/cecchisandrone/smarthome-server/scheduler"
"github.com/facebookgo/inject"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"gopkg.in/resty.v1"
"time"
)
func main() {
var g inject.Graph
// Prepare and inject dependencies
config.Init()
db := persistence.Init()
router := gin.Default()
// CORS config
config := cors.DefaultConfig()
config.AllowAllOrigins = true
config.AddAllowHeaders("Origin", "Content-Length", "Content-Type", "Authorization")
config.AddAllowMethods("PUT", "DELETE", "GET", "POST")
router.Use(cors.New(config))
// Resty config
// Retries are configured per client
resty.DefaultClient.SetTimeout(10 * time.Second)
influxdbClient := &influxdb.Client{}
influxdbClient.Init()
controllers := []controller.Controller{&controller.HealthCheck{}, &controller.Profile{}, &controller.Configuration{}, &controller.Camera{}, &controller.Authentication{}, &controller.Temperature{}, &controller.Raspsonar{}, &controller.Gate{}, &controller.Notification{}, &controller.Alarm{}, &controller.WellPump{}, &controller.RainGauge{}, &controller.Humidity{}, &controller.Inverter{}, &controller.Heater{}, &controller.PowerMeter{}, &controller.Relay{}, &controller.Rental{}}
services := []service.Service{&service.Profile{}, &service.Configuration{}, &service.Camera{}, &service.Temperature{}, &service.Raspsonar{}, &service.Gate{}, &service.Notification{}, &service.Alarm{}, &service.WellPump{}, &service.RainGauge{}, &service.Humidity{}, &service.Inverter{}, &service.Heater{}, &service.PowerMeter{}, &service.Relay{}, &service.Rental{}}
for _, c := range controllers {
g.Provide(&inject.Object{Value: c})
}
for _, s := range services {
g.Provide(&inject.Object{Value: s})
}
authMiddlewareFactory := &authentication.AuthMiddlewareFactory{}
schedulerManager := &scheduler.SchedulerManager{}
g.Provide(&inject.Object{Value: db})
g.Provide(&inject.Object{Value: router})
g.Provide(&inject.Object{Value: authMiddlewareFactory})
g.Provide(&inject.Object{Value: schedulerManager})
g.Provide(&inject.Object{Value: influxdbClient})
if err := g.Populate(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
// Init auth middleware
authMiddlewareFactory.Init()
// Init controller routes
for _, c := range controllers {
c.InitRoutes()
}
// Init services
for _, s := range services {
s.Init()
}
// Start task scheduler
schedulerManager.Start()
router.Run()
}