2020-06-23 04:11:56 +03:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
2022-09-11 01:37:07 +03:00
|
|
|
"encoding/json"
|
2022-12-13 03:57:17 +03:00
|
|
|
"fmt"
|
2020-06-23 04:11:56 +03:00
|
|
|
"net/http"
|
2020-10-04 00:35:03 +03:00
|
|
|
"path/filepath"
|
2022-09-11 01:37:07 +03:00
|
|
|
"strings"
|
2020-06-23 04:11:56 +03:00
|
|
|
|
2022-09-11 01:37:07 +03:00
|
|
|
"github.com/owncast/owncast/core/data"
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/router/middleware"
|
2022-09-11 01:37:07 +03:00
|
|
|
"github.com/owncast/owncast/static"
|
2020-10-05 20:07:09 +03:00
|
|
|
"github.com/owncast/owncast/utils"
|
2020-06-23 04:11:56 +03:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// IndexHandler handles the default index route.
|
2020-06-23 04:11:56 +03:00
|
|
|
func IndexHandler(w http.ResponseWriter, r *http.Request) {
|
2021-10-01 22:40:13 +03:00
|
|
|
middleware.EnableCors(w)
|
2022-01-13 00:53:10 +03:00
|
|
|
|
2020-10-09 02:34:40 +03:00
|
|
|
isIndexRequest := r.URL.Path == "/" || filepath.Base(r.URL.Path) == "index.html" || filepath.Base(r.URL.Path) == ""
|
2020-06-30 10:06:33 +03:00
|
|
|
|
2021-09-01 00:49:04 +03:00
|
|
|
if utils.IsUserAgentAPlayer(r.UserAgent()) && isIndexRequest {
|
|
|
|
http.Redirect(w, r, "/hls/stream.m3u8", http.StatusTemporaryRedirect)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-10-01 00:14:39 +03:00
|
|
|
// Set a cache control max-age header
|
|
|
|
middleware.SetCachingHeaders(w, r)
|
|
|
|
|
2022-12-13 03:57:17 +03:00
|
|
|
nonceRandom, _ := utils.GenerateRandomString(5)
|
|
|
|
|
2021-08-31 05:43:28 +03:00
|
|
|
// Set our global HTTP headers
|
2022-12-13 03:57:17 +03:00
|
|
|
middleware.SetHeaders(w, fmt.Sprintf("nonce-%s", nonceRandom))
|
|
|
|
|
|
|
|
if isIndexRequest {
|
|
|
|
renderIndexHtml(w, nonceRandom)
|
|
|
|
return
|
|
|
|
}
|
2021-04-16 08:17:52 +03:00
|
|
|
|
2022-06-20 01:30:32 +03:00
|
|
|
serveWeb(w, r)
|
2020-06-30 10:06:33 +03:00
|
|
|
}
|
2022-09-11 01:37:07 +03:00
|
|
|
|
2022-12-13 03:57:17 +03:00
|
|
|
func renderIndexHtml(w http.ResponseWriter, nonce string) {
|
2022-09-11 01:37:07 +03: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 03:57:17 +03:00
|
|
|
Nonce string
|
2022-09-11 01:37:07 +03: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 02:56:49 +03:00
|
|
|
ThumbnailURL: "/thumbnail.jpg",
|
|
|
|
Thumbnail: "/thumbnail.jpg",
|
2022-09-11 01:37:07 +03:00
|
|
|
Image: "/logo/external",
|
|
|
|
StatusJSON: string(sb),
|
|
|
|
ServerConfigJSON: string(cb),
|
2022-12-13 03:57:17 +03:00
|
|
|
Nonce: nonce,
|
2022-09-11 01:37:07 +03: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)
|
|
|
|
}
|
|
|
|
}
|