From f0ce712f2ec3404a3e3b0e6c905666692640f735 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Sat, 13 Apr 2019 00:34:21 +0100
Subject: [PATCH] Fix the status template (and add a test)

---
 node/bindata.go           | 14 ++++-----
 node/node_test.go         | 61 +++++++++++++++++++++++++++++++++++++++
 node/server.go            |  7 ++++-
 node/templates/index.html | 12 ++++----
 4 files changed, 80 insertions(+), 14 deletions(-)

diff --git a/node/bindata.go b/node/bindata.go
index 8ec2a79e..ac86f062 100644
--- a/node/bindata.go
+++ b/node/bindata.go
@@ -272,21 +272,21 @@ var _templatesIndexHtml = []byte(`<!DOCTYPE html>
             {{$domain := .Domain}}
             {{range .Mounts}}
             <li>
-              <a href="http://{{$domain}}{{.Mount.Name}}"
+              <a href="http://{{$domain}}{{.Mount.Path}}"
                  {{if .Mount.RelayUrl}}
                  data-toggle="tooltip" data-delay="300" title="relay of {{.Mount.RelayUrl}}"
                  {{end}}
-                 >{{.Mount.Name}}</a>
-              <a href="http://{{$domain}}{{.Mount.Name}}.m3u">(m3u)</a>
+                 >{{.Mount.Path}}</a>
+              <a href="http://{{$domain}}{{.Mount.Path}}.m3u">(m3u)</a>
               <span class="badge">{{.Listeners}}</span>
               {{if .TransMounts}}
               <ul>
                 {{range .TransMounts}}
                 <li>
-                  <a href="http://{{$domain}}{{.Mount.Name}}"
+                  <a href="http://{{$domain}}{{.Mount.Path}}"
                      data-toggle="tooltip" data-delay="300" title="{{.Mount.Transcoding.String}}"
-                     >{{.Mount.Name}}</a>
-                  <a href="http://{{$domain}}{{.Mount.Name}}.m3u">(m3u)</a>
+                     >{{.Mount.Path}}</a>
+                  <a href="http://{{$domain}}{{.Mount.Path}}.m3u">(m3u)</a>
                   <span class="badge">{{.Listeners}}</span>
                 </li>
                 {{end}}
@@ -339,7 +339,7 @@ func templatesIndexHtml() (*asset, error) {
 		return nil, err
 	}
 
-	info := bindataFileInfo{name: "templates/index.html", size: 2567, mode: os.FileMode(420), modTime: time.Unix(1543702463, 0)}
+	info := bindataFileInfo{name: "templates/index.html", size: 2567, mode: os.FileMode(420), modTime: time.Unix(1555111991, 0)}
 	a := &asset{bytes: bytes, info: info}
 	return a, nil
 }
diff --git a/node/node_test.go b/node/node_test.go
index 653a230b..a218b751 100644
--- a/node/node_test.go
+++ b/node/node_test.go
@@ -5,11 +5,15 @@ import (
 	"fmt"
 	"log"
 	"net"
+	"net/http"
+	"net/http/httptest"
 	"os"
 	"testing"
 	"time"
 
+	"git.autistici.org/ale/autoradio"
 	pb "git.autistici.org/ale/autoradio/proto"
+	"github.com/golang/protobuf/proto"
 	"go.etcd.io/etcd/clientv3/concurrency"
 	"go.etcd.io/etcd/embed"
 	"go.etcd.io/etcd/etcdserver/api/v3client"
@@ -33,6 +37,7 @@ func TestNode(t *testing.T) {
 	if err != nil {
 		t.Fatalf("StartEtcd: %v", err)
 	}
+	defer e.Close()
 	<-e.Server.ReadyNotify()
 
 	cli := v3client.New(e.Server)
@@ -70,3 +75,59 @@ func TestNode(t *testing.T) {
 		n.Wait()
 	}
 }
+
+func TestNode_StatusPage(t *testing.T) {
+	cfg := embed.NewConfig()
+	cfg.Dir = "default.etcd"
+	defer os.RemoveAll(cfg.Dir)
+	e, err := embed.StartEtcd(cfg)
+	if err != nil {
+		t.Fatalf("StartEtcd: %v", err)
+	}
+	defer e.Close()
+	<-e.Server.ReadyNotify()
+
+	cli := v3client.New(e.Server)
+
+	session, _ := concurrency.NewSession(cli, concurrency.WithTTL(2))
+	ctx, cancel := context.WithCancel(context.Background())
+	defer cancel()
+
+	// Create a new Mount
+	m := &pb.Mount{
+		Path:           "/test.ogg",
+		SourceUsername: "user",
+		SourcePassword: "pass",
+	}
+	mdata, _ := proto.Marshal(m)
+	_, err = cli.Put(ctx, autoradio.MountPrefix+"test.ogg", string(mdata))
+	if err != nil {
+		t.Fatalf("Put error: %v", err)
+	}
+
+	n, err := New(
+		ctx,
+		session,
+		&fakeIcecast{},
+		"node1",
+		[]net.IP{net.ParseIP("127.0.0.1")},
+		net.ParseIP("127.0.0.1"),
+		4014,
+		"random",
+		0, 0,
+	)
+	if err != nil {
+		t.Fatalf("NewNode: %v", err)
+	}
+
+	httpSrv := httptest.NewServer(newHTTPHandler(n, 8080, "example.com"))
+
+	resp, err := http.Get(httpSrv.URL)
+	if err != nil {
+		t.Fatalf("http.Get error: %v", err)
+	}
+	defer resp.Body.Close()
+	if resp.StatusCode != 200 {
+		t.Fatalf("HTTP response: %s", resp.Status)
+	}
+}
diff --git a/node/server.go b/node/server.go
index a9d53b44..b9fc6a43 100644
--- a/node/server.go
+++ b/node/server.go
@@ -78,17 +78,22 @@ func (s *Server) Wait() error {
 
 // NewServer creates a new Server. Will use publicAddrs / peerAddr to
 // build all the necessary addr/port combinations.
+//
+// The main http handler will bind on all available interfaces. The
+// DNS servers will bind only to the publicAddrs (both TCP and
+// UDP). The metrics and the status services, which are internal, will
+// bind on peerAddr.
 func NewServer(n *Node, domain string, nameservers []string, publicAddrs []net.IP, peerAddr net.IP, httpPort, dnsPort, gossipPort, icecastPort, metricsPort int) *Server {
 	httpHandler := newHTTPHandler(n, icecastPort, domain)
 	dnsHandler := newDNSHandler(n, domain, nameservers)
 
 	servers := []genericServer{
 		newStatusServer(mkaddr(peerAddr, gossipPort), n.statusMgr),
+		newHTTPServer("main", fmt.Sprintf(":%d", httpPort), httpHandler),
 		newHTTPServer("metrics", fmt.Sprintf(":%d", metricsPort), newMetricsHandler()),
 	}
 	for _, ip := range publicAddrs {
 		servers = append(servers,
-			newHTTPServer("main", mkaddr(ip, httpPort), httpHandler),
 			newDNSServer("dns(udp)", mkaddr(ip, dnsPort), "udp", dnsHandler),
 			newDNSServer("dns(tcp)", mkaddr(ip, dnsPort), "tcp", dnsHandler),
 		)
diff --git a/node/templates/index.html b/node/templates/index.html
index 1b95732d..cfcd9052 100644
--- a/node/templates/index.html
+++ b/node/templates/index.html
@@ -25,21 +25,21 @@
             {{$domain := .Domain}}
             {{range .Mounts}}
             <li>
-              <a href="http://{{$domain}}{{.Mount.Name}}"
+              <a href="http://{{$domain}}{{.Mount.Path}}"
                  {{if .Mount.RelayUrl}}
                  data-toggle="tooltip" data-delay="300" title="relay of {{.Mount.RelayUrl}}"
                  {{end}}
-                 >{{.Mount.Name}}</a>
-              <a href="http://{{$domain}}{{.Mount.Name}}.m3u">(m3u)</a>
+                 >{{.Mount.Path}}</a>
+              <a href="http://{{$domain}}{{.Mount.Path}}.m3u">(m3u)</a>
               <span class="badge">{{.Listeners}}</span>
               {{if .TransMounts}}
               <ul>
                 {{range .TransMounts}}
                 <li>
-                  <a href="http://{{$domain}}{{.Mount.Name}}"
+                  <a href="http://{{$domain}}{{.Mount.Path}}"
                      data-toggle="tooltip" data-delay="300" title="{{.Mount.Transcoding.String}}"
-                     >{{.Mount.Name}}</a>
-                  <a href="http://{{$domain}}{{.Mount.Name}}.m3u">(m3u)</a>
+                     >{{.Mount.Path}}</a>
+                  <a href="http://{{$domain}}{{.Mount.Path}}.m3u">(m3u)</a>
                   <span class="badge">{{.Listeners}}</span>
                 </li>
                 {{end}}
-- 
GitLab