fix base url and read other headers
All checks were successful
/ build-container (push) Successful in 7m25s

This commit is contained in:
Finn 2024-04-25 12:38:24 -07:00
parent d8ebb87191
commit f3fbd0a59f
3 changed files with 41 additions and 14 deletions

View file

@ -19,10 +19,8 @@ var (
Templates *template.Template
funcs = template.FuncMap{
"static": staticFn,
"staticCSS": func(filename string) (template.CSS, error) {
data, err := staticFn(filename)
return template.CSS(data), err
"version": func() string {
return "better-zwave-locks v0.x.x aaaaaaaa"
},
}
)

View file

@ -2,16 +2,16 @@
<html>
<head>
<title>Lock Server</title>
<title>Better Z-Wave Locks for Home Assistant</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" />
<style type="text/css">{{ staticCSS "main.css" }}</style>
<link rel="stylesheet" href="{{ .BaseURL }}/static/main.css" />
</head>
<body>
<div id="wrapper">
<header>
<h1>Lockserver</h1>
<h1>Better Z-Wave Locks</h1>
<div id="user"></div>
</header>
<div id="main">
@ -21,7 +21,7 @@
</ul>
</div>
<footer>
<code>lockserver v0.x.x aaaaaa</code>
<code>{{ version }}</code>
</footer>
</div>
</body>

View file

@ -3,15 +3,16 @@ package httpserver
import (
"database/sql"
"errors"
"net/http"
echo "github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"git.janky.solutions/finn/lockserver/db"
"git.janky.solutions/finn/lockserver/frontend"
)
type baseTemplateData struct {
BaseURL string
Username string
UserDisplayName string
}
@ -22,11 +23,36 @@ type indexTemplateData struct {
Locks []db.Lock
}
func indexHandler(c echo.Context) error {
for k, v := range c.Request().Header {
logrus.WithFields(logrus.Fields{"header": k, "value": v}).Debug("request header")
// header=X-Forwarded-Proto value="[http]"
// header=Accept value="[text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8]"
// header=Referer value="[https://ha.herzfeld.casa/hassio/store]"
// header=X-Forwarded-For value="[10.5.0.235, 10.5.1.245, 172.30.32.1]"
// header=X-Forwarded-Host value="[ha.herzfeld.casa]"
// header=Sec-Fetch-Mode value="[same-origin]"
// header=X-Ingress-Path value="[/api/hassio_ingress/VBVnN1BaXJun6ydy3xCuRoy1HyGH__attJ-gN193OU0]"
// header=Accept-Encoding value="[gzip, deflate, br]"
// header=Content-Length value="[0]"
// header=X-Remote-User-Id value="[a65ffe9ec5664336b4c08def63384aa0]"
// header=Accept-Language value="[en-US,en;q=0.5]"
// header=Upgrade-Insecure-Requests value="[1]"
// header=Sec-Fetch-Dest value="[empty]"
// header=X-Hass-Source value="[core.ingress]"
// header=X-Remote-User-Name value="[finn]"
// header=X-Remote-User-Display-Name value="[Finn]"
// header=Cookie value="[ingress_session=3dcd2006c013774475c5fec0a4301689803eacd343a3554ba2d920440bced0d973808524139c998d74b614e7950fe5f08cead286cde56e3541a3980b7d506a85]"
// header=User-Agent value="[Mozilla/5.0 (X11; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0]"
// header=Sec-Fetch-Site value="[same-origin]"
// header=Connection value="[close]"
func getBaseTemplateData(headers http.Header) baseTemplateData {
return baseTemplateData{
BaseURL: headers.Get("X-Ingress-Path"),
Username: headers.Get("X-Remote-User-Name"),
UserDisplayName: headers.Get("X-Remote-User-Display-Name"),
}
}
func indexHandler(c echo.Context) error {
queries, dbc, err := db.Get()
if err != nil {
return err
@ -38,5 +64,8 @@ func indexHandler(c echo.Context) error {
return err
}
return frontend.Templates.ExecuteTemplate(c.Response(), "index.html", indexTemplateData{Locks: locks})
return frontend.Templates.ExecuteTemplate(c.Response(), "index.html", indexTemplateData{
baseTemplateData: getBaseTemplateData(c.Request().Header),
Locks: locks,
})
}