Skip to content

Commit

Permalink
Add hx-boost to dashboard to prevent downloading innecesary html
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardolat committed Sep 5, 2024
1 parent cce4034 commit 5093aa5
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 61 deletions.
23 changes: 13 additions & 10 deletions internal/view/middleware/inject_reqctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import (
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
"github.com/eduardolat/pgbackweb/internal/logger"
"github.com/eduardolat/pgbackweb/internal/view/reqctx"
"github.com/eduardolat/pgbackweb/internal/view/web/htmx"
"github.com/labstack/echo/v4"
)

func (m *Middleware) InjectReqctx(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
reqCtx := reqctx.Ctx{
IsHTMXBoosted: htmx.ServerIsBoosted(c),
}

found, user, err := m.servs.AuthService.GetUserFromSessionCookie(c)
if err != nil {
logger.Error("failed to get user from session cookie", logger.KV{
Expand All @@ -21,21 +26,19 @@ func (m *Middleware) InjectReqctx(next echo.HandlerFunc) echo.HandlerFunc {
return c.String(http.StatusInternalServerError, "Internal server error")
}

if !found {
return next(c)
}

reqctx.SetCtx(c, reqctx.Ctx{
IsAuthed: true,
SessionID: user.SessionID,
User: dbgen.User{
if found {
reqCtx.IsAuthed = true
reqCtx.SessionID = user.SessionID
reqCtx.User = dbgen.User{
ID: user.ID,
Name: user.Name,
Email: user.Email,
CreatedAt: user.CreatedAt,
UpdatedAt: user.UpdatedAt,
},
})
}
}

reqctx.SetCtx(c, reqCtx)
return next(c)
}
}
7 changes: 4 additions & 3 deletions internal/view/reqctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ const (

// Ctx represents the values passed through a single request context.
type Ctx struct {
IsAuthed bool
SessionID uuid.UUID
User dbgen.User
IsHTMXBoosted bool
IsAuthed bool
SessionID uuid.UUID
User dbgen.User
}

// SetCtx inserts values into the Echo request context.
Expand Down
8 changes: 5 additions & 3 deletions internal/view/web/dashboard/about/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/eduardolat/pgbackweb/internal/config"
"github.com/eduardolat/pgbackweb/internal/util/echoutil"
"github.com/eduardolat/pgbackweb/internal/view/reqctx"
"github.com/eduardolat/pgbackweb/internal/view/web/component"
"github.com/eduardolat/pgbackweb/internal/view/web/layout"
"github.com/labstack/echo/v4"
Expand All @@ -13,10 +14,11 @@ import (
)

func (h *handlers) indexPageHandler(c echo.Context) error {
return echoutil.RenderGomponent(c, http.StatusOK, indexPage())
reqCtx := reqctx.GetCtx(c)
return echoutil.RenderGomponent(c, http.StatusOK, indexPage(reqCtx))
}

func indexPage() gomponents.Node {
func indexPage(reqCtx reqctx.Ctx) gomponents.Node {
content := []gomponents.Node{
component.H1Text("About PG Back Web"),
component.H2Text(config.Version),
Expand Down Expand Up @@ -80,7 +82,7 @@ func indexPage() gomponents.Node {
),
}

return layout.Dashboard(layout.DashboardParams{
return layout.Dashboard(reqCtx, layout.DashboardParams{
Title: "About",
Body: content,
})
Expand Down
8 changes: 5 additions & 3 deletions internal/view/web/dashboard/backups/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/eduardolat/pgbackweb/internal/util/echoutil"
"github.com/eduardolat/pgbackweb/internal/view/reqctx"
"github.com/eduardolat/pgbackweb/internal/view/web/component"
"github.com/eduardolat/pgbackweb/internal/view/web/htmx"
"github.com/eduardolat/pgbackweb/internal/view/web/layout"
Expand All @@ -13,10 +14,11 @@ import (
)

func (h *handlers) indexPageHandler(c echo.Context) error {
return echoutil.RenderGomponent(c, http.StatusOK, indexPage())
reqCtx := reqctx.GetCtx(c)
return echoutil.RenderGomponent(c, http.StatusOK, indexPage(reqCtx))
}

func indexPage() gomponents.Node {
func indexPage(reqCtx reqctx.Ctx) gomponents.Node {
content := []gomponents.Node{
html.Div(
html.Class("flex justify-between items-start"),
Expand Down Expand Up @@ -63,7 +65,7 @@ func indexPage() gomponents.Node {
}),
}

return layout.Dashboard(layout.DashboardParams{
return layout.Dashboard(reqCtx, layout.DashboardParams{
Title: "Backups",
Body: content,
})
Expand Down
8 changes: 5 additions & 3 deletions internal/view/web/dashboard/databases/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/eduardolat/pgbackweb/internal/util/echoutil"
"github.com/eduardolat/pgbackweb/internal/view/reqctx"
"github.com/eduardolat/pgbackweb/internal/view/web/component"
"github.com/eduardolat/pgbackweb/internal/view/web/htmx"
"github.com/eduardolat/pgbackweb/internal/view/web/layout"
Expand All @@ -13,10 +14,11 @@ import (
)

func (h *handlers) indexPageHandler(c echo.Context) error {
return echoutil.RenderGomponent(c, http.StatusOK, indexPage())
reqCtx := reqctx.GetCtx(c)
return echoutil.RenderGomponent(c, http.StatusOK, indexPage(reqCtx))
}

func indexPage() gomponents.Node {
func indexPage(reqCtx reqctx.Ctx) gomponents.Node {
content := []gomponents.Node{
html.Div(
html.Class("flex justify-between items-start"),
Expand Down Expand Up @@ -55,7 +57,7 @@ func indexPage() gomponents.Node {
}),
}

return layout.Dashboard(layout.DashboardParams{
return layout.Dashboard(reqCtx, layout.DashboardParams{
Title: "Databases",
Body: content,
})
Expand Down
8 changes: 5 additions & 3 deletions internal/view/web/dashboard/destinations/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/eduardolat/pgbackweb/internal/util/echoutil"
"github.com/eduardolat/pgbackweb/internal/view/reqctx"
"github.com/eduardolat/pgbackweb/internal/view/web/component"
"github.com/eduardolat/pgbackweb/internal/view/web/htmx"
"github.com/eduardolat/pgbackweb/internal/view/web/layout"
Expand All @@ -13,10 +14,11 @@ import (
)

func (h *handlers) indexPageHandler(c echo.Context) error {
return echoutil.RenderGomponent(c, http.StatusOK, indexPage())
reqCtx := reqctx.GetCtx(c)
return echoutil.RenderGomponent(c, http.StatusOK, indexPage(reqCtx))
}

func indexPage() gomponents.Node {
func indexPage(reqCtx reqctx.Ctx) gomponents.Node {
content := []gomponents.Node{
html.Div(
html.Class("flex justify-between items-start"),
Expand Down Expand Up @@ -64,7 +66,7 @@ func indexPage() gomponents.Node {
}),
}

return layout.Dashboard(layout.DashboardParams{
return layout.Dashboard(reqCtx, layout.DashboardParams{
Title: "S3 Destinations",
Body: content,
})
Expand Down
9 changes: 6 additions & 3 deletions internal/view/web/dashboard/executions/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/eduardolat/pgbackweb/internal/util/echoutil"
"github.com/eduardolat/pgbackweb/internal/util/strutil"
"github.com/eduardolat/pgbackweb/internal/validate"
"github.com/eduardolat/pgbackweb/internal/view/reqctx"
"github.com/eduardolat/pgbackweb/internal/view/web/component"
"github.com/eduardolat/pgbackweb/internal/view/web/htmx"
"github.com/eduardolat/pgbackweb/internal/view/web/layout"
Expand All @@ -22,6 +23,8 @@ type execsQueryData struct {
}

func (h *handlers) indexPageHandler(c echo.Context) error {
reqCtx := reqctx.GetCtx(c)

var queryData execsQueryData
if err := c.Bind(&queryData); err != nil {
return c.String(http.StatusBadRequest, err.Error())
Expand All @@ -30,10 +33,10 @@ func (h *handlers) indexPageHandler(c echo.Context) error {
return c.String(http.StatusBadRequest, err.Error())
}

return echoutil.RenderGomponent(c, http.StatusOK, indexPage(queryData))
return echoutil.RenderGomponent(c, http.StatusOK, indexPage(reqCtx, queryData))
}

func indexPage(queryData execsQueryData) gomponents.Node {
func indexPage(reqCtx reqctx.Ctx, queryData execsQueryData) gomponents.Node {
content := []gomponents.Node{
component.H1Text("Executions"),
component.CardBox(component.CardBoxParams{
Expand Down Expand Up @@ -84,7 +87,7 @@ func indexPage(queryData execsQueryData) gomponents.Node {
}),
}

return layout.Dashboard(layout.DashboardParams{
return layout.Dashboard(reqCtx, layout.DashboardParams{
Title: "Executions",
Body: content,
})
Expand Down
8 changes: 4 additions & 4 deletions internal/view/web/dashboard/profile/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,22 @@ func (h *handlers) indexPageHandler(c echo.Context) error {
}

return echoutil.RenderGomponent(
c, http.StatusOK, indexPage(reqCtx.User, sessions),
c, http.StatusOK, indexPage(reqCtx, sessions),
)
}

func indexPage(user dbgen.User, sessions []dbgen.Session) gomponents.Node {
func indexPage(reqCtx reqctx.Ctx, sessions []dbgen.Session) gomponents.Node {
content := []gomponents.Node{
component.H1Text("Profile"),

html.Div(
html.Class("mt-4 grid grid-cols-2 gap-4"),
html.Div(updateUserForm(user)),
html.Div(updateUserForm(reqCtx.User)),
html.Div(closeAllSessionsForm(sessions)),
),
}

return layout.Dashboard(layout.DashboardParams{
return layout.Dashboard(reqCtx, layout.DashboardParams{
Title: "Profile",
Body: content,
})
Expand Down
9 changes: 6 additions & 3 deletions internal/view/web/dashboard/restorations/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/eduardolat/pgbackweb/internal/util/echoutil"
"github.com/eduardolat/pgbackweb/internal/util/strutil"
"github.com/eduardolat/pgbackweb/internal/validate"
"github.com/eduardolat/pgbackweb/internal/view/reqctx"
"github.com/eduardolat/pgbackweb/internal/view/web/component"
"github.com/eduardolat/pgbackweb/internal/view/web/htmx"
"github.com/eduardolat/pgbackweb/internal/view/web/layout"
Expand All @@ -21,6 +22,8 @@ type resQueryData struct {
}

func (h *handlers) indexPageHandler(c echo.Context) error {
reqCtx := reqctx.GetCtx(c)

var queryData resQueryData
if err := c.Bind(&queryData); err != nil {
return c.String(http.StatusBadRequest, err.Error())
Expand All @@ -29,10 +32,10 @@ func (h *handlers) indexPageHandler(c echo.Context) error {
return c.String(http.StatusBadRequest, err.Error())
}

return echoutil.RenderGomponent(c, http.StatusOK, indexPage(queryData))
return echoutil.RenderGomponent(c, http.StatusOK, indexPage(reqCtx, queryData))
}

func indexPage(queryData resQueryData) gomponents.Node {
func indexPage(reqCtx reqctx.Ctx, queryData resQueryData) gomponents.Node {
content := []gomponents.Node{
component.H1Text("Restorations"),
component.CardBox(component.CardBoxParams{
Expand Down Expand Up @@ -79,7 +82,7 @@ func indexPage(queryData resQueryData) gomponents.Node {
}),
}

return layout.Dashboard(layout.DashboardParams{
return layout.Dashboard(reqCtx, layout.DashboardParams{
Title: "Restorations",
Body: content,
})
Expand Down
13 changes: 8 additions & 5 deletions internal/view/web/dashboard/summary/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
lucide "github.com/eduardolat/gomponents-lucide"
"github.com/eduardolat/pgbackweb/internal/database/dbgen"
"github.com/eduardolat/pgbackweb/internal/util/echoutil"
"github.com/eduardolat/pgbackweb/internal/view/reqctx"
"github.com/eduardolat/pgbackweb/internal/view/web/alpine"
"github.com/eduardolat/pgbackweb/internal/view/web/component"
"github.com/eduardolat/pgbackweb/internal/view/web/layout"
Expand All @@ -18,6 +19,7 @@ import (

func (h *handlers) indexPageHandler(c echo.Context) error {
ctx := c.Request().Context()
reqCtx := reqctx.GetCtx(c)

databasesQty, err := h.servs.DatabasesService.GetDatabasesQty(ctx)
if err != nil {
Expand All @@ -43,12 +45,14 @@ func (h *handlers) indexPageHandler(c echo.Context) error {
return echoutil.RenderGomponent(
c, http.StatusOK,
indexPage(
databasesQty, destinationsQty, backupsQty, executionsQty, restorationsQty,
reqCtx, databasesQty, destinationsQty, backupsQty, executionsQty,
restorationsQty,
),
)
}

func indexPage(
reqCtx reqctx.Ctx,
databasesQty dbgen.DatabasesServiceGetDatabasesQtyRow,
destinationsQty dbgen.DestinationsServiceGetDestinationsQtyRow,
backupsQty dbgen.BackupsServiceGetBackupsQtyRow,
Expand Down Expand Up @@ -300,9 +304,8 @@ func indexPage(
),
}

return layout.Dashboard(layout.DashboardParams{
Title: "Summary",
Body: content,
LoadChartJS: true,
return layout.Dashboard(reqCtx, layout.DashboardParams{
Title: "Summary",
Body: content,
})
}
8 changes: 5 additions & 3 deletions internal/view/web/dashboard/webhooks/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"net/http"

"github.com/eduardolat/pgbackweb/internal/util/echoutil"
"github.com/eduardolat/pgbackweb/internal/view/reqctx"
"github.com/eduardolat/pgbackweb/internal/view/web/component"
"github.com/eduardolat/pgbackweb/internal/view/web/htmx"
"github.com/eduardolat/pgbackweb/internal/view/web/layout"
Expand All @@ -13,12 +14,13 @@ import (
)

func (h *handlers) indexPageHandler(c echo.Context) error {
reqCtx := reqctx.GetCtx(c)
return echoutil.RenderGomponent(
c, http.StatusOK, indexPage(),
c, http.StatusOK, indexPage(reqCtx),
)
}

func indexPage() gomponents.Node {
func indexPage(reqCtx reqctx.Ctx) gomponents.Node {
content := []gomponents.Node{
html.Div(
html.Class("flex justify-between items-start"),
Expand Down Expand Up @@ -58,7 +60,7 @@ func indexPage() gomponents.Node {
}),
}

return layout.Dashboard(layout.DashboardParams{
return layout.Dashboard(reqCtx, layout.DashboardParams{
Title: "Webhooks",
Body: content,
})
Expand Down
2 changes: 1 addition & 1 deletion internal/view/web/layout/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func Auth(params AuthParams) gomponents.Node {
Language: "en",
Title: title,
Head: []gomponents.Node{
head(headParams{}),
head(),
},
Body: []gomponents.Node{
components.Classes{
Expand Down
Loading

0 comments on commit 5093aa5

Please sign in to comment.