lockserver/httpserver/server.go

130 lines
3.4 KiB
Go
Raw Normal View History

2024-04-09 23:18:43 +00:00
package httpserver
import (
"context"
"database/sql"
"errors"
"net/http"
"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
2024-11-23 05:15:21 +00:00
func ListenAndServe(zwaveClient *zwavejs.Client) {
2024-04-09 23:18:43 +00:00
server = echo.New()
server.HideBanner = true
server.HidePort = true
server.HTTPErrorHandler = handleError
2024-11-23 05:15:21 +00:00
server.Renderer = &Template{}
server.Use(accessLogMiddleware)
2024-11-23 03:50:18 +00:00
server.RouteNotFound("/*", tmpl("404.html"))
2024-04-24 07:15:12 +00:00
server.StaticFS("/static", frontend.Static)
server.GET("/", indexHandler)
2024-11-23 05:15:21 +00:00
server.GET("/locks/:lock", lockHandler)
server.GET("/locks/:lock/edit", lockEditHandler)
server.POST("/locks/:lock/edit", lockEditHandler)
server.GET("/locks/:lock/codes/:slot", lockCodeEditHandler)
server.POST("/locks/:lock/codes/:slot", lockCodeEditHandler, addZWaveClientToContextKey(zwaveClient))
2024-11-23 03:50:18 +00:00
server.GET("/add-code", tmpl("add-code.html"))
2024-11-23 05:15:21 +00:00
server.POST("/add-code", addCode, addZWaveClientToContextKey(zwaveClient))
2024-04-09 23:18:43 +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 05:15:21 +00:00
c.Response().WriteHeader(http.StatusNotFound)
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
}
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)
}
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)
}
log.Debug("request handled")
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))
}
}