Skip to content

Commit

Permalink
add ability to share session messages
Browse files Browse the repository at this point in the history
  • Loading branch information
seagyn committed May 26, 2022
1 parent a604d26 commit 822a04e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
9 changes: 6 additions & 3 deletions inertia.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,36 @@ import (
"strings"

"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
)

// Inertia type.
type Inertia struct {
url string
rootTemplate string
version string
store *session.Store
sharedProps map[string]interface{}
sharedFuncMap template.FuncMap
templateFS fs.FS
}

// New function.
func New(url, rootTemplate, version string) *Inertia {
func New(url, rootTemplate, version string, store *session.Store) *Inertia {
i := new(Inertia)
i.url = url
i.rootTemplate = rootTemplate
i.version = version
i.store = store
i.sharedProps = make(map[string]interface{})
i.sharedFuncMap = template.FuncMap{"marshal": marshal}

return i
}

// NewWithFS function.
func NewWithFS(url, rootTemplate, version string, templateFS fs.FS) *Inertia {
i := New(url, rootTemplate, version)
func NewWithFS(url, rootTemplate, version string, store session.Session, templateFS fs.FS) *Inertia {
i := New(url, rootTemplate, version, store)
i.templateFS = templateFS

return i
Expand Down
9 changes: 9 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ func (i *Inertia) Middleware(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusConflict)
}

sess, _ := i.store.Get(c)

message := sess.Get("message")

if message != nil {
i.Share("message", sess.Get("message"))
sess.Delete("message")
}

// Go to next middleware:
return c.Next()
}

0 comments on commit 822a04e

Please sign in to comment.