2020-10-14 19:37:05 +00:00
|
|
|
// Package web serves the JavaScript player and WebRTC negotiation
|
2020-09-21 15:47:31 +00:00
|
|
|
package web
|
|
|
|
|
|
|
|
import (
|
2020-11-09 16:18:37 +00:00
|
|
|
"gitlab.crans.org/nounous/ghostream/stream/ovenmediaengine"
|
2020-09-21 15:47:31 +00:00
|
|
|
"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-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-10-19 17:57:04 +00:00
|
|
|
"gitlab.crans.org/nounous/ghostream/messaging"
|
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-09 20:06:30 +00:00
|
|
|
Enabled bool
|
2020-10-11 09:04:40 +00:00
|
|
|
CustomCSS string
|
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-13 15:12:19 +00:00
|
|
|
MapDomainToStream map[string]string
|
2020-10-11 20:02:20 +00:00
|
|
|
PlayerPoster string
|
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-11-12 00:42:28 +00:00
|
|
|
LegalMentionsEntity string
|
|
|
|
LegalMentionsAddress string
|
|
|
|
LegalMentionsFullAddress []string
|
|
|
|
LegalMentionsEmail string
|
2020-09-22 09:42:57 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 09:24:13 +00:00
|
|
|
var (
|
|
|
|
cfg *Options
|
|
|
|
|
2020-11-09 16:18:37 +00:00
|
|
|
omeCfg *ovenmediaengine.Options
|
|
|
|
|
2020-09-24 09:24:13 +00:00
|
|
|
// Preload templates
|
2020-09-28 15:36:40 +00:00
|
|
|
templates *template.Template
|
2020-10-17 10:38:18 +00:00
|
|
|
|
|
|
|
// Streams to get statistics
|
2020-10-19 17:57:04 +00:00
|
|
|
streams *messaging.Streams
|
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-11-09 16:18:37 +00:00
|
|
|
func Serve(s *messaging.Streams, c *Options, ome *ovenmediaengine.Options) {
|
2020-10-17 10:38:18 +00:00
|
|
|
streams = s
|
2020-09-24 09:24:13 +00:00
|
|
|
cfg = c
|
2020-11-09 16:18:37 +00:00
|
|
|
omeCfg = ome
|
2020-09-21 18:38:21 +00:00
|
|
|
|
2020-10-09 20:06:30 +00:00
|
|
|
if !cfg.Enabled {
|
2020-10-09 21:57:39 +00:00
|
|
|
// Web server is not enabled, ignore
|
2020-10-09 20:06:30 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
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-10-20 17:12:15 +00:00
|
|
|
mux.HandleFunc("/_ws/", websocketHandler)
|
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
|
|
|
}
|