diff --git a/controllers/index.go b/controllers/index.go index fa42cd132..9a2e2dc8c 100644 --- a/controllers/index.go +++ b/controllers/index.go @@ -1,18 +1,36 @@ package controllers import ( + "fmt" + "log" "net/http" "path" + "text/template" + "github.com/gabek/owncast/config" "github.com/gabek/owncast/core" "github.com/gabek/owncast/router/middleware" "github.com/gabek/owncast/utils" + + "xojoc.pw/useragent" ) +type MetadataPage struct { + Config config.InstanceDetails + RequestedURL string +} + //IndexHandler handles the default index route func IndexHandler(w http.ResponseWriter, r *http.Request) { middleware.EnableCors(&w) + ua := useragent.Parse(r.UserAgent()) + + if ua.Type == useragent.Crawler && r.URL.Path == "/" { + handleScraperMetadataPage(w, r) + return + } + http.ServeFile(w, r, path.Join("webroot", r.URL.Path)) if path.Ext(r.URL.Path) == ".m3u8" { @@ -22,3 +40,19 @@ func IndexHandler(w http.ResponseWriter, r *http.Request) { core.SetClientActive(clientID) } } + +// Return a basic HTML page with server-rendered metadata from the config file +// to give to Opengraph clients and web scrapers (bots, web crawlers, etc). +func handleScraperMetadataPage(w http.ResponseWriter, r *http.Request) { + tmpl := template.Must(template.ParseFiles(path.Join("static", "metadata.html"))) + + fullURL := fmt.Sprintf("http://%s%s", r.Host, r.URL.Path) + metadata := MetadataPage{config.Config.InstanceDetails, fullURL} + + w.Header().Set("Content-Type", "text/html") + err := tmpl.Execute(w, metadata) + + if err != nil { + log.Panicln(err) + } +} diff --git a/go.mod b/go.mod index 3d526edeb..ab2cfd95a 100644 --- a/go.mod +++ b/go.mod @@ -18,4 +18,5 @@ require ( github.com/yutopp/go-rtmp v0.0.0-20191212152852-4e41609a99bb golang.org/x/net v0.0.0-20200602114024-627f9648deb9 gopkg.in/yaml.v2 v2.3.0 + xojoc.pw/useragent v0.0.0-20200116211053-1ec61d55e8fe ) diff --git a/go.sum b/go.sum index a5305f46c..b91feb203 100644 --- a/go.sum +++ b/go.sum @@ -1192,3 +1192,5 @@ rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0= rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA= sourcegraph.com/sourcegraph/go-diff v0.5.0/go.mod h1:kuch7UrkMzY0X+p9CRK03kfuPQ2zzQcaEFbx8wA8rck= sourcegraph.com/sqs/pbtypes v0.0.0-20180604144634-d3ebe8f20ae4/go.mod h1:ketZ/q3QxT9HOBeFhu6RdvsftgpsbFHBF5Cas6cDKZ0= +xojoc.pw/useragent v0.0.0-20200116211053-1ec61d55e8fe h1:KHyqPlOEFFT7OPh4WR7qFzNNndwj1VuwV+rZ+Tb3bio= +xojoc.pw/useragent v0.0.0-20200116211053-1ec61d55e8fe/go.mod h1:71om/Qz9HbIEjbUrkrzmJiF26FSh6tcwqSFdBBkLtJQ= diff --git a/static/metadata.html b/static/metadata.html new file mode 100644 index 000000000..3cf101b01 --- /dev/null +++ b/static/metadata.html @@ -0,0 +1,54 @@ + + + + + + + {{.Config.Name}} + + + + + + + + + + + + + + + + + + + + + + + +

{{.Config.Title}}

+

{{.Config.Name}}

+ +
+ +
+ +

{{.Config.Summary}}

+ + {{range .Config.Tags}} +
  • {{.}}
  • + {{end}} + +
    + +

    Connect with {{.Config.Name}} elsewhere by visiting:

    + + {{range .Config.SocialHandles}} +
  • {{.}}
  • + {{end}} + + + + \ No newline at end of file