2024-04-09 23:18:43 +00:00
|
|
|
package httpserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"errors"
|
|
|
|
"net/http"
|
2024-04-24 00:34:57 +00:00
|
|
|
"time"
|
2024-04-09 23:18:43 +00:00
|
|
|
|
|
|
|
echo "github.com/labstack/echo/v4"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
|
|
"git.janky.solutions/finn/lockserver/config"
|
2024-04-24 07:15:12 +00:00
|
|
|
"git.janky.solutions/finn/lockserver/frontend"
|
2024-04-09 23:18:43 +00:00
|
|
|
"git.janky.solutions/finn/lockserver/zwavejs"
|
|
|
|
)
|
|
|
|
|
2024-11-23 03:50:18 +00:00
|
|
|
const contextKeyZWaveClient = "zwave-client"
|
2024-04-09 23:18:43 +00:00
|
|
|
|
2024-11-23 03:50:18 +00:00
|
|
|
var server *echo.Echo
|
2024-04-09 23:18:43 +00:00
|
|
|
|
|
|
|
func ListenAndServe(client *zwavejs.Client) {
|
|
|
|
server = echo.New()
|
|
|
|
server.HideBanner = true
|
|
|
|
server.HidePort = true
|
|
|
|
server.HTTPErrorHandler = handleError
|
2024-04-24 00:34:57 +00:00
|
|
|
server.Use(accessLogMiddleware)
|
2024-11-23 03:50:18 +00:00
|
|
|
server.RouteNotFound("/*", tmpl("404.html"))
|
2024-04-24 00:34:57 +00:00
|
|
|
|
2024-04-24 07:15:12 +00:00
|
|
|
server.StaticFS("/static", frontend.Static)
|
|
|
|
server.GET("/", indexHandler)
|
2024-11-23 03:50:18 +00:00
|
|
|
server.GET("/locks/:id", lockHandler)
|
|
|
|
server.GET("/add-code", tmpl("add-code.html"))
|
|
|
|
server.POST("/add-code", addCode)
|
2024-04-09 23:18:43 +00:00
|
|
|
|
2024-04-24 00:34:57 +00:00
|
|
|
logrus.WithField("address", config.C.HTTPBind).Info("starting http server")
|
2024-04-09 23:18:43 +00:00
|
|
|
err := server.Start(config.C.HTTPBind)
|
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
logrus.WithError(err).Fatal("error starting http server")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-11-23 03:50:18 +00:00
|
|
|
type errorTemplateData struct {
|
|
|
|
baseTemplateData
|
|
|
|
Error error
|
|
|
|
}
|
|
|
|
|
2024-04-09 23:18:43 +00:00
|
|
|
func handleError(err error, c echo.Context) {
|
|
|
|
if errors.Is(err, sql.ErrNoRows) {
|
2024-11-23 03:50:18 +00:00
|
|
|
err = frontend.Templates.ExecuteTemplate(c.Response(), "404.html", getBaseTemplateData(c.Request().Header))
|
|
|
|
if err == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-04-09 23:18:43 +00:00
|
|
|
}
|
|
|
|
|
2024-04-24 00:34:57 +00:00
|
|
|
logrus.WithFields(logrus.Fields{
|
|
|
|
"path": c.Request().URL.Path,
|
|
|
|
"method": c.Request().Method,
|
|
|
|
"error": err,
|
|
|
|
}).Error("error handling request")
|
2024-11-23 03:50:18 +00:00
|
|
|
frontend.Templates.ExecuteTemplate(c.Response(), "500.html", errorTemplateData{
|
|
|
|
baseTemplateData: getBaseTemplateData(c.Request().Header),
|
|
|
|
Error: err,
|
|
|
|
})
|
2024-04-09 23:18:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Shutdown(ctx context.Context) error {
|
|
|
|
if server == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return server.Shutdown(ctx)
|
|
|
|
}
|
2024-04-24 00:34:57 +00:00
|
|
|
|
|
|
|
func accessLogMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
start := time.Now()
|
|
|
|
err := next(c)
|
|
|
|
|
|
|
|
log := logrus.WithFields(logrus.Fields{
|
|
|
|
"method": c.Request().Method,
|
|
|
|
"path": c.Request().URL.Path,
|
|
|
|
"duration": time.Since(start),
|
|
|
|
"status": c.Response().Status,
|
|
|
|
"source": c.Request().RemoteAddr,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
log = log.WithError(err)
|
|
|
|
}
|
2024-04-25 19:06:20 +00:00
|
|
|
log.Debug("request handled")
|
2024-04-24 00:34:57 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-11-23 03:50:18 +00:00
|
|
|
|
|
|
|
func addZWaveClientToContextKey(client *zwavejs.Client) func(echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
c.Set(contextKeyZWaveClient, client)
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type baseTemplateData struct {
|
|
|
|
BaseURL string
|
|
|
|
Username string
|
|
|
|
UserDisplayName string
|
|
|
|
}
|
|
|
|
|
|
|
|
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 tmpl(filename string) echo.HandlerFunc {
|
|
|
|
return func(c echo.Context) error {
|
|
|
|
return frontend.Templates.ExecuteTemplate(c.Response(), filename, getBaseTemplateData(c.Request().Header))
|
|
|
|
}
|
|
|
|
}
|