2020-06-23 03:11:56 +02:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2022-09-11 00:37:07 +02:00
|
|
|
"encoding/json"
|
2022-12-13 01:57:17 +01:00
|
|
|
"fmt"
|
2020-06-23 03:11:56 +02:00
|
|
|
"net/http"
|
2020-10-03 23:35:03 +02:00
|
|
|
"path/filepath"
|
2022-09-11 00:37:07 +02:00
|
|
|
"strings"
|
2020-06-23 03:11:56 +02:00
|
|
|
|
2022-09-11 00:37:07 +02:00
|
|
|
"github.com/owncast/owncast/core/data"
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/router/middleware"
|
2022-09-11 00:37:07 +02:00
|
|
|
"github.com/owncast/owncast/static"
|
2020-10-05 19:07:09 +02:00
|
|
|
"github.com/owncast/owncast/utils"
|
2020-06-23 03:11:56 +02:00
|
|
|
)
|
|
|
|
|
2020-11-13 00:14:59 +01:00
|
|
|
// IndexHandler handles the default index route.
|
2020-06-23 03:11:56 +02:00
|
|
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
2021-10-01 21:40:13 +02:00
|
|
|
middleware.EnableCors(w)
|
2022-01-12 22:53:10 +01:00
|
|
|
|
2020-10-09 01:34:40 +02:00
|
|
|
isIndexRequest := r.URL.Path == "/" || filepath.Base(r.URL.Path) == "index.html" || filepath.Base(r.URL.Path) == ""
|
2020-06-30 09:06:33 +02:00
|
|
|
|
2021-08-31 23:49:04 +02:00
|
|
|
if utils.IsUserAgentAPlayer(r.UserAgent()) && isIndexRequest {
|
|
|
|
http.Redirect(w, r, "/hls/stream.m3u8", http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-09-30 23:14:39 +02:00
|
|
|
// Set a cache control max-age header
|
|
|
|
middleware.SetCachingHeaders(w, r)
|
|
|
|
|
2022-12-13 01:57:17 +01:00
|
|
|
nonceRandom, _ := utils.GenerateRandomString(5)
|
|
|
|
|
2021-08-31 04:43:28 +02:00
|
|
|
// Set our global HTTP headers
|
2022-12-13 01:57:17 +01:00
|
|
|
middleware.SetHeaders(w, fmt.Sprintf("nonce-%s", nonceRandom))
|
|
|
|
|
|
|
|
if isIndexRequest {
|
|
|
|
renderIndexHtml(w, nonceRandom)
|
|
|
|
return
|
|
|
|
}
|
2021-04-16 07:17:52 +02:00
|
|
|
|
2022-06-20 00:30:32 +02:00
|
|
|
serveWeb(w, r)
|
2020-06-30 09:06:33 +02:00
|
|
|
}
|
2022-09-11 00:37:07 +02:00
|
|
|
|
2022-12-13 01:57:17 +01:00
|
|
|
func renderIndexHtml(w http.ResponseWriter, nonce string) {
|
2022-09-11 00:37:07 +02:00
|
|
|
type serverSideContent struct {
|
|
|
|
Name string
|
|
|
|
Summary string
|
|
|
|
RequestedURL string
|
|
|
|
TagsString string
|
|
|
|
ThumbnailURL string
|
|
|
|
Thumbnail string
|
|
|
|
Image string
|
|
|
|
StatusJSON string
|
|
|
|
ServerConfigJSON string
|
2022-12-13 01:57:17 +01:00
|
|
|
Nonce string
|
2022-09-11 00:37:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
status := getStatusResponse()
|
|
|
|
sb, err := json.Marshal(status)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
config := getConfigResponse()
|
|
|
|
cb, err := json.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
content := serverSideContent{
|
|
|
|
Name: data.GetServerName(),
|
|
|
|
Summary: data.GetServerSummary(),
|
|
|
|
RequestedURL: data.GetServerURL(),
|
|
|
|
TagsString: strings.Join(data.GetServerMetadataTags(), ","),
|
2023-02-25 00:56:49 +01:00
|
|
|
ThumbnailURL: "/thumbnail.jpg",
|
|
|
|
Thumbnail: "/thumbnail.jpg",
|
2022-09-11 00:37:07 +02:00
|
|
|
Image: "/logo/external",
|
|
|
|
StatusJSON: string(sb),
|
|
|
|
ServerConfigJSON: string(cb),
|
2022-12-13 01:57:17 +01:00
|
|
|
Nonce: nonce,
|
2022-09-11 00:37:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
index, err := static.GetWebIndexTemplate()
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := index.Execute(w, content); err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
}
|