mirror of
https://github.com/openziti/zrok.git
synced 2025-06-23 03:01:54 +02:00
very basic test endpoint (#36)
This commit is contained in:
parent
d671ec4c78
commit
5e4eee670f
@ -27,15 +27,15 @@
|
|||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: column;
|
flex-direction: column;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
height: 50vh;
|
height: 600px;
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
}
|
}
|
||||||
#banner h1 {
|
#banner h1 {
|
||||||
font-size: 64pt;
|
font-size: 64pt;
|
||||||
}
|
}
|
||||||
#info {
|
#info {
|
||||||
margin-left: 25px;
|
margin-left: 50px;
|
||||||
margin-right: 25px;
|
margin-right: 50px;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
@ -43,10 +43,11 @@
|
|||||||
<div id="root">
|
<div id="root">
|
||||||
<div id="banner">
|
<div id="banner">
|
||||||
<img src="ziggy.svg" width="200"/>
|
<img src="ziggy.svg" width="200"/>
|
||||||
<h1>zrok</h1>
|
<h1>zrok test endpoint</h1>
|
||||||
</div>
|
</div>
|
||||||
<div id="info">
|
<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>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
|
@ -2,9 +2,13 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"github.com/opentracing/opentracing-go/log"
|
||||||
"github.com/openziti-test-kitchen/zrok/cmd/zrok/endpoint_ui"
|
"github.com/openziti-test-kitchen/zrok/cmd/zrok/endpoint_ui"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
"html/template"
|
||||||
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
@ -20,6 +24,7 @@ var testCmd = &cobra.Command{
|
|||||||
type testEndpointCommand struct {
|
type testEndpointCommand struct {
|
||||||
address string
|
address string
|
||||||
port uint16
|
port uint16
|
||||||
|
t *template.Template
|
||||||
cmd *cobra.Command
|
cmd *cobra.Command
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,6 +35,10 @@ func newTestEndpointCommand() *testEndpointCommand {
|
|||||||
Args: cobra.ExactArgs(0),
|
Args: cobra.ExactArgs(0),
|
||||||
}
|
}
|
||||||
command := &testEndpointCommand{cmd: cmd}
|
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().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.Flags().Uint16VarP(&command.port, "port", "p", 9090, "The port for the HTTP listener")
|
||||||
cmd.Run = command.run
|
cmd.Run = command.run
|
||||||
@ -38,7 +47,39 @@ func newTestEndpointCommand() *testEndpointCommand {
|
|||||||
|
|
||||||
func (cmd *testEndpointCommand) run(_ *cobra.Command, _ []string) {
|
func (cmd *testEndpointCommand) run(_ *cobra.Command, _ []string) {
|
||||||
fs := http.FileServer(http.FS(endpoint_ui.FS))
|
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)
|
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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user