very basic test endpoint (#36)

This commit is contained in:
Michael Quigley 2022-08-30 09:35:42 -04:00
parent d671ec4c78
commit 5e4eee670f
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
2 changed files with 48 additions and 6 deletions

View File

@ -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;
}
</style>
</head>
@ -43,10 +43,11 @@
<div id="root">
<div id="banner">
<img src="ziggy.svg" width="200"/>
<h1>zrok</h1>
<h1>zrok test endpoint</h1>
</div>
<div id="info">
<p>This is an HTTP server running at address:</p>
<h2>The Current Now is {{ .Now }}</h2>
<p>This host has these IP addresses: {{ .Ips }}</p>
</div>
</div>
</body>

View File

@ -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()
}
}
}
}