2020-09-21 15:47:31 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
2020-09-28 15:36:40 +00:00
|
|
|
"io/ioutil"
|
2020-09-21 15:47:31 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-09-24 14:13:21 +00:00
|
|
|
"regexp"
|
2020-09-28 15:36:40 +00:00
|
|
|
"strings"
|
2020-09-21 17:59:41 +00:00
|
|
|
|
2020-09-28 15:36:40 +00:00
|
|
|
"github.com/markbates/pkger"
|
2020-09-23 11:52:12 +00:00
|
|
|
"github.com/pion/webrtc/v3"
|
2020-09-21 15:47:31 +00:00
|
|
|
)
|
|
|
|
|
2020-09-22 09:42:57 +00:00
|
|
|
// Options holds web package configuration
|
|
|
|
type Options struct {
|
2020-10-04 15:56:03 +00:00
|
|
|
Favicon string
|
|
|
|
Hostname string
|
2020-09-29 16:44:32 +00:00
|
|
|
ListenAddress string
|
|
|
|
Name string
|
2020-10-04 15:56:03 +00:00
|
|
|
OneStreamPerDomain bool
|
2020-10-04 09:45:16 +00:00
|
|
|
SRTServerPort string
|
2020-10-04 15:56:03 +00:00
|
|
|
STUNServers []string
|
2020-09-29 16:44:32 +00:00
|
|
|
ViewersCounterRefreshPeriod int
|
2020-10-04 15:56:03 +00:00
|
|
|
WidgetURL string
|
2020-09-22 09:42:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 09:24:13 +00:00
|
|
|
var (
|
|
|
|
cfg *Options
|
|
|
|
|
|
|
|
// WebRTC session description channels
|
2020-10-05 20:00:08 +00:00
|
|
|
remoteSdpChan chan struct {
|
|
|
|
StreamID string
|
|
|
|
RemoteDescription webrtc.SessionDescription
|
|
|
|
}
|
|
|
|
localSdpChan chan webrtc.SessionDescription
|
2020-09-24 09:24:13 +00:00
|
|
|
|
|
|
|
// Preload templates
|
2020-09-28 15:36:40 +00:00
|
|
|
templates *template.Template
|
2020-09-24 14:13:21 +00:00
|
|
|
|
|
|
|
// Precompile regex
|
|
|
|
validPath = regexp.MustCompile("^\\/[a-z0-9_-]*\\/?$")
|
2020-09-24 09:24:13 +00:00
|
|
|
)
|
2020-09-21 15:47:31 +00:00
|
|
|
|
2020-09-28 15:36:40 +00:00
|
|
|
// Load templates with pkger
|
|
|
|
// templates will be packed in the compiled binary
|
|
|
|
func loadTemplates() error {
|
|
|
|
templates = template.New("")
|
|
|
|
return pkger.Walk("/web/template", func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip non-templates
|
|
|
|
if info.IsDir() || !strings.HasSuffix(path, ".html") {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open file with pkger
|
|
|
|
f, err := pkger.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read and parse template
|
|
|
|
temp, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
templates, err = templates.Parse(string(temp))
|
|
|
|
return err
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-24 09:24:13 +00:00
|
|
|
// Serve HTTP server
|
2020-10-05 20:00:08 +00:00
|
|
|
func Serve(rSdpChan chan struct {
|
|
|
|
StreamID string
|
|
|
|
RemoteDescription webrtc.SessionDescription
|
|
|
|
}, lSdpChan chan webrtc.SessionDescription, c *Options) {
|
2020-09-24 09:24:13 +00:00
|
|
|
remoteSdpChan = rSdpChan
|
|
|
|
localSdpChan = lSdpChan
|
|
|
|
cfg = c
|
2020-09-21 18:38:21 +00:00
|
|
|
|
2020-09-28 15:36:40 +00:00
|
|
|
// Load templates
|
|
|
|
if err := loadTemplates(); err != nil {
|
|
|
|
log.Fatalln("Failed to load templates:", err)
|
|
|
|
}
|
|
|
|
|
2020-09-21 15:47:31 +00:00
|
|
|
// Set up HTTP router and server
|
2020-09-21 19:33:32 +00:00
|
|
|
mux := http.NewServeMux()
|
2020-09-24 09:24:13 +00:00
|
|
|
mux.HandleFunc("/", viewerHandler)
|
2020-09-28 16:06:10 +00:00
|
|
|
mux.Handle("/static/", staticHandler())
|
2020-09-29 16:03:28 +00:00
|
|
|
mux.HandleFunc("/_stats/", statisticsHandler)
|
2020-09-22 09:42:57 +00:00
|
|
|
log.Printf("HTTP server listening on %s", cfg.ListenAddress)
|
|
|
|
log.Fatal(http.ListenAndServe(cfg.ListenAddress, mux))
|
2020-09-21 15:47:31 +00:00
|
|
|
}
|