rip out all auth stuff, add a containerfile
This commit is contained in:
parent
7fdd02bd33
commit
350fc3b339
27 changed files with 885 additions and 28 deletions
44
httpserver/browser-endpoints.go
Normal file
44
httpserver/browser-endpoints.go
Normal file
|
@ -0,0 +1,44 @@
|
|||
package httpserver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
|
||||
echo "github.com/labstack/echo/v4"
|
||||
|
||||
"git.janky.solutions/finn/lockserver/db"
|
||||
"git.janky.solutions/finn/lockserver/frontend"
|
||||
)
|
||||
|
||||
type browserEndpoints struct{}
|
||||
|
||||
type baseTemplateData struct {
|
||||
Username string
|
||||
UserDisplayName string
|
||||
}
|
||||
|
||||
func (b browserEndpoints) Register(e *echo.Echo) {
|
||||
e.GET("/", b.Index)
|
||||
e.StaticFS("/static", frontend.Static)
|
||||
}
|
||||
|
||||
type indexTemplateData struct {
|
||||
baseTemplateData
|
||||
|
||||
Locks []db.Lock
|
||||
}
|
||||
|
||||
func (browserEndpoints) Index(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{Locks: locks})
|
||||
}
|
|
@ -5,6 +5,7 @@ import (
|
|||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
echo "github.com/labstack/echo/v4"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
@ -25,9 +26,13 @@ func ListenAndServe(client *zwavejs.Client) {
|
|||
server.HideBanner = true
|
||||
server.HidePort = true
|
||||
server.HTTPErrorHandler = handleError
|
||||
server.Use(accessLogMiddleware)
|
||||
|
||||
browserEndpoints{}.Register(server)
|
||||
|
||||
openapi.RegisterHandlersWithBaseURL(server, lockserver{ZWaveJS: client}, "/api")
|
||||
|
||||
logrus.WithField("address", config.C.HTTPBind).Info("starting http server")
|
||||
err := server.Start(config.C.HTTPBind)
|
||||
if err != http.ErrServerClosed {
|
||||
logrus.WithError(err).Fatal("error starting http server")
|
||||
|
@ -40,7 +45,11 @@ func handleError(err error, c echo.Context) {
|
|||
return
|
||||
}
|
||||
|
||||
logrus.WithError(err).Error("error handling request")
|
||||
logrus.WithFields(logrus.Fields{
|
||||
"path": c.Request().URL.Path,
|
||||
"method": c.Request().Method,
|
||||
"error": err,
|
||||
}).Error("error handling request")
|
||||
_ = c.JSON(http.StatusInternalServerError, map[string]string{"error": "internal server error"})
|
||||
}
|
||||
|
||||
|
@ -51,3 +60,24 @@ func Shutdown(ctx context.Context) error {
|
|||
|
||||
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.Info("request handled")
|
||||
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
51
httpserver/user-codes-admin.go
Normal file
51
httpserver/user-codes-admin.go
Normal file
|
@ -0,0 +1,51 @@
|
|||
package httpserver
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"git.janky.solutions/finn/lockserver/db"
|
||||
"git.janky.solutions/finn/lockserver/openapi"
|
||||
echo "github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func (l lockserver) AddUserCode(c echo.Context) error {
|
||||
queries, dbc, err := db.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dbc.Close()
|
||||
ctx := c.Request().Context()
|
||||
|
||||
if _, err = queries.CreateUserCode(ctx, db.CreateUserCodeParams{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return c.NoContent(http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
func (l lockserver) GetAllUserCodes(c echo.Context) error {
|
||||
queries, dbc, err := db.Get()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dbc.Close()
|
||||
ctx := c.Request().Context()
|
||||
|
||||
resp := []openapi.UserCode{}
|
||||
|
||||
codes, err := queries.GetAllUserCodes(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
for _, code := range codes {
|
||||
resp = append(resp, code.OpenAPI())
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusOK, resp)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue