fix base url and read other headers

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

View file

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

View file

@ -2,16 +2,16 @@
<html> <html>
<head> <head>
<title>Lock Server</title> <title>Better Z-Wave Locks for Home Assistant</title>
<meta charset="utf-8" /> <meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no" /> <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> </head>
<body> <body>
<div id="wrapper"> <div id="wrapper">
<header> <header>
<h1>Lockserver</h1> <h1>Better Z-Wave Locks</h1>
<div id="user"></div> <div id="user"></div>
</header> </header>
<div id="main"> <div id="main">
@ -21,7 +21,7 @@
</ul> </ul>
</div> </div>
<footer> <footer>
<code>lockserver v0.x.x aaaaaa</code> <code>{{ version }}</code>
</footer> </footer>
</div> </div>
</body> </body>

View file

@ -3,15 +3,16 @@ package httpserver
import ( import (
"database/sql" "database/sql"
"errors" "errors"
"net/http"
echo "github.com/labstack/echo/v4" echo "github.com/labstack/echo/v4"
"github.com/sirupsen/logrus"
"git.janky.solutions/finn/lockserver/db" "git.janky.solutions/finn/lockserver/db"
"git.janky.solutions/finn/lockserver/frontend" "git.janky.solutions/finn/lockserver/frontend"
) )
type baseTemplateData struct { type baseTemplateData struct {
BaseURL string
Username string Username string
UserDisplayName string UserDisplayName string
} }
@ -22,11 +23,36 @@ type indexTemplateData struct {
Locks []db.Lock Locks []db.Lock
} }
func indexHandler(c echo.Context) error { // header=X-Forwarded-Proto value="[http]"
for k, v := range c.Request().Header { // header=Accept value="[text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8]"
logrus.WithFields(logrus.Fields{"header": k, "value": v}).Debug("request header") // 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() queries, dbc, err := db.Get()
if err != nil { if err != nil {
return err return err
@ -38,5 +64,8 @@ func indexHandler(c echo.Context) error {
return err 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,
})
} }