53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"html/template"
|
||
|
"net/http"
|
||
|
|
||
|
"git.sr.ht/~blallo/logz/interface"
|
||
|
)
|
||
|
|
||
|
var uiTmpl = `
|
||
|
<!DOCTYPE html>
|
||
|
<html>
|
||
|
<head>
|
||
|
<title>WebSocket test</title>
|
||
|
<script>
|
||
|
const socket = new WebSocket("ws://{{ .BaseAddr }}/liveness");
|
||
|
|
||
|
socket.onmessage = (event) => {
|
||
|
console.log(event.data);
|
||
|
};
|
||
|
</script>
|
||
|
</head>
|
||
|
<body>
|
||
|
Open the console...
|
||
|
</body>
|
||
|
</html>
|
||
|
`
|
||
|
var tmpl *template.Template
|
||
|
|
||
|
func init() {
|
||
|
tmpl = template.Must(template.New("testUI").Parse(uiTmpl))
|
||
|
}
|
||
|
|
||
|
type testUIHandler struct {
|
||
|
baseAddr string
|
||
|
logger logz.Logger
|
||
|
}
|
||
|
|
||
|
func (h *testUIHandler) TestUI(w http.ResponseWriter, r *http.Request) {
|
||
|
h.logger.Debug(map[string]any{
|
||
|
"msg": "Serving test ui",
|
||
|
"context": "http",
|
||
|
})
|
||
|
|
||
|
if err := tmpl.ExecuteTemplate(w, "testUI", map[string]string{"BaseAddr": h.baseAddr}); err != nil {
|
||
|
h.logger.Err(map[string]any{
|
||
|
"msg": "Failed to render testUI template",
|
||
|
"context": "http",
|
||
|
"err": err.Error(),
|
||
|
})
|
||
|
}
|
||
|
}
|