-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
325 lines (262 loc) · 7.23 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
package main
import (
"embed"
"flag"
"fmt"
"html/template"
"io"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/CyCoreSystems/audimance/agenda"
"github.com/CyCoreSystems/audimance/internal/osc"
"github.com/CyCoreSystems/audimance/showtime"
"github.com/labstack/echo-contrib/prometheus"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
prom "github.com/prometheus/client_golang/prometheus"
promauto "github.com/prometheus/client_golang/prometheus/promauto"
"github.com/labstack/gommon/log"
"golang.org/x/net/websocket"
)
//go:embed all:app/*
var content embed.FS
var keyFile string
var certFile string
// addr is the listen address.
var addr string
// qlabAddr is the listen address.
var qlabAddr string
// oscAddr is the destination address of the OSC server.
var oscAddr string
// oscRoomIndex is the index number of the room to be sent to the OSC server.
var oscRoomIndex int
// debug enables debug mode, which uses local files
// instead of bundled ones
var debug bool
var (
metricRoomEntry *prom.CounterVec
)
func init() {
metricRoomEntry = promauto.NewCounterVec(prom.CounterOpts{
Name: "audimance_room_load",
Help: "Total number of room loads",
}, []string{"room"})
}
// Template contains an HTML templates for the web service
type Template struct {
templates *template.Template
}
// Render adapts the native template to the echo web server renderer interface
func (t *Template) Render(w io.Writer, name string, data interface{}, ctx echo.Context) error {
return t.templates.ExecuteTemplate(w, name, data)
}
// CustomContext extends the Echo context to allow for custom data
type CustomContext struct {
echo.Context
Agenda *agenda.Agenda
ShowTime *showtime.Service
}
func init() {
flag.StringVar(&addr, "addr", ":9000", "TCP Address on which to listen for web requests")
flag.StringVar(&qlabAddr, "qlab", ":9001", "UDP Address on which to listen for QLab cues")
flag.StringVar(&keyFile, "key", "", "TLS key")
flag.StringVar(&certFile, "cert", "", "TLS certificate")
flag.BoolVar(&debug, "debug", false, "enable debug logging")
flag.StringVar(&oscAddr, "osc", "", "Address (<host>:<port>) of an OSC service to configure")
flag.IntVar(&oscRoomIndex, "oscroom", 0, "Index number of room to be used as the OSC room")
}
func main() {
flag.Parse()
// Read the Agenda
a, err := agenda.New("agenda.yaml")
if err != nil {
fmt.Printf("failed to read agenda: %s", err.Error())
os.Exit(1)
}
// Create web server
e := echo.New()
// Create the showtime service
svc := new(showtime.Service)
svc.Echo = e
go func() {
err := svc.Run(qlabAddr)
if err != nil {
fmt.Printf("showtime service died: %s", err.Error())
os.Exit(1)
}
}()
// Attach middleware
e.Use(func(h echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
c := &CustomContext{
Context: ctx,
Agenda: a,
ShowTime: svc,
}
return h(c)
}
})
e.Use(middleware.Logger())
e.Use(middleware.Recover())
p := prometheus.NewPrometheus("audimance", nil)
p.Use(e)
fList, err := content.ReadDir("app/_snowpack/pkg")
if err != nil {
log.Fatal("failed to read directory:", err)
}
for _, f := range fList {
log.Printf("file: %s", f.Name())
}
if debug {
e.Logger.SetLevel(log.DEBUG)
for _, room := range a.Rooms {
e.Logger.Debug("Room:", room.ID)
}
}
if oscAddr != "" {
if err := osc.SetupPositions(a, oscRoomIndex, oscAddr); err != nil {
log.Fatalf("failed to configure OSC positions: %w", err)
}
}
// Compile and attach templates
e.Renderer = &Template{
templates: template.Must(template.ParseGlob("views/*.html")),
}
// Attach handlers
// Handle the index
e.GET("/", func(c echo.Context) error {
return c.Render(200, "index.html", a)
})
// Serve internal javascript files
e.GET("/app/*", echo.WrapHandler(http.FileServer(http.FS(content))))
// Serve user-supplied assets
e.Static("/js", "js")
e.Static("/css", "css")
e.Static("/media", "media")
e.GET("/admin", admin)
e.GET("/live", live)
e.GET("/room/:id", enterRoom)
e.GET("/tracks/:id", roomTracks)
// command API for manually triggering cues --
// FIXME: this is unprotected, unauthenticated
e.PUT("/cues/:id", triggerCue)
// performanceTime provides a websocket connection which provides time tickers and cues based on realtime performance status
e.GET("/ws/performanceTime", performanceTime)
e.GET("/agenda.json", agendaJSON)
// Listen to OS kill signals
go func() {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
<-sigs
e.Logger.Info("exiting on signal")
os.Exit(100)
}()
// If we have TLS assets, start the TLS server
if certFile != "" && keyFile != "" {
e.Logger.Debug("listening on 443")
e.Logger.Fatal(e.StartTLS(":443", certFile, keyFile))
}
// Listen for connections
e.Logger.Debugf("listening on %s\n", addr)
e.Logger.Fatal(e.Start(addr))
}
func agendaJSON(c echo.Context) error {
ctx := c.(*CustomContext)
return ctx.JSON(200, ctx.Agenda)
}
func enterRoom(c echo.Context) error {
ctx := c.(*CustomContext)
// Find our room
id := ctx.Param("id")
var r *agenda.Room
for _, room := range ctx.Agenda.Rooms {
if room.ID == id {
r = room
break
}
}
if r == nil {
return ctx.String(http.StatusNotFound, "no such room")
}
data := struct {
Announcements []*agenda.Announcement `json:"announcements"`
Room *agenda.Room `json:"room"`
}{
Announcements: ctx.Agenda.Announcements,
Room: r,
}
metricRoomEntry.With(prom.Labels{
"room": "spatial",
}).Add(1)
return ctx.Render(200, "room.html", data)
}
func triggerCue(c echo.Context) error {
ctx := c.(*CustomContext)
id := ctx.Param("id")
var cue *agenda.Cue
for _, thisCue := range ctx.Agenda.Cues {
if thisCue.ID == id {
cue = thisCue
}
}
if cue == nil {
return ctx.String(http.StatusNotFound, "no such cue")
}
ctx.ShowTime.Trigger(cue.Data)
return ctx.String(http.StatusOK, fmt.Sprintf(`Cue "%s" triggered`, cue.Name))
}
func admin(c echo.Context) error {
ctx := c.(*CustomContext)
return c.Render(200, "admin.html", ctx.Agenda)
}
func live(c echo.Context) error {
ctx := c.(*CustomContext)
return c.Render(200, "live.html", ctx.Agenda)
}
func roomTracks(c echo.Context) error {
ctx := c.(*CustomContext)
// Find our room
id := ctx.Param("id")
var r *agenda.Room
for _, room := range ctx.Agenda.Rooms {
if room.ID == id {
r = room
break
}
}
if r == nil {
return ctx.String(http.StatusNotFound, "no such room")
}
data := struct {
Announcements []*agenda.Announcement `json:"announcements"`
Room *agenda.Room `json:"room"`
}{
Announcements: ctx.Agenda.Announcements,
Room: r,
}
metricRoomEntry.With(prom.Labels{
"room": "tracks",
}).Add(1)
return ctx.Render(200, "tracks.html", data)
}
func performanceTime(c echo.Context) error {
ctx := c.(*CustomContext)
websocket.Handler(func(ws *websocket.Conn) {
defer ws.Close()
// Create a subscription to the showtime service
sub := ctx.ShowTime.Subscribe()
defer sub.Cancel()
for {
// Process announcements
ann := <-sub.C
err := websocket.JSON.Send(ws, ann)
if err != nil {
ctx.Logger().Error(fmt.Errorf("failed to send announcement: %w", err))
break
}
}
}).ServeHTTP(ctx.Response(), ctx.Request())
return nil
}