forked from pydio/cells
/
action.livekit.go
80 lines (67 loc) · 1.72 KB
/
action.livekit.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
package modifiers
import (
"bytes"
"context"
"html/template"
"net/url"
"os"
"go.uber.org/zap"
"github.com/pydio/cells/common/caddy"
"github.com/pydio/cells/common/config"
"github.com/pydio/cells/common/log"
"github.com/pydio/cells/common/plugins"
)
var (
actionLivekitTemplate *template.Template
actionLivekitTemplateStr = `
{{if .LivekitURL}}
proxy /rtc {{.LivekitURL}} {
websocket
transparent
header_upstream Host {{if .Site.ExternalHost}}{{.Site.ExternalHost}}{{else}}{host}{{end}}
header_upstream X-Real-IP {remote}
header_upstream X-Forwarded-Proto {scheme}
}
{{end}}
`
)
type actionLivekitData struct {
LivekitURL string
Site caddy.SiteConf
}
func init() {
if os.Getenv("CELLS_ENABLE_LIVEKIT") != "" {
plugins.Register("main", func(ctx context.Context) {
caddy.RegisterPluginTemplate(
playLK,
[]string{"frontend", "plugin", "action.livekit"},
"/rtc",
)
tmpl, err := template.New("caddyfile").Funcs(caddy.FuncMap).Parse(actionLivekitTemplateStr)
if err != nil {
log.Fatal("Could not read template ", zap.Error(err))
}
actionLivekitTemplate = tmpl
})
}
}
func playLK(site ...caddy.SiteConf) (*bytes.Buffer, error) {
data := new(actionLivekitData)
if len(site) > 0 {
data.Site = site[0]
}
enabled := config.Get("frontend", "plugin", "action.livekit", config.KeyFrontPluginEnabled).Bool()
lkUrl := config.Get("frontend", "plugin", "action.livekit", "LK_WS_URL").String()
if enabled && lkUrl != "" {
u, e := url.Parse(lkUrl)
if e != nil {
return nil, e
}
data.LivekitURL = u.Host
}
buf := bytes.NewBuffer([]byte{})
if err := actionLivekitTemplate.Execute(buf, data); err != nil {
return nil, err
}
return buf, nil
}