Skip to content

Commit

Permalink
fix: use the fiber-htmx components
Browse files Browse the repository at this point in the history
  • Loading branch information
katallaxie authored Sep 28, 2024
1 parent 69ca185 commit 1d3b14b
Show file tree
Hide file tree
Showing 36 changed files with 206 additions and 47 deletions.
20 changes: 16 additions & 4 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/zeiss/fiber-goth/providers"
"github.com/zeiss/fiber-goth/providers/entraid"
"github.com/zeiss/fiber-goth/providers/github"
reload "github.com/zeiss/fiber-reload"
seed "github.com/zeiss/gorm-seed"
"github.com/zeiss/service-lens/internal/adapters/db"
"github.com/zeiss/service-lens/internal/adapters/handlers"
"github.com/zeiss/service-lens/internal/cfg"
Expand All @@ -24,6 +22,10 @@ import (
"github.com/spf13/cobra"
goth "github.com/zeiss/fiber-goth"
adapter "github.com/zeiss/fiber-goth/adapters/gorm"
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/toasts"
reload "github.com/zeiss/fiber-reload"
seed "github.com/zeiss/gorm-seed"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/schema"
Expand Down Expand Up @@ -146,9 +148,15 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R
CookieHTTPOnly: true,
}

userHandler := handlers.NewUserHandler()

handlers := handlers.New(store)

