lockserver/httpserver/index.go
Finn f3fbd0a59f
All checks were successful
/ build-container (push) Successful in 7m25s
fix base url and read other headers
2024-04-25 12:38:24 -07:00

71 lines
2.3 KiB
Go

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