-
Notifications
You must be signed in to change notification settings - Fork 0
/
home_handler.go
84 lines (81 loc) · 2.48 KB
/
home_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"net"
"net/http"
"strings"
)
func (r *registry) homeHandler(w http.ResponseWriter, req *http.Request) {
// The "/" pattern matches everything, so we need to check
// that we're at the root here.
if req.URL.Path != "/" {
http.NotFound(w, req)
return
}
// We construct the data to be shown in the web page
type item struct {
OriginHost net.IP
OriginPort int
CacheHost net.IP
CachePort int
OriginName string
OriginPrefix string
CacheName string
}
ctx := []item{}
r.servicesMu.RLock()
// for every origin
for _, oSvc := range r.serviceMap["stream_publisher._tcp"] {
// XXX: (agnivade)- improve this logic later by sending the metadata
// in 2 separate items
streamMetadata := strings.Split(oSvc.Info, "_")
if len(streamMetadata) < 2 {
r.logger.Println("malformed stream publisher info: ", oSvc.Info)
continue
}
originName := streamMetadata[0]
prefix := streamMetadata[1]
// for every cache
for _, cSvc := range r.serviceMap["proxy_cache._tcp"] {
ctx = append(ctx, item{
OriginHost: oSvc.AddrV4,
OriginPort: oSvc.Port,
CacheHost: cSvc.AddrV4,
CachePort: cSvc.Port,
OriginName: originName,
OriginPrefix: prefix,
CacheName: cSvc.Info,
})
}
}
r.servicesMu.RUnlock()
err := r.homeTmpl.Execute(w, ctx)
if err != nil {
r.logger.Println(err)
}
}
const homePage = `<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Coordinator home page</title>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="description" content="video livestream url">
<meta name="viewport" content="width=device-width,initial-scale=1.0">
</head>
<body>
<h1>Home page</h1>
<p>
<h3>Live streams available right now:</h3>
{{range $item := .}}
<a href="/stream?prefix={{.OriginPrefix}}&origin={{.OriginHost}}:{{.OriginPort}}&old=true">[oldjs-pointing to origin]From {{.OriginName}}, cached at {{.CacheName}}</a>
<br />
<a href="/stream?prefix={{.OriginPrefix}}&cache={{.CacheHost}}:{{.CachePort}}&old=true">[oldjs-pointing to cache]From {{.OriginName}}, cached at {{.CacheName}}</a>
<br />
<a href="/stream?prefix={{.OriginPrefix}}&origin={{.OriginHost}}:{{.OriginPort}}&old=false">[newjs-pointing to origin]From {{.OriginName}}, cached at {{.CacheName}}</a>
<br />
<a href="/stream?prefix={{.OriginPrefix}}&cache={{.CacheHost}}:{{.CachePort}}&old=false">[newjs-pointing to cache]From {{.OriginName}}, cached at {{.CacheName}}</a>
{{end}}
</p>
</body>
</html>
`