From f3fbd0a59f966dd3331f745316e6da946716d4c4 Mon Sep 17 00:00:00 2001 From: Finn Date: Thu, 25 Apr 2024 12:38:24 -0700 Subject: [PATCH] fix base url and read other headers --- frontend/frontend.go | 6 ++---- frontend/index.html | 8 ++++---- httpserver/index.go | 41 +++++++++++++++++++++++++++++++++++------ 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/frontend/frontend.go b/frontend/frontend.go index 2f67754..d8458c2 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -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" }, } ) diff --git a/frontend/index.html b/frontend/index.html index f44f800..e5419b9 100644 --- a/frontend/index.html +++ b/frontend/index.html @@ -2,16 +2,16 @@ - Lock Server + Better Z-Wave Locks for Home Assistant - +
-

Lockserver

+

Better Z-Wave Locks

@@ -21,7 +21,7 @@
diff --git a/httpserver/index.go b/httpserver/index.go index a9b02cd..20bbf9e 100644 --- a/httpserver/index.go +++ b/httpserver/index.go @@ -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, + }) }