Skip to content

Commit

Permalink
feat: Meta var to views
Browse files Browse the repository at this point in the history
  • Loading branch information
ItsNotGoodName committed Jul 24, 2023
1 parent f52ff0d commit 73fbe77
Show file tree
Hide file tree
Showing 19 changed files with 186 additions and 87 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ all: dep-web snapshot
snapshot:
goreleaser release --snapshot --clean

preview:
go generate ./... && go run .

dev:
air

Expand Down
12 changes: 6 additions & 6 deletions web/controllers/attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ import (
"github.com/ItsNotGoodName/smtpbridge/internal/envelope"
"github.com/ItsNotGoodName/smtpbridge/internal/procs"
"github.com/ItsNotGoodName/smtpbridge/pkg/pagination"
"github.com/ItsNotGoodName/smtpbridge/web/helpers"
h "github.com/ItsNotGoodName/smtpbridge/web/helpers"
"github.com/gofiber/fiber/v2"
)

func Attachments(c *fiber.Ctx, cc core.Context) error {
// Request
page, err := strconv.Atoi(c.Query("page", "1"))
if err != nil {
return helpers.Error(c, err, http.StatusBadRequest)
return h.Error(c, err, http.StatusBadRequest)
}

perPage, err := strconv.Atoi(c.Query("perPage", "1"))
if err != nil {
return helpers.Error(c, err, http.StatusBadRequest)
return h.Error(c, err, http.StatusBadRequest)
}

// Execute
Expand All @@ -30,16 +30,16 @@ func Attachments(c *fiber.Ctx, cc core.Context) error {
}
res, err := procs.EnvelopeAttachmentList(cc, pagination.NewPage(page, perPage), filter)
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

// Response
queries := c.Queries()
if res.PageResult.Page > res.PageResult.TotalPages {
return c.Redirect("/attachments?" + helpers.Query(queries, "page", res.PageResult.TotalPages))
return c.Redirect("/attachments?" + h.Query(queries, "page", res.PageResult.TotalPages))
}

return c.Render("attachments", fiber.Map{
return h.Render(c, "attachments", fiber.Map{
"Queries": queries,
"Attachments": res.Attachments,
"AttachmentsPageResult": res.PageResult,
Expand Down
39 changes: 21 additions & 18 deletions web/controllers/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ package controllers
import (
"github.com/ItsNotGoodName/smtpbridge/internal/core"
"github.com/ItsNotGoodName/smtpbridge/internal/procs"
"github.com/ItsNotGoodName/smtpbridge/web/helpers"
h "github.com/ItsNotGoodName/smtpbridge/web/helpers"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
)

type LoginData struct {
Flash string
func loginData(flash string) fiber.Map {
return fiber.Map{
"Flash": flash,
}
}

func Login(c *fiber.Ctx, cc core.Context) error {
return c.Render("login", LoginData{})
return h.Render(c, "login", fiber.Map{})
}

func LoginPost(c *fiber.Ctx, cc core.Context, store *session.Store) error {
Expand All @@ -24,50 +26,51 @@ func LoginPost(c *fiber.Ctx, cc core.Context, store *session.Store) error {
// Execute
err := procs.AuthHTTPLogin(cc, username, password)
if err != nil {
if helpers.IsHTMXRequest(c) {
return c.Render("login", LoginData{Flash: err.Error()}, "form")
if h.IsHTMXRequest(c) {
return h.Render(c, "login", loginData(err.Error()), "form")
}
return c.Render("login", LoginData{Flash: err.Error()})
return h.Render(c, "login", loginData(err.Error()))
}

// Response
sess, err := store.Get(c)
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

sess.Set("auth", true)
if err := sess.Save(); err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

return helpers.Redirect(c, "/")
return h.Redirect(c, "/")
}

func Logout(c *fiber.Ctx, cc core.Context, store *session.Store) error {
sess, err := store.Get(c)
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

sess.Delete("auth")
if err := sess.Save(); err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

return helpers.Redirect(c, "/login")
return h.Redirect(c, "/login")
}

func UserRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
if procs.AuthHTTPAnonymous(cc) {
return helpers.NotFound(c)
return h.NotFound(c)
}

return authRequire(c, cc, store)
}

func AuthRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {
if procs.AuthHTTPAnonymous(cc) {
c.Locals(h.AnonymousContextKey, true)
return c.Next()
}

Expand All @@ -82,25 +85,25 @@ func authRequire(c *fiber.Ctx, cc core.Context, store *session.Store) error {

auth := sess.Get("auth")
if auth == nil {
return helpers.Redirect(c, "/login")
return h.Redirect(c, "/login")
}

return c.Next()
}

func AuthRestrict(c *fiber.Ctx, cc core.Context, store *session.Store) error {
if procs.AuthHTTPAnonymous(cc) {
return helpers.Redirect(c, "/")
return h.Redirect(c, "/")
}

sess, err := store.Get(c)
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

auth := sess.Get("auth")
if auth != nil {
return helpers.Redirect(c, "/")
return h.Redirect(c, "/")
}

return c.Next()
Expand Down
28 changes: 14 additions & 14 deletions web/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/ItsNotGoodName/smtpbridge/internal/procs"
"github.com/ItsNotGoodName/smtpbridge/pkg/pagination"
"github.com/ItsNotGoodName/smtpbridge/web"
"github.com/ItsNotGoodName/smtpbridge/web/helpers"
h "github.com/ItsNotGoodName/smtpbridge/web/helpers"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/filesystem"
)
Expand All @@ -18,18 +18,18 @@ func Index(c *fiber.Ctx, cc core.Context) error {
// Execute
storage, err := procs.StorageGet(cc)
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

messages, err := procs.EnvelopeMessageList(cc, pagination.NewPage(1, 5), envelope.MessageFilter{})
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

policy := procs.RetentionPolicyGet(cc)

// Response
return c.Render("index", fiber.Map{
return h.Render(c, "index", fiber.Map{
"Messages": messages.Messages,
"Storage": storage,
"RetentionPolicy": policy,
Expand All @@ -40,11 +40,11 @@ func IndexStorageTable(c *fiber.Ctx, cc core.Context) error {
// Execute
storage, err := procs.StorageGet(cc)
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

// Response
return c.Render("index", fiber.Map{
return h.Render(c, "index", fiber.Map{
"Storage": storage,
}, "storage-table")
}
Expand All @@ -53,11 +53,11 @@ func IndexRecentEnvelopesTable(c *fiber.Ctx, cc core.Context) error {
// Execute
messages, err := procs.EnvelopeMessageList(cc, pagination.NewPage(1, 5), envelope.MessageFilter{})
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

// Response
return c.Render("index", fiber.Map{
return h.Render(c, "index", fiber.Map{
"Messages": messages.Messages,
}, "recent-envelopes-table")
}
Expand All @@ -73,17 +73,17 @@ func Send(c *fiber.Ctx, cc core.Context) error {
// Request
envelopeID, err := strconv.ParseInt(c.FormValue("envelope"), 10, 64)
if err != nil {
return helpers.Error(c, err, http.StatusBadRequest)
return h.Error(c, err, http.StatusBadRequest)
}
endpointID, err := strconv.ParseInt(c.FormValue("endpoint"), 10, 64)
if err != nil {
return helpers.Error(c, err, http.StatusBadRequest)
return h.Error(c, err, http.StatusBadRequest)
}

// Execute
err = procs.EndpointSend(cc, envelopeID, endpointID)
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

// Response
Expand All @@ -94,7 +94,7 @@ func Vacuum(c *fiber.Ctx, cc core.Context) error {
// Execute
err := procs.DatabaseVacuum(cc)
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

// Response
Expand All @@ -106,7 +106,7 @@ func Trim(c *fiber.Ctx, cc core.Context) error {
// Execute
err := procs.TrimStart(cc)
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

// Response
Expand All @@ -115,5 +115,5 @@ func Trim(c *fiber.Ctx, cc core.Context) error {
}

func SomethingWentWrong(c *fiber.Ctx) error {
return c.Render("something-went-wrong", fiber.Map{"Error": ""})
return h.Render(c, "something-went-wrong", fiber.Map{"Error": ""})
}
8 changes: 4 additions & 4 deletions web/controllers/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@ import (

"github.com/ItsNotGoodName/smtpbridge/internal/core"
"github.com/ItsNotGoodName/smtpbridge/internal/procs"
"github.com/ItsNotGoodName/smtpbridge/web/helpers"
h "github.com/ItsNotGoodName/smtpbridge/web/helpers"
"github.com/gofiber/fiber/v2"
)

func Endpoints(c *fiber.Ctx, cc core.Context) error {
// Execute
ends, err := procs.EndpointList(cc)
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

// Response
return c.Render("endpoints", fiber.Map{
return h.Render(c, "endpoints", fiber.Map{
"Endpoints": ends,
})
}
Expand All @@ -26,7 +26,7 @@ func EndpointTest(c *fiber.Ctx, cc core.Context, id int64) error {
// Execute
err := procs.EndpointTest(cc, id)
if err != nil {
return helpers.Error(c, err)
return h.Error(c, err)
}

// Response
Expand Down
Loading

0 comments on commit 73fbe77

Please sign in to comment.