-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (57 loc) · 1.61 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
package main
import (
"context"
"fmt"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/sirupsen/logrus"
"os"
"os/signal"
"somfyRtsGateway/core"
"somfyRtsGateway/homekit"
"somfyRtsGateway/somfy"
"somfyRtsGateway/web"
"somfyRtsGateway/web/views"
"time"
)
func main() {
ctx := core.InitApp()
ctrl, err := somfy.NewController(ctx)
if err != nil {
logrus.Errorf("error creating somfy-controller: %v", err)
}
defer ctrl.Close()
ctx.Controller = ctrl
homekit.StartHomeKitBridge(ctx, ctrl)
e := echo.New()
e.Renderer = web.NewTemplate(ctx)
e.Debug = true
//e.Logger.SetLevel(log.DEBUG)
//e.Use(middleware.Logger())
e.Use(middleware.CORS())
e.Use(core.CreateCtx(ctx))
root := e.Group(ctx.Config.BasePath)
root.GET("/", somfy.ListDevices(ctrl))
root.GET("/:device", somfy.GetDevice(ctrl))
root.POST("/:device/:cmd", somfy.Cmd)
root.Static("/static", "./web/static")
root.GET("/web", views.Index)
root.POST("/web/:device/:cmd", views.Cmd)
// Start server
go func() {
logrus.Infof("listening on :%s/%s", ctx.Config.Port, ctx.Config.BasePath)
if err := e.Start(fmt.Sprintf(":%s", ctx.Config.Port)); err != nil {
logrus.Info("shutting down...")
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
cancelCtx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := e.Shutdown(cancelCtx); err != nil {
e.Logger.Fatal(err)
}
}