server/receiver/resolution.go

49 lines
1.2 KiB
Go
Raw Normal View History

2016-11-19 15:43:57 +00:00
package main
import (
"log"
"net/http"
2022-05-24 15:36:33 +00:00
"net/url"
2016-11-19 15:43:57 +00:00
"path"
2022-05-24 15:36:33 +00:00
"strings"
2016-11-19 15:43:57 +00:00
"text/template"
)
var enableResolutionRoute bool = false
2022-05-24 15:36:33 +00:00
type ResolutionHandler struct{}
2016-11-19 15:43:57 +00:00
const resolutiontpl = `<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
2023-05-12 12:53:15 +00:00
<title>Résolution</title>
2016-11-19 15:43:57 +00:00
</head>
<body style="margin: 0">
<video src="{{.}}" controls width="100%" height="100%"></video>
</body>
</html>
`
func (s ResolutionHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if addr := r.Header.Get("X-Forwarded-For"); addr != "" {
r.RemoteAddr = addr
}
if !enableResolutionRoute {
log.Printf("UNHANDELED %s request from %s: /resolution%s [%s]\n", r.Method, r.RemoteAddr, r.URL.Path, r.UserAgent())
http.NotFound(w, r)
return
}
log.Printf("%s \"%s /resolution%s\" [%s]\n", r.RemoteAddr, r.Method, r.URL.Path, r.UserAgent())
2016-11-19 15:43:57 +00:00
w.Header().Set("Content-Type", "text/html")
if resolutionTmpl, err := template.New("resolution").Parse(resolutiontpl); err != nil {
log.Println("Cannot create template: ", err)
2022-05-24 15:36:33 +00:00
} else if err = resolutionTmpl.Execute(w, path.Join("/vids/", strings.Replace(url.PathEscape(r.URL.Path), "%2F", "/", -1))); err != nil {
2016-11-19 15:43:57 +00:00
log.Println("An error occurs during template execution: ", err)
}
}