lockserver/httpserver/index.go

72 lines
2.3 KiB
Go
Raw Normal View History

package httpserver
import (
"database/sql"
"errors"
2024-04-25 19:38:24 +00:00
"net/http"
echo "github.com/labstack/echo/v4"
"git.janky.solutions/finn/lockserver/db"
"git.janky.solutions/finn/lockserver/frontend"
)
type baseTemplateData struct {
2024-04-25 19:38:24 +00:00
BaseURL string
Username string
UserDisplayName string
}
type indexTemplateData struct {
baseTemplateData
Locks []db.Lock
}
2024-04-25 19:38:24 +00:00
// 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"),
}
2024-04-25 19:38:24 +00:00
}
2024-04-25 19:38:24 +00:00
func indexHandler(c echo.Context) error {
queries, dbc, err := db.Get()
if err != nil {
return err
}
defer dbc.Close()
locks, err := queries.GetLocks(c.Request().Context())
if err != nil && !errors.Is(err, sql.ErrNoRows) {
return err
}
2024-04-25 19:38:24 +00:00
return frontend.Templates.ExecuteTemplate(c.Response(), "index.html", indexTemplateData{
baseTemplateData: getBaseTemplateData(c.Request().Header),
Locks: locks,
})
}