/
echo.go
107 lines (88 loc) · 2.35 KB
/
echo.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
package echo
import (
"fmt"
"github.com/byuoitav/auth/wso2"
"github.com/byuoitav/shipyard"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
type Service struct {
disableAuth bool
opaAddress string
opaToken string
wso2CallbackURL string
wso2ClientID string
wso2ClientSecret string
wso2GatewayURL string
datastore shipyard.Datastore
logger shipyard.Logger
controlDocCompiler shipyard.RoomControlCompiler
}
func New(ds shipyard.Datastore, comp shipyard.RoomControlCompiler, opts ...Option) *Service {
s := Service{
datastore: ds,
disableAuth: true,
logger: &nullLogger{},
controlDocCompiler: comp,
}
// Apply options
for _, opt := range opts {
opt(&s)
}
return &s
}
func (s *Service) Serve(address string) error {
router := echo.New()
authRouter := router.Group("")
uiRouter := router.Group("")
// Enable auth if it hasn't been disabled
if !s.disableAuth {
if s.opaAddress == "" {
return fmt.Errorf("echo/Serve No OPA Address given")
}
client := wso2.Client{
CallbackURL: s.wso2CallbackURL,
ClientID: s.wso2ClientID,
ClientSecret: s.wso2ClientSecret,
GatewayURL: s.wso2GatewayURL,
}
authRouter.Use(
echo.WrapMiddleware(client.AuthCodeMiddleware),
s.authorize,
)
uiRouter.Use(
echo.WrapMiddleware(client.AuthCodeMiddleware),
s.authorize,
)
}
// Rooms
authRouter.GET("/rooms", s.getRoomList)
authRouter.GET("/rooms/:room_id", s.getRoom)
authRouter.PUT("/rooms/:room_id", s.saveRoom)
// Devices
authRouter.GET("/rooms/:room_id/devices", s.getRoomDevices)
authRouter.GET("/devices/:device_id", s.getDevice)
authRouter.PUT("/devices/:device_id", s.saveDevice)
// Device Templates
authRouter.GET("/device_templates", s.listDeviceTemplates)
authRouter.GET("/device_templates/:template_id", s.getDeviceTemplate)
// UI Config
authRouter.GET("/ui_config/:room_id", s.getUIConfig)
authRouter.PUT("/ui_config/:room_id", s.saveUIConfig)
// Config
authRouter.GET("/config/:config_id", s.getConfig)
// UI
uiRouter.Use(
middleware.StaticWithConfig(middleware.StaticConfig{
Root: "ui",
Index: "index.html",
HTML5: true,
Browse: true,
}),
)
err := router.Start(address)
if err != nil {
return fmt.Errorf("echo/Serve failed to start server: %w", err)
}
return nil
}