forked from Glimesh/broadcast-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
232 lines (188 loc) · 5.96 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
package main
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path"
"strings"
"crypto/tls"
"log"
"net/http"
"github.com/glimesh/broadcast-box/internal/webrtc"
"github.com/joho/godotenv"
)
const (
envFileProd = ".env.production"
envFileDev = ".env.development"
)
type (
whepLayerRequestJSON struct {
MediaId string `json:"mediaId"`
EncodingId string `json:"encodingId"`
}
)
func logHTTPError(w http.ResponseWriter, err string, code int) {
log.Println(err)
http.Error(w, err, code)
}
func whipHandler(res http.ResponseWriter, r *http.Request) {
streamKey := r.Header.Get("Authorization")
if streamKey == "" {
logHTTPError(res, "Authorization was not set", http.StatusBadRequest)
return
}
offer, err := io.ReadAll(r.Body)
if err != nil {
logHTTPError(res, err.Error(), http.StatusBadRequest)
return
}
answer, err := webrtc.WHIP(string(offer), streamKey)
if err != nil {
logHTTPError(res, err.Error(), http.StatusBadRequest)
return
}
res.Header().Add("Location", "/api/whip")
res.WriteHeader(http.StatusCreated)
fmt.Fprint(res, answer)
}
func whepHandler(res http.ResponseWriter, req *http.Request) {
streamKey := req.Header.Get("Authorization")
if streamKey == "" {
logHTTPError(res, "Authorization was not set", http.StatusBadRequest)
return
}
offer, err := io.ReadAll(req.Body)
if err != nil {
logHTTPError(res, err.Error(), http.StatusBadRequest)
return
}
answer, whepSessionId, err := webrtc.WHEP(string(offer), streamKey)
if err != nil {
logHTTPError(res, err.Error(), http.StatusBadRequest)
return
}
apiPath := req.Host + strings.TrimSuffix(req.URL.RequestURI(), "whep")
res.Header().Add("Link", `<`+apiPath+"sse/"+whepSessionId+`>; rel="urn:ietf:params:whep:ext:core:server-sent-events"; events="layers"`)
res.Header().Add("Link", `<`+apiPath+"layer/"+whepSessionId+`>; rel="urn:ietf:params:whep:ext:core:layer"`)
res.Header().Add("Location", "/api/whep")
res.WriteHeader(http.StatusCreated)
fmt.Fprint(res, answer)
}
func whepServerSentEventsHandler(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Content-Type", "text/event-stream")
res.Header().Set("Cache-Control", "no-cache")
res.Header().Set("Connection", "keep-alive")
vals := strings.Split(req.URL.RequestURI(), "/")
whepSessionId := vals[len(vals)-1]
layers, err := webrtc.WHEPLayers(whepSessionId)
if err != nil {
logHTTPError(res, err.Error(), http.StatusBadRequest)
return
}
fmt.Fprint(res, "event: layers\n")
fmt.Fprintf(res, "data: %s\n", string(layers))
fmt.Fprint(res, "\n\n")
}
func whepLayerHandler(res http.ResponseWriter, req *http.Request) {
var r whepLayerRequestJSON
if err := json.NewDecoder(req.Body).Decode(&r); err != nil {
logHTTPError(res, err.Error(), http.StatusBadRequest)
return
}
vals := strings.Split(req.URL.RequestURI(), "/")
whepSessionId := vals[len(vals)-1]
if err := webrtc.WHEPChangeLayer(whepSessionId, r.EncodingId); err != nil {
logHTTPError(res, err.Error(), http.StatusBadRequest)
return
}
}
type StreamStatus struct {
StreamKey string `json:"streamKey"`
}
func statusHandler(res http.ResponseWriter, req *http.Request) {
statuses := []StreamStatus{}
for _, s := range webrtc.GetAllStreams() {
statuses = append(statuses, StreamStatus{StreamKey: s})
}
if err := json.NewEncoder(res).Encode(statuses); err != nil {
logHTTPError(res, err.Error(), http.StatusBadRequest)
}
}
func indexHTMLWhenNotFound(fs http.FileSystem) http.Handler {
fileServer := http.FileServer(fs)
return http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
_, err := fs.Open(path.Clean(req.URL.Path)) // Do not allow path traversals.
if errors.Is(err, os.ErrNotExist) {
http.ServeFile(resp, req, "./web/build/index.html")
return
}
fileServer.ServeHTTP(resp, req)
})
}
func corsHandler(next func(w http.ResponseWriter, r *http.Request)) http.HandlerFunc {
return func(res http.ResponseWriter, req *http.Request) {
res.Header().Set("Access-Control-Allow-Origin", "*")
res.Header().Set("Access-Control-Allow-Methods", "*")
res.Header().Set("Access-Control-Allow-Headers", "*")
res.Header().Set("Access-Control-Expose-Headers", "*")
if req.Method != http.MethodOptions {
next(res, req)
}
}
}
func main() {
if os.Getenv("APP_ENV") == "production" {
log.Println("Loading `" + envFileProd + "`")
if err := godotenv.Load(envFileProd); err != nil {
log.Fatal(err)
}
} else {
log.Println("Loading `" + envFileDev + "`")
if err := godotenv.Load(envFileDev); err != nil {
log.Fatal(err)
}
}
webrtc.Configure()
if os.Getenv("ENABLE_HTTP_REDIRECT") != "" {
go func() {
redirectServer := &http.Server{
Addr: ":80",
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "https://"+r.Host+r.URL.String(), http.StatusMovedPermanently)
}),
}
log.Println("Running HTTP->HTTPS redirect Server at :80")
log.Fatal(redirectServer.ListenAndServe())
}()
}
mux := http.NewServeMux()
mux.Handle("/", indexHTMLWhenNotFound(http.Dir("./web/build")))
mux.HandleFunc("/api/whip", corsHandler(whipHandler))
mux.HandleFunc("/api/whep", corsHandler(whepHandler))
mux.HandleFunc("/api/status", corsHandler(statusHandler))
mux.HandleFunc("/api/sse/", corsHandler(whepServerSentEventsHandler))
mux.HandleFunc("/api/layer/", corsHandler(whepLayerHandler))
server := &http.Server{
Handler: mux,
Addr: os.Getenv("HTTP_ADDRESS"),
}
tlsKey := os.Getenv("SSL_KEY")
tlsCert := os.Getenv("SSL_CERT")
if tlsKey != "" && tlsCert != "" {
server.TLSConfig = &tls.Config{
Certificates: []tls.Certificate{},
}
cert, err := tls.LoadX509KeyPair(tlsCert, tlsKey)
if err != nil {
log.Fatal(err)
}
server.TLSConfig.Certificates = append(server.TLSConfig.Certificates, cert)
log.Println("Running HTTPS Server at `" + os.Getenv("HTTP_ADDRESS") + "`")
log.Fatal(server.ListenAndServeTLS("", ""))
} else {
log.Println("Running HTTP Server at `" + os.Getenv("HTTP_ADDRESS") + "`")
log.Fatal(server.ListenAndServe())
}
}