app := fiber.New()
app := fiber.New(
fiber.Config{
ErrorHandler: toasts.DefaultErrorHandler,
},
)
app.Use(requestid.New())
app.Use(logger.New())
app.Use(reload.Environment(s.cfg.Flags.Environment))
Expand All @@ -159,9 +167,13 @@ func (s *WebSrv) Start(ctx context.Context, ready server.ReadyFunc, run server.R

app.Use(goth.NewProtectMiddleware(gothConfig))

compFuncConfig := htmx.Config{
ErrorHandler: toasts.DefaultErrorHandler,
}

app.Get("/", handlers.Dashboard())
app.Post("/", handlers.PostDashboard())
app.Get("/login", handlers.Login())
app.Get("/login", htmx.NewCompFuncHandler(userHandler.Login(), compFuncConfig))
app.Get("/login/:provider", goth.NewBeginAuthHandler(gothConfig))
app.Get("/auth/:provider/callback", goth.NewCompleteAuthHandler(gothConfig))
app.Get("/logout", goth.NewLogoutHandler(gothConfig))
Expand Down
26 changes: 26 additions & 0 deletions internal/adapters/handlers/user_handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package handlers

import (
"github.com/gofiber/fiber/v2"
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/service-lens/internal/components"
"github.com/zeiss/service-lens/internal/components/login"
)

type UserHandler struct{}

func NewUserHandler() *UserHandler {
return &UserHandler{}
}

func (h *UserHandler) Login() htmx.CompFunc {
return func(c *fiber.Ctx) (htmx.Node, error) {
return components.Page(
components.PageProps{},
components.Wrap(
components.WrapProps{},
login.NewLogin(),
),
), nil
}
}
5 changes: 1 addition & 4 deletions internal/components/layout.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ func Layout(p LayoutProps, children ...htmx.Node) htmx.Node {
drawers.DrawerContentProps{
ID: "drawer",
},
toasts.Toasts(
toasts.ToastsProps{},
toasts.Toaster(),
),
toasts.Toasts(),
htmx.Div(
htmx.ClassNames{
"h-full": true,
Expand Down
129 changes: 129 additions & 0 deletions internal/components/login/page.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package login

import (
htmx "github.com/zeiss/fiber-htmx"
"github.com/zeiss/fiber-htmx/components/buttons"
"github.com/zeiss/fiber-htmx/components/cards"
"github.com/zeiss/fiber-htmx/components/dividers"
"github.com/zeiss/fiber-htmx/components/forms"
"github.com/zeiss/fiber-htmx/components/links"
)

func NewLogin() htmx.Node {
return htmx.Fragment(
htmx.Section(
htmx.Merge(
htmx.ClassNames{
"bg-gray-50": true,
"dark:bg-gray-900": true,
},
),
),
htmx.Div(
htmx.Merge(
htmx.ClassNames{
"flex": true,
"flex-col": true,
"items-center": true,
"justify-center": true,
"px-6": true,
"py-8": true,
"mx-auto": true,
"md:h-screen": true,
"lg:py-0": true,
},
),
cards.CardBordered(
cards.CardProps{
ClassNames: htmx.ClassNames{
"w-96": true,
"max-w-lg": true,
},
},
cards.Body(
cards.BodyProps{},
cards.Title(
cards.TitleProps{},
htmx.Text("Sign in to your account"),
),
htmx.Div(
htmx.ClassNames{
"mt-4": true,
},
links.Button(
links.LinkProps{
ClassNames: htmx.ClassNames{
"w-full": true,
"btn-outline": true,
},
Href: "/login/entraid",
},
htmx.Text("Login on Microsoft Entra ID"),
),
),
htmx.Div(
htmx.ClassNames{
"mt-4": true,
},
links.Button(
links.LinkProps{
ClassNames: htmx.ClassNames{
"w-full": true,
"btn-outline": true,
},
Href: "/login/github",
},
htmx.Text("Login on GitHub"),
),
),
dividers.Divider(
dividers.DividerProps{},
htmx.Text("OR"),
),
htmx.Form(
htmx.HxPost("/login"),
forms.FormControl(
forms.FormControlProps{
ClassNames: htmx.ClassNames{
"py-4": true,
},
},
forms.TextInputBordered(
forms.TextInputProps{
Name: "username",
Placeholder: "indy@jones.com",
},
),
),
forms.FormControl(
forms.FormControlProps{},
forms.TextInputBordered(
forms.TextInputProps{
Name: "password",
Placeholder: "supersecret",
},
htmx.Type("password"),
),
),
cards.Actions(
cards.ActionsProps{
ClassNames: htmx.ClassNames{
"py-4": true,
"-mb-4": true,
},
},
buttons.Outline(
buttons.ButtonProps{
ClassNames: htmx.ClassNames{
"w-full": true,
},
},
htmx.Text("Login"),
),
),
),
),
),
),
)
}
5 changes: 5 additions & 0 deletions internal/components/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ func Page(props PageProps, children ...htmx.Node) htmx.Node {
htmx.Attribute("src", "https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"),
htmx.Attribute("defer", ""),
),
htmx.Script(
htmx.Attribute("src", "https://unpkg.com/fiber-htmx@1.3.26"),
htmx.CrossOrigin("anonymous"),
htmx.Attribute("defer", ""),
),
}, props.Head...),
},
htmx.Body(
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/dashboard/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewShowDashboardController(store seed.Database[ports.ReadTx, ports.ReadWrit

// Error ...
func (d *ShowDashboardController) Error(err error) error {
return toasts.RenderToasts(d.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Get ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/designs/comments.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewCommentsController(store seed.Database[ports.ReadTx, ports.ReadWriteTx])

// Error ...
func (l *CommentsControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Post ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/designs/comments/reaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewReactionCommentController(store seed.Database[ports.ReadTx, ports.ReadWr

// Error ...
func (l *ReactionCommentControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Delete ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/designs/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (l *CreateDesignControllerImpl) Prepare() error {

// Error ...
func (l *CreateDesignControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Post ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/designs/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func NewDesignDeleteController(store seed.Database[ports.ReadTx, ports.ReadWrite

// Error ...
func (l *DesignDeleteControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Prepare ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/designs/edit/body/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func NewUpdateController(store seed.Database[ports.ReadTx, ports.ReadWriteTx]) *

// Error ...
func (l *UpdateControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Prepare ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/designs/edit/title/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func NewUpdateController(store seed.Database[ports.ReadTx, ports.ReadWriteTx]) *

// Error ...
func (l *UpdateControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Prepare ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/designs/reactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewReactionController(store seed.Database[ports.ReadTx, ports.ReadWriteTx])

// Error ...
func (l *ReactionControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Delete ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/designs/search-templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewSearchTemplatesController(store seed.Database[ports.ReadTx, ports.ReadWr

// Error ...
func (l *SearchTemplatesControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Prepare ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/designs/search-workflows.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewSearchWorkflowsController(store seed.Database[ports.ReadTx, ports.ReadWr

// Error ...
func (l *SearchWorkflowsControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Prepare ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/designs/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func NewTagController(store seed.Database[ports.ReadTx, ports.ReadWriteTx]) *Tag

// Error ...
func (l *TagControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Delete ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/designs/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewTaskController(store seed.Database[ports.ReadTx, ports.ReadWriteTx]) *Ta

// Error ...
func (l *TaskControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Prepare ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/environments/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewCreateEnvironmentController(store seed.Database[ports.ReadTx, ports.Read

// Error ...
func (l *CreateEnvironmentControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Prepare ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/lenses/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NewLensController(store seed.Database[ports.ReadTx, ports.ReadWriteTx]) *Ne

// Error ...
func (l *NewLensControllerImpl) Error(err error) error {
return toasts.RenderToasts(l.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Post ...
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/lenses/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewLensPublishController(store seed.Database[ports.ReadTx, ports.ReadWriteT

// Error ...
func (c *LensPublishControllerImpl) Error(err error) error {
return toasts.RenderToasts(c.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Delete ...
Expand Down
5 changes: 0 additions & 5 deletions internal/controllers/profiles/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ func NewCreateProfileController(store seed.Database[ports.ReadTx, ports.ReadWrit
return &CreateProfileControllerImpl{store: store}
}

// Error ...
func (l *CreateProfileControllerImpl) Error(err error) error {
return err
}

// Prepare ...
func (l *CreateProfileControllerImpl) Prepare() error {
validate = validator.New()
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/profiles/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (p *NewProfileControllerImpl) Post() error {
return tx.CreateProfile(ctx, &profile)
})
if err != nil {
return toasts.RenderToasts(p.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

return p.Redirect(fmt.Sprintf(utils.ShowProfileUrlFormat, profile.ID))
Expand Down
2 changes: 1 addition & 1 deletion internal/controllers/tags/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NewTagController(store seed.Database[ports.ReadTx, ports.ReadWriteTx]) *New

// Error ...
func (t *NewTagControllerImpl) Error(err error) error {
return toasts.RenderToasts(t.Ctx(), toasts.Error(err.Error()))
return toasts.Error(err.Error())
}

// Prepare ...
Expand Down
Loading

0 comments on commit 1d3b14b

Please sign in to comment.