diff --git a/cmd/zrok/endpoint_ui/index.html b/cmd/zrok/endpoint_ui/index.html index c86c0ce1..0057ca68 100644 --- a/cmd/zrok/endpoint_ui/index.html +++ b/cmd/zrok/endpoint_ui/index.html @@ -27,15 +27,15 @@ display: flex; flex-direction: column; align-items: center; - height: 50vh; + height: 600px; justify-content: center; } #banner h1 { font-size: 64pt; } #info { - margin-left: 25px; - margin-right: 25px; + margin-left: 50px; + margin-right: 50px; } @@ -43,10 +43,11 @@
-

This is an HTTP server running at address:

+

The Current Now is {{ .Now }}

+

This host has these IP addresses: {{ .Ips }}

diff --git a/cmd/zrok/test.go b/cmd/zrok/test.go index 75e6e3bf..6db51a4b 100644 --- a/cmd/zrok/test.go +++ b/cmd/zrok/test.go @@ -2,9 +2,13 @@ package main import ( "fmt" + "github.com/opentracing/opentracing-go/log" "github.com/openziti-test-kitchen/zrok/cmd/zrok/endpoint_ui" "github.com/spf13/cobra" + "html/template" + "net" "net/http" + "time" ) func init() { @@ -20,6 +24,7 @@ var testCmd = &cobra.Command{ type testEndpointCommand struct { address string port uint16 + t *template.Template cmd *cobra.Command } @@ -30,6 +35,10 @@ func newTestEndpointCommand() *testEndpointCommand { Args: cobra.ExactArgs(0), } command := &testEndpointCommand{cmd: cmd} + var err error + if command.t, err = template.ParseFS(endpoint_ui.FS, "index.html"); err != nil { + panic(err) + } cmd.Flags().StringVarP(&command.address, "address", "a", "0.0.0.0", "The address for the HTTP listener") cmd.Flags().Uint16VarP(&command.port, "port", "p", 9090, "The port for the HTTP listener") cmd.Run = command.run @@ -38,7 +47,39 @@ func newTestEndpointCommand() *testEndpointCommand { func (cmd *testEndpointCommand) run(_ *cobra.Command, _ []string) { fs := http.FileServer(http.FS(endpoint_ui.FS)) - if err := http.ListenAndServe(fmt.Sprintf("%v:%d", cmd.address, cmd.port), fs); err != nil { + http.HandleFunc("/", cmd.handleIndex) + http.HandleFunc("/index.html", cmd.handleIndex) + http.Handle("/ziggy.svg", fs) + if err := http.ListenAndServe(fmt.Sprintf("%v:%d", cmd.address, cmd.port), nil); err != nil { panic(err) } } + +func (cmd *testEndpointCommand) handleIndex(w http.ResponseWriter, r *http.Request) { + ed := &endpointData{ + Now: time.Now(), + } + ed.getIps() + if err := cmd.t.Execute(w, ed); err != nil { + log.Error(err) + } +} + +type endpointData struct { + Now time.Time + Ips string +} + +func (ed *endpointData) getIps() { + addrs, err := net.InterfaceAddrs() + if err == nil { + for _, address := range addrs { + if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() { + if len(ed.Ips) != 0 { + ed.Ips += ", " + } + ed.Ips += ipnet.IP.String() + } + } + } +}