-
Notifications
You must be signed in to change notification settings - Fork 0
/
portal.go
144 lines (120 loc) · 3 KB
/
portal.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
package animportal
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path"
"sync/atomic"
"time"
"github.com/dmisol/animportal/defs"
"github.com/google/uuid"
"github.com/livekit/protocol/auth"
"github.com/valyala/fasthttp"
"gopkg.in/yaml.v2"
)
const (
lifetime = 2 * time.Hour
initJson = "init.json"
)
type AnimationPortal struct {
*defs.PortalConf
index int64
}
func NewPortal(name string) (ap *AnimationPortal, err error) {
var cont []byte
cont, err = ioutil.ReadFile(name)
if err != nil {
return
}
ap = &AnimationPortal{
PortalConf: &defs.PortalConf{},
}
if err = yaml.Unmarshal(cont, ap.PortalConf); err != nil {
log.Println("yaml err", name, err)
return
}
jn := initJson
if len(ap.PortalConf.DefaultInitJson) > 0 {
jn = ap.PortalConf.DefaultInitJson
}
if cont, err = ioutil.ReadFile(jn); err != nil {
log.Println("file read err", jn, err)
return
}
if err = json.Unmarshal(cont, &ap.PortalConf.InitialJson); err != nil {
log.Println("json err", jn, err)
return
}
ap.PortalConf.InitialJson.Ftar = ap.PortalConf.DefaultFtar
return
}
// /animate?name=xxx
// /animate?name=xxx&hall=yyy&ftar=zzz
// if body exists, it contains alternative InitialJson
func (ap *AnimationPortal) Handler(r *fasthttp.RequestCtx) {
name := string(r.FormValue("name"))
hall := string(r.FormValue("hall"))
ftar := string(r.FormValue("ftar"))
dummy := uuid.NewString()
if name == "" {
r.Error("no name", fasthttp.StatusBadRequest)
return
}
if hall == "" {
hall = "ft"
}
conf := *ap.PortalConf
body := r.Request.Body()
if len(body) > 0 {
if err := json.Unmarshal(body, &conf.InitialJson); err != nil {
r.Error("invalid body - initial json", fasthttp.StatusBadRequest)
return
}
}
if len(ftar) != 0 {
conf.InitialJson.Ftar = path.Join(path.Dir(conf.DefaultFtar), ftar)
}
x := atomic.AddInt64(&ap.index, 1)
conf.InitialJson.Dir = fmt.Sprintf("%s/%d", conf.Ram, x)
if err := os.MkdirAll(conf.InitialJson.Dir, 0777); err != nil {
r.Error("can't create ramfs folder", fasthttp.StatusInternalServerError)
return
}
t, err := ap.signToken(lifetime, name, "", dummy)
if err != nil {
r.Error("can't make token", fasthttp.StatusInternalServerError)
return
}
ctx, _ := context.WithTimeout(context.Background(), lifetime)
go func() {
p, err := ap.newUser(ctx, hall, dummy, name, conf)
if err != nil {
r.Error("can't start portal", fasthttp.StatusInternalServerError)
return
}
r.WriteString(t)
<-ctx.Done()
p.Println("portal closed")
}()
}
func (ap *AnimationPortal) signToken(lifetime time.Duration, uid, name, room string) (token string, err error) {
canPublish := true
canSubscribe := true
at := auth.NewAccessToken(ap.PortalConf.Key, ap.PortalConf.Secret)
grant := &auth.VideoGrant{
RoomJoin: true,
Room: room,
CanPublish: &canPublish,
CanSubscribe: &canSubscribe,
}
at.AddGrant(grant).SetIdentity(uid)
if len(name) > 0 {
at.SetName(name)
}
at.AddGrant(grant).SetValidFor(lifetime)
token, err = at.ToJWT()
return
}