2021-12-03 03:11:01 -08:00
|
|
|
package msg
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/sessions"
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-07-09 17:57:05 -07:00
|
|
|
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/log"
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/session"
|
2021-12-03 03:11:01 -08:00
|
|
|
)
|
|
|
|
|
2022-02-10 05:56:07 -08:00
|
|
|
// Type is a message type
|
2021-12-03 03:11:01 -08:00
|
|
|
type Type string
|
|
|
|
|
|
|
|
const (
|
2022-02-10 05:56:07 -08:00
|
|
|
// TypeSuccess represents a success message type
|
2021-12-03 13:44:04 -08:00
|
|
|
TypeSuccess Type = "success"
|
2022-02-10 05:56:07 -08:00
|
|
|
|
|
|
|
// TypeInfo represents a info message type
|
|
|
|
TypeInfo Type = "info"
|
|
|
|
|
|
|
|
// TypeWarning represents a warning message type
|
2021-12-03 13:44:04 -08:00
|
|
|
TypeWarning Type = "warning"
|
2022-02-10 05:56:07 -08:00
|
|
|
|
|
|
|
// TypeDanger represents a danger message type
|
|
|
|
TypeDanger Type = "danger"
|
2021-12-03 03:11:01 -08:00
|
|
|
)
|
|
|
|
|
2021-12-21 18:42:53 -08:00
|
|
|
const (
|
|
|
|
// sessionName stores the name of the session which contains flash messages
|
|
|
|
sessionName = "msg"
|
|
|
|
)
|
2021-12-03 03:11:01 -08:00
|
|
|
|
2021-12-21 18:42:53 -08:00
|
|
|
// Success sets a success flash message
|
|
|
|
func Success(ctx echo.Context, message string) {
|
|
|
|
Set(ctx, TypeSuccess, message)
|
2021-12-03 13:44:04 -08:00
|
|
|
}
|
|
|
|
|
2021-12-21 18:42:53 -08:00
|
|
|
// Info sets an info flash message
|
|
|
|
func Info(ctx echo.Context, message string) {
|
|
|
|
Set(ctx, TypeInfo, message)
|
2021-12-03 13:44:04 -08:00
|
|
|
}
|
|
|
|
|
2021-12-21 18:42:53 -08:00
|
|
|
// Warning sets a warning flash message
|
|
|
|
func Warning(ctx echo.Context, message string) {
|
|
|
|
Set(ctx, TypeWarning, message)
|
2021-12-03 13:44:04 -08:00
|
|
|
}
|
|
|
|
|
2021-12-21 18:42:53 -08:00
|
|
|
// Danger sets a danger flash message
|
|
|
|
func Danger(ctx echo.Context, message string) {
|
|
|
|
Set(ctx, TypeDanger, message)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
// Set adds a new flash message of a given type into the session storage.
|
|
|
|
// Errors will be logged and not returned.
|
2021-12-21 18:42:53 -08:00
|
|
|
func Set(ctx echo.Context, typ Type, message string) {
|
|
|
|
if sess, err := getSession(ctx); err == nil {
|
|
|
|
sess.AddFlash(message, string(typ))
|
|
|
|
save(ctx, sess)
|
|
|
|
}
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
|
|
|
|
2024-06-14 09:35:35 -07:00
|
|
|
// Get gets flash messages of a given type from the session storage.
|
|
|
|
// Errors will be logged and not returned.
|
2021-12-21 18:42:53 -08:00
|
|
|
func Get(ctx echo.Context, typ Type) []string {
|
|
|
|
var msgs []string
|
|
|
|
|
|
|
|
if sess, err := getSession(ctx); err == nil {
|
|
|
|
if flash := sess.Flashes(string(typ)); len(flash) > 0 {
|
|
|
|
save(ctx, sess)
|
2021-12-11 16:32:34 -08:00
|
|
|
|
2021-12-21 18:42:53 -08:00
|
|
|
for _, m := range flash {
|
|
|
|
msgs = append(msgs, m.(string))
|
|
|
|
}
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
2021-12-21 18:42:53 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
return msgs
|
|
|
|
}
|
2021-12-03 03:11:01 -08:00
|
|
|
|
2021-12-21 18:42:53 -08:00
|
|
|
// getSession gets the flash message session
|
|
|
|
func getSession(ctx echo.Context) (*sessions.Session, error) {
|
2024-06-15 13:56:47 -07:00
|
|
|
sess, err := session.Get(ctx, sessionName)
|
2021-12-21 18:42:53 -08:00
|
|
|
if err != nil {
|
2024-06-14 18:01:48 -07:00
|
|
|
log.Ctx(ctx).Error("cannot load flash message session",
|
|
|
|
"error", err,
|
|
|
|
)
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
2021-12-21 18:42:53 -08:00
|
|
|
return sess, err
|
2021-12-03 03:11:01 -08:00
|
|
|
}
|
2021-12-03 13:44:04 -08:00
|
|
|
|
2021-12-21 18:42:53 -08:00
|
|
|
// save saves the flash message session
|
|
|
|
func save(ctx echo.Context, sess *sessions.Session) {
|
|
|
|
if err := sess.Save(ctx.Request(), ctx.Response()); err != nil {
|
2024-06-14 18:01:48 -07:00
|
|
|
log.Ctx(ctx).Error("failed to set flash message",
|
|
|
|
"error", err,
|
|
|
|
)
|
2021-12-21 18:42:53 -08:00
|
|
|
}
|
2021-12-03 13:44:04 -08:00
|
|
|
}
